androidnfcmime-typesintentfilterndef

NFC and MIME TYPE case sensitive


I'm attempting just the basic version of NFC, but then I discovered that MIME TYPE is case sensitive. The package name for my app has one capital letter.

Package name: com.example.Main_Activity

<intent-filter>
  <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
  <category android:name="android.intent.category.DEFAULT"/>
  <data android:mimeType="application/com.example.Main_Activity"/>
</intent-filter>

Is there a way around it?


Solution

  • MIME types are case-insensitive as per the RFC. However, Android's intent filter matiching is case-sensitive. In order to overcome this problem you should always use lower-case MIME types only.

    Specifically with the Android NFC API's MIME type record helper methods, MIME types will automatically be converted to lower-case letters only. So calling the method NdefRecord.createMime() with a mixed-case type name will always result into the creation of a lower-case only MIME type name. E.g.

    NdefRecord r1 = NdefRecord.createMime("text/ThisIsMyMIMEType", ...);
    NdefRecord r2 = NdefRecord.createMime("text/tHISiSmYmimetYPE", ...);
    NdefRecord r3 = NdefRecord.createMime("text/THISISMYMIMETYPE", ...);
    NdefRecord r4 = NdefRecord.createMime("text/thisismymimetype", ...);
    

    will all result into the creation of the same MIME type record type:

    +----------------------------------------------------------+
    | MIME:text/thisismymimetype | ...                         |
    +----------------------------------------------------------+
    

    So your intent filter will also need to be all-lower-case letters:

    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/thisismymimetype" />
    </intent-filter>