Search code examples
androidnfcndefnfc-p2pandroid-beam

Get notified when NDEF is sent / touch to beam is pressed


I am using setNdefPushMessageCallback to send an NDEF message (text/plain) through Android Beam (TM) from one Android device to another one. In onResume I am checking ACTION_NDEF_DISCOVERED so in this way the Android phone that is detecting/reciving the message is being notified.

But how can I get notified in the other phone (the one sending the message)? As I can think there should be a way to detect when we touch the screen to beam the message, but I have not seen any constans on NfcAdapter that is called for this purpose.

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    mAdapter.setNdefPushMessageCallback(this, this);
}

@Override
public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
    ...
    NdefRecord ndefRecord = NdefRecord.createMime("text/plain", message.getBytes());
    NdefMessage ndefMessage = new NdefMessage(ndefRecord);
    return ndefMessage;

}

@Override
protected void onResume(){
    super.onResume();

    Intent intent = getIntent();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage message = (NdefMessage) rawMessages[0]; 
        status = new String(message.getRecords()[0].getPayload());
    }
    ...
}

Solution

  • You can register an OnNdefPushCompleteCallback:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        mAdapter.setOnNdefPushCompleteCallback(this, this);
        mAdapter.setNdefPushMessageCallback(this, this);
    }
    
    @Override
    public void onNdefPushComplete(NfcEvent event) {
        ...
    }
    

    This callback will be invoked when the message was successfully pushed over Beam.