NFC。扫描NDEF消息时启动一个活动消息、NFC、NDEF

2023-09-06 07:08:22 作者:浮海苍茫

我想,当我的智能手机扫描一个NDEF消息开始活动。这是我的清单:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.androidbeam
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =16
        机器人:targetSdkVersion =17/>

    <使用-权限的Andr​​oid:名称=android.permission.NFC/>

    <使用特征的android:NAME =android.hardware.nfc/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名称=com.example.androidbeam.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动机器人:名称=com.example.androidbeam.SendTextActivity>

        < /活性GT;
        <活动机器人:名称=com.example.androidbeam.ReceiverNDEFActivity>
            <意向滤光器>
                <作用机器人:名称=android.nfc.action.NDEF_DISCOVERED/>
                <类机器人:名称=android.intent.category.DEFAULT/>
                <数据
                    机器人:主机=分机
                    机器人:路径preFIX =/ com.example:externalType
                    机器人:计划=vnd.android.nfc/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>

< /舱单>
 

每当我扫描我的NDEF消息,我的主要活动发布会,但我想我的ReceiverNDEFActivity推出。我不知道为什么是这样的情况。

这是我NdefMessage:

  @覆盖
公共NdefMessage createNdefMessage(NfcEvent事件){
    byte []的有效载荷=新的String(你好);
    字符串域=为com.example;
    字符串类型=externalType;
    NdefRecord extRecord = NdefRecord.createExternal(域,类型,有效载荷);

    NdefMessage味精=新NdefMessage(新NdefRecord [] {extRecord});
    返回味精;
}
 
升级到iOS11后,iPhone的NFC功能有这么多

解决方案

诀窍是,NFC论坛外部类型名称不区分大小写。然而,Android的意图过滤系统是区分敏感。因此,你必须始终使用所有小写外部输入您的意图过滤器的名称:

 <活动机器人:名称=com.example.androidbeam.ReceiverNDEFActivity>
    <意向滤光器>
        <作用机器人:名称=android.nfc.action.NDEF_DISCOVERED/>
        <类机器人:名称=android.intent.category.DEFAULT/>
        <数据
            机器人:计划=vnd.android.nfc
            机器人:主机=分机
            机器人:路径preFIX =/ com.example:externaltype/>
    &所述; /意图滤光器>
< /活性GT;
 

注意 NdefRecord.createExternal(...)方法会自动将所有类型名称为小写的拼写。

I'm trying to start a activity when my smartphone scans an NDEF message. This is my manifest:

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

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.NFC" />

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

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

        </activity>
        <activity android:name="com.example.androidbeam.ReceiverNDEFActivity" >
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="ext"
                    android:pathPrefix="/com.example:externalType"
                    android:scheme="vnd.android.nfc" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Whenever I scan my NDEF message, my main activity launches but I'd like my ReceiverNDEFActivity to launch. I'm not sure why this is the case.

This is my NdefMessage:

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    byte[] payload= new String("Hello");
    String domain = "com.example";
    String type = "externalType"; 
    NdefRecord extRecord = NdefRecord.createExternal(domain, type, payload);

    NdefMessage msg = new NdefMessage(new NdefRecord[] { extRecord });
    return msg;
}

解决方案

The trick is that NFC Forum external type names are case-insensitive. However, Android's intent filter system is case-SENSITIVE. Therefore, you must always use ALL lower-case external type names in your intent filters:

<activity android:name="com.example.androidbeam.ReceiverNDEFActivity" >
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data
            android:scheme="vnd.android.nfc"
            android:host="ext"
            android:pathPrefix="/com.example:externaltype" />
    </intent-filter>
</activity>

Note that the NdefRecord.createExternal(...) method will automatically convert all type names to lower-case spelling.