I need to disable beam and enable on certain fragments. So I have base fragment, vendor fragment and product fragment. In base fragment onResume I call:
NfcAdapter adapter = NfcAdapter.getDecaultAdapter(getActivity());
adapter.setNdefPushMessage(null, getActivity());
So at this point when I put the two NFC devices together, it plays the sound but there is no UI involved. This is what I want.
Then the user goes to products, orders the products and review sthem on the ordersummary fragment. In this I create my NDEF message and pass it into my NDEF push message callback:
mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
NdefMessage message = new NdefMessage((new NdefRecord[]{createMime("application/com.me.myactivity.fragments", mToBeam.getBytes()) }));
return message;
}
}, mActivity);
mNfcAdapter.setOnNdefPushCompleteCallback(new NfcAdapter.OnNdefPushCompleteCallback() {
@Override
public void onNdefPushComplete(NfcEvent event) {
}
}, mActivity);
However on successful transmission I then bring the user back to the product page where in the onResume()
I call:
mNfcAdapter.setNdefPushMessage(null, getActivity());
This is called however, now when I bring the two devices together I do get the UI even though the message is null.
You are mixing two different approaches: static and callback based. In order to disable the callback-based approach, you have to call setNdefPushMessageCallback
passing null
as the callback:
adapter.setNdefPushMessage(null, getActivity());
adapter.setNdefPushMessageCallback(null, getActivity());
adapter.setOnNdefPushCompleteCallback(null, getActivity());