I'm developing an Android app which interacts with other devices using NFC. This interaction consists of 2 steps basically:
- When the device receives a specific URI by NFC from other device, the app is launched.
- When the app starts, it sends a NDEF message back to the other device.
For the first step, I have added the following lines to the AndroidManifest.xml file. That way, the MainActivity
will be launched when the device receives a URI of type myprotocol:something
:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="myprotocol" />
</intent-filter>
For the second step, my MainActivity
class implements CreateNdefMessageCallback
and OnNdefPushCompleteCallback
. The code looks like following:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
adapter.setNdefPushMessageCallback(this, this);
adapter.setOnNdefPushCompleteCallback(this, this);
}
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
NdefRecord uriRecord = NdefRecord.createUri("protocol:something");
NdefMessage message = new NdefMessage(new NdefRecord[] { uriRecord });
return message;
}
@Override
public void onNdefPushComplete(NfcEvent event) {
}
So, now the problem:
These two parts work fine independently, but not when I add both to the app.
That is, if I add the first part, the app is correctly launched when receiving the NDEF message. Also, if I only add the second part, if I tap my device to other device while running the app, I see Touch to beam interface, and the NDEF message is sent.
The problem is, that if I add both, when tapping to the other device, the app is launched, but the Touch to beam interface is never shown. If I separate the devices and tap again, the MainActivity
is relaunched, but I never get to see the option to send the message.
How could I achieve the desired scenario?