Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

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.

NdefRecordr1= NdefRecord.createMime("text/ThisIsMyMIMEType", ...);
NdefRecordr2= NdefRecord.createMime("text/tHISiSmYmimetYPE", ...);
NdefRecordr3= NdefRecord.createMime("text/THISISMYMIMETYPE", ...);
NdefRecordr4= 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><actionandroid:name="android.nfc.action.NDEF_DISCOVERED" /><categoryandroid:name="android.intent.category.DEFAULT" /><dataandroid:mimeType="text/thisismymimetype" /></intent-filter>

Solution 2:

I finally got it. Even though my package name has capital letter in it, you must write your MIME TYPE in small letters in your code and in your intent filter.

I know in realty they're not the same package. However, MIME TYPE in NFC will still recognize your app. Just make sure to write the correct package when you create application record. If you notice I had to use the correct package name which include CAPS. Otherwise your application won't be found.

publicNdefMessagecreateNdefMessage(NfcEvent event) {
    String text = ("Beam me up, Android!\n\n" +
                   "Beam Time: " + System.currentTimeMillis());
    NdefMessage msg = newNdefMessage(
            newNdefRecord[] { NdefRecord.createMime(
                    "application/com.example.main_activity", text.getBytes())
     /**
      * The Android Application Record (AAR) is commented out. When
      * a device receives a push with an AAR in it, the application
      * specified in the AAR is guaranteed to run.
      *
      * The AAR overrides the tag dispatch system. You can add it
      * back in to guarantee that this activity starts when
      * receiving a beamed message. For now, this code uses
      * the tag dispatch system.
      */
      ,NdefRecord.createApplicationRecord("com.example.Main_Activity")
    });
    return msg;
}

<intent-filter>
    <actionandroid:name="android.nfc.action.NDEF_DISCOVERED"/><categoryandroid:name="android.intent.category.DEFAULT"/><dataandroid:mimeType="application/com.example.main_activity"/>
</intent-filter>

Post a Comment for "Nfc And Mime Type Case Sensitive"