turtleboy
turtleboy

Reputation: 7574

NFC tag scan only triggers my app to start

I have an app that scans NFC tags, collects the data from the tag and sends that data to the server. The data on the tag is in the following format:

1,3,30012,somebodys name

These tags are placed in client's houses for carers to scan. Each scan logs them into a call.

This has been working fine so far. The problem i have now is that the carers must use another app alongside our app which scans a different NFC tag, with different data on it and sends it to a different server. This app is installed on the same carer's phone.

This other app will scan NFC tags that might have data on it in the following format:

abc|123??

The question is how can i ensure that when the carer scans our tag, ONLY our app is launched?

Is there a way in the manifest of specifying this in the intent-filters for my app?

This is what i have in the manifest for my Activity. I understand that if an Intent is launched and there are 2 apps that could process an NFC tag then a list is presented to the user. The Android docs discourage against this.

Thanks in advance.

[Edit] One thing i have noticed, if the phone is on the home screen and i scan a tag, then my app is launched. This is fine if the carer scans the correct tag.

<activity android:name=".NfcscannerActivity"
    android:screenOrientation="portrait" >
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.carefreegroup.rr3.QRCODE_ACTION" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data
        android:name="android.nfc.action.TECH_DISCOVERED"
        android:resource="@xml/nfc_tech_filter" />
</activity>

Upvotes: 1

Views: 2307

Answers (1)

griffinjm
griffinjm

Reputation: 503

You could create a custom mimeType in your NDEF message and then create an intent-filter which matches it exactly. This would mean your app would be launched as it is the most specific filter.

Example:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="application/vnd.com.my.app.package.customString" />
</intent-filter>

Edit: Alternatively if you cannot create a custom mimeType then perhaps your utility will create a custom URL? Then you can create a filter for the custom url instead:

<intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data
                android:scheme="http"
                android:host="www.mywebsite.net" 
                android:path="/customPathForAction" />
</intent-filter>

Upvotes: 5

Related Questions