1
votes

Is it possible to launch a specific activity in an app using NFC tag?

I want to start an activity called MainMap which is not the main activity. I've tried using a tag writer for writing mime record and still failing.

Here's the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.km.parkit"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />

<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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=".MainMap"
        android:label="@string/app_name" >

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

        </activity>

    <activity
        android:name="com.km.parkit.parka"
        android:label="@string/app_name" />

    <activity
        android:name="com.km.parkit.parkb"
        android:label="@string/app_name" />

    <activity
        android:name="com.km.parkit.parkc"
        android:label="@string/app_name" />

    <activity
        android:name="com.km.parkit.parkd"
        android:label="@string/app_name" />


    <!-- Google Maps API Key -->
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyBf-0hUDrDTK-NOveZrnT8wec5TTTLqjAw" />

 <meta-data 
     android:name="com.google.android.gms.version"
     android:value="@integer/google_play_services_version" />

</application>



</manifest>

I wrote the MIME type "application/com.km.parkit" into the NFC tag using https://github.com/balloob/Android-NFC-Tag-Writer

What am I missing here?

Oh my problem is that the app doesn't get started when I tap it to the NFC tag, when I tap the tag, it shows the usual popup box showing the programs which I can use to access the NFC tag or read the datas, namely NFC Tag Info and New Tag Collected.

1
Have you tried changing the mimeType in the intent-filter with application/com.km.parkit (in lower case)?Bruno Parmentier
Try adding android.nfc.action.TECH_DISCOVERED and android.nfc.action.TAG_DISCOVERED on intent-filter action.Héctor
Yes, it has to be in lower case, thanks Bruno! :D And I don't want to use TECH_DISCOVERED because I would like the app to be in the top priority for the NFC tag launching :)Kevin Murvie

1 Answers

4
votes

The NDEF_DISCOVERED intent filter should use an all lower-case MIME type name (and that's also how you should store the type name on the tag):

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

The problem is that MIME types are case-insensitive as per the RFC while Android's intent filter matching is case-sensitive. As a consequence, the helper methods for handling MIME type records on Android will automatically convert type names to lower-case letters only and you should therefore only use all lower-case letters for MIME type names.

For instance, using the method NdefRecord.createMime() with a mixed-case type name will always result into the creation of a lower-case only MIME type name:

NdefRecord r1 = NdefRecord.createMime("application/ThisIsMyMIMEType", ...);
NdefRecord r2 = NdefRecord.createMime("application/thisismymimetype", ...);

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

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