Question

Hi I have two Samsung galaxy S4. I developed an NFC based android app that will beam simple text to the phone in contact (target phone) when a button is touched on the phone that runs the app(source phone). My problem is that when I run my app on the source phone and bring the target phone closer to the source phone, samsung's default app launches(This app shows a screenshot of the current activity in the source phone and asks you to touch the screen and then it launches the same activity in the target phone). I want to disable this, so that the app I developed works without any issues. How do I achieve this?

Was it helpful?

Solution

If you want to (or have to1) use Android Beam, there is no way to avoid the Beam UI (that's the activity with the small screenshot of your activity).

In order to properly use Beam (with the Beam UI) and send your text2 is to do the following:

  1. Enable the foreground dispatch system for the activity on your target device, so that the received NDEF message is immediately processed by your app. You can then receive NDEF messages (or rather NFC discovery events in your activity's onNewIntent() method. See this answer on how to enable the foreground dispatch.
  2. Disable beaming for the activity on your target device using something like this (e.g. in the receiving activity's onCreate() method):

    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter != null) {
        nfcAdapter.setNdefPushMessage(null, this);
    }
    
  3. Change the NDEF message sent by your source device to the NDEF message that you actually want to send (e.g. an NDEF message containing your Text record). If you don't do this, Android will (by default) send an Android Application Record that gets your default activity to be launched on the target device. You can set up the NDEF message like this (e.g. in the sending activity's onCreate() method):

    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter != null) {
        byte[] text = "Hello World!".getBytes("UTF-8");
        byte[] recordPayload = new byte[3 + text.length];
        recordPayload[0] = (byte)0x02; //UTF-8, 2-byte language code
        recordPayload[1] = (byte)0x65; //'e'
        recordPayload[2] = (byte)0x6E; //'n'
        System.arraycopy(text, 0, recordPayload, 3, text.length);
        NdefMessage ndefMsg = new NdefMessage(
            new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, null, recordPayload)
        );
        nfcAdapter.setNdefPushMessage(ndefMsg, this);
    }
    

If both devices use Android 4.4+, instead of using Beam, you could use Host-based Card Emulation (HCE) on one device and NFC reader mode on the other device. When you combine those features (i.e. you have a HCE on-host card emulation service on one device and put the second device into reader mode), both devices can communicate with each other using ISO 7816-4 APDUs. The advantage of this would be that you can completely skip the annoying Beam UI.



1) That's the case if you are using an Android version < 4.4.
2) I assume an NDEF Text record, right?

OTHER TIPS

Try instantiating the NfcAdapter and calling the disableForegroundDispatch or disableReaderMode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top