Sending URL from Android to Windows Phone via NFC gives Play Store link

911 views Asked by At

I am trying to use NFC to send a URL from an Android app to a WP8 phone.

When beaming to an Android device, the URL is sent correctly. However, when beaming to WP8, IE loads a link to the Play Store instead of the one I want to send (e.g. "http://www.stackoverflow.com").

The Play Store link is: "https://play.google.com/store/apps/details?id=com.example.conductrnfc&feature=beam". Where "com.example.conductrnfc" is the package name in the project.

The code I used to generate the NFC message is given below. Is there something I'm doing wrong here that breaks compatibility with WP8?

NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
nfc.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
    @Override
    public NdefMessage createNdefMessage(NfcEvent event)
    {
        NdefRecord uriRecord = NdefRecord.createUri(urlString);
        return new NdefMessage(new NdefRecord[] { uriRecord });
    }
}, 
this);
2

There are 2 answers

3
72DFBF5B A0DF5BE9 On

Can you try this:

NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
nfc.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
    @Override
    public NdefMessage createNdefMessage(NfcEvent event)
    {
        byte[] payload = urlString.getBytes();
        NdefRecord uriRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload); 
        return new NdefMessage(new NdefRecord[] { uriRecord });
    }
}, 
this);
2
Michael Roland On

Eventhough I still miss some more debugging results from the OP, I thought I'd give it a shot:

As the discussion in the commands revealed that the createNdefMessage callback is not called when interacting with a WP8 phone, it would be interesting why this appens and how to prevent this. Unfortunately I have no details about the actual lifecycle of the activity, so I can only guess what might go wrong.

  1. One reason why a registered createNdefMessage callback may not be called is that the activity that registered the callback is no longer in the foreground. So there may be a difference between an Android device and a WP8 device that causes the current activity to be paused.

  2. Another reason would be that the WP8 device interupts communication before the Android NFC stack had the time to call the createNdefMessage callback method. However, this should be detectable as the Beam UI would typically disappear before the user is able to click it.

One cause for reason 1 may be that the WP8 device itself sends an NDEF message that causes intent processing on the Android device. If that's the case, a method to overcome this problem may be to register for the foreground dispatch system. This would prevent regular intent processing and would directly send all incoming NDEF messages to the current activity:

@Override
public void onResume() {
    super.onResume();
    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    adapter.enableForegroundDispatch(this, pi, null, null);
}

@Override
public void onNewIntent(Intent intent) {
    if (intent != null) {
        String action = intent.getAction();
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) ||
            NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action) ||
            NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
            Log.d("NdefTest", "This problem was actually caused by an incoming NDEF message.");
        }
    }
}