简体   繁体   中英

Android NFC App not pushing Ndef messages

I am working on creating an Android App that uses NFC to register touches from device to device. I am using two Nexus 7s for testing.

Ideal use case is to have the App active on one device, not active on the other. The active device pushes an NdefMessage with a record containing some data for the passive device app to deal with. The passive device passes a recording containing some data back to the active app.

I have the following intent filters set up in my manifest:

  <activity android:name=".MainActivity" android:label="@string/title_activity_main">
        <intent-filter>
          <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
          <data android:mimeType="application/com.killerapprejji.MainActivity"/>
          <data android:mimeType="application/com.*"/>
          <data android:mimeType="application/com.killerapprejji.*"/>
          <data android:mimeType="application/*"/>
          <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
  </activity>

In my MainActivity I have the following in onCreate to set up the NFC adapter:

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
PendingIntent pendingIntent = PendingIntent.getActivity(
            this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mNfcPendingIntent = pendingIntent;
    // Intent filters for exchanging over p2p.
if(mNfcAdapter != null){
    IntentFilter ndefDetected = new
                       IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("application/*");    
    }
    catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    this.intentFiltersArray = new IntentFilter[] {ndef, };
    Log.d(this.toString(), "mNfcPendingIntent: ");
    mNdefExchangeFilters = new IntentFilter[] { ndefDetected,defendDetected };

}

I have the following for onNewIntent :

protected void onNewIntent(Intent intent) {
    // NDEF exchange mode
    Log.d("onNewIntent", intent.getAction());
    if (NfcAdapter.ACTION_NDEF_DISCOVERED == (intent.getAction())) {
        NdefMessage[] msgs = getNdefMessages(intent);
        for(int i = 0; i < msgs.length; i++){
            Log.d("onNewIntent", "found new NdefMessage");
        }
     }

    finish();
}

Right now, I run this call:

public void setIdleMessage(){
    InteractionHistory intHist = InteractionHistory.getInstance();
    NdefMessage attackNdefMessage = null;
    NdefRecord[] ndefRecords = new NdefRecord[10];
    ndefRecords[0] = NdefRecord.createMime("application/com.killerapprejji.NfcHandle", new String("attack,attacker:"
            + intHist.getDisplayName() 
            + ",attackerid:" + "1").getBytes());
    attackNdefMessage = new NdefMessage(ndefRecords[0]);
    // need to come up with a way to end if the above try/catch fails
    mNfcAdapter.setNdefPushMessage(attackNdefMessage, this);
}

Expecting it to set the NdefPushMessage. Whenever I touch put the two devices in range of NFC, I still only get the optional "Touch to beam" interface.

Any ideas as to how I can pick up these intents, or if my NdefMessage is even getting sent as I expect?

"Touch to Beam" message, is the one defined by Google to push that NDEF message that you are expecting to share on your setNdefPushMessage, so until you push that screen, nothing will be sent to the other device.

In fact, that API you are using from Android is called Android BEAM

In the other way, if one device is pushing NDEF messages, it cannot receive messaged until you stop pushing , so you have to implement the NDefPushCallback to catch the succesfull sent of your NDEF message, to stop pushing in the first device, and then it will be able to receive a new intent (NDEF message received) from the other device


I recommend you make a test app first using URI mime type, or plain text (something easy) to make sure that your "Share Logic" is OK. When you confirm that, you can rollback to your own mime types

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM