Search code examples
androidnfcintentfilterndefandroid-applicationrecord

Intent-filter android.nfc.action.NDEF_DISCOVERED not working


I'm building an app that sends data through NFC. I had it working but changing namespaces, some build options to make release APK, updating Android Studio and other things, but not changing the code, it's not working anymore.

Here is my manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.devdr.touch"
    android:versionCode="1"
    android:versionName="0.1" >
[...]
<activity
    android:name="com.devdr.touch.ui.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name="com.devdr.touch.ui.NFCDisplayActivity"
    android:label="NFC Data Display">
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="application/com.devdr.touch" />
    </intent-filter>
</activity>

Here is my NDEF message:

// Record to launch Play Store if app is not installed
NdefRecord appRecord = NdefRecord.createApplicationRecord(this.getPackageName());

// Record with actual data we care about
NdefRecord relayRecord = NdefRecord.createMime(
        "application/" + this.getPackageName()                  ,
         byteArray);

// Complete NDEF message with both records
NdefMessage mNdefMessage = new NdefMessage(new NdefRecord[]{relayRecord
        , appRecord
});

Breakpoint at this.getPackageName() gives: "com.devdr.touch"

Now the phone receiving the data launches the phone parameters or my app but on MainActivity if AAR not present, or my app but on MainActivity if AAR is present.

Any suggestions how I could debug that?

Edit 1

Adding a part of my manifest and the value of this.getPackageName(). It began to bug more or less when I started signing my APK to make a release.

Edit 2

Ok, now I know where the problem is, but don't know how to solve it: Variable byteArray is made of an object. This object contains 2 strings and 1 image. Now I'm sending bigger images and that seems not to be handled correctly. With a 50ko image it takes 40 seconds! Any idea why it's so slow?


Solution

  • Obviously, if you interrupt the write transfer before the NDEF message was completely written to the NFC tag (e.g. by tearing the tag out of range), the NFC tag won't be in consistent state and the Android device will later not detect any NDEF message at all (or it might even detect an NDEF message with invalid content!)

    I'm even surprised that you have an NFC tag that coud fit 50 KiB of data. But since you do, the 40 seconds sound reasonable for that amount of data. NFC supports a transfer speed of 106 kbps (or even 212 kbps and 424 kbps), which would be approx. 13 KiB per second. However, this is only the gross data rate. You can't expect to transfer your actual net data with that speed. In fact, depending on the tag, you will get a significantly lower net data rate. For instance, with a Type 2 tag, the underlying WRITE command will write only 4 bytes at a time with 2 bytes of command overhead + framing + response round trip time + delay between command-response sequences. Even for Type 4 tags (where the payload-overhead ratio is usually significantly smaller), you will typically have a net data rate of around 1/10 of the gross data rate (at least that's my experience with reading electronic passports on Android).