0 Replies - 347 Views - Last Post: 26 May 2015 - 02:04 AM

#1 GaryDoo   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 142
  • Joined: 10-February 13

Android NFC Peer to Peer Data transfer

Posted 26 May 2015 - 02:04 AM


I'm developing an app that requires nfc peer to peer communication. I followed a tutorial in Android Wireless Application Development book and implemented code (below). This works fine however, I don't want the 'touch to beam' ui appearing after the discovery of another nfc enabled device. I need the data to transfer without user input.

I have done a lot of digging on this and I don't think there is an option to disable this UI. So I think the best option is to get the NFC source package and try to remove the UI call from there. However, I don't know where to get my hands on this code.

If anyone has any ideas on this I'd appreciate hearing from you.

    private static final String DEBUG_TAG = "AndroidBeamActivity";
    public static final String MIMETYPE = "application/com.androidbook.simplewireless";
    NfcAdapter mNfcAdapter;
    TextView mStatusText;
    EditText messageToBeam;
    private static final int BEAM_BEAMED = 0x1001;

    public void onCreate(Bundle savedInstanceState) {

        mStatusText = (TextView) findViewById(R.id.status);
        messageToBeam = (EditText) findViewById(R.id.messageToBeam);

        // Check for available NFC Adapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

        if (mNfcAdapter == null) {
            mStatusText.setText("NFC is not available on this device.");

        // Register to create and NDEF message when another device is in range
        mNfcAdapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() {
            public NdefMessage createNdefMessage(NfcEvent event) {
                Time time = new Time();
                String message = messageToBeam.getText().toString();
                String text = (message + " \n[Sent @ "
                        + time.format("%H:%M:%S") + "]");
                byte[] mime = MIMETYPE.getBytes(Charset.forName("US-ASCII"));
                NdefRecord mimeMessage = new NdefRecord(
                        NdefRecord.TNF_MIME_MEDIA, mime, new byte[0], text
                NdefMessage msg = new NdefMessage(
                        new NdefRecord[] {
                                        .createApplicationRecord("com.androidbook.simplewireless") });
                return msg;
        }, this);

        // And handle the send status

                new OnNdefPushCompleteCallback() {

                    public void onNdefPushComplete(NfcEvent event) {
                }, this);

    private final Handler mHandler = new Handler() {
        public void handleMessage(Message message) {
            switch (message.what) {
            case BEAM_BEAMED:
                mStatusText.setText("Your message has been beamed");

    public void onresume() {
        // Did we receive an NDEF message?
               Intent intent = getIntent();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
            try {
                Parcelable[] rawMsgs = intent

                // we created the message, so we know the format

                NdefMessage msg = (NdefMessage) rawMsgs[0];
                NdefRecord[] records = msg.getRecords();
                byte[] firstPayload = records[0].getPayload();
                String message = new String(firstPayload);
            } catch (Exception e) {
                Log.e(DEBUG_TAG, "Error retrieving beam message.", e);

    public void onNewIntent(Intent intent) {
        // handle singleTop so we don't launch a bunch of instances..
        if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
            return; // ignore NFC intents


    public void onNFCSettingsClick(View view) {
        Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);

Is This A Good Question/Topic? 0
  • +

Page 1 of 1