【问题标题】:Android NFC App not pushing Ndef messagesAndroid NFC App 不推送 Ndef 消息
【发布时间】:2012-12-04 03:31:53
【问题描述】:

我正在创建一个 Android 应用程序,它使用 NFC 来注册设备之间的触摸。我正在使用两台 Nexus 7 进行测试。

理想的用例是让应用在一台设备上处于活动状态,而在另一台设备上不处于活动状态。主动设备推送一个带有记录的 NdefMessage,其中包含一些数据供被动设备应用程序处理。被动设备将包含一些数据的记录传递回主动应用程序。

我在清单中设置了以下意图过滤器:

  <activity android:name=".MainActivity" android:label="@string/title_activity_main">
        <intent-filter>
          <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
          <data android:mimeType="application/com.killerapprejji.MainActivity"/>
          <data android:mimeType="application/com.*"/>
          <data android:mimeType="application/com.killerapprejji.*"/>
          <data android:mimeType="application/*"/>
          <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
  </activity>

在我的 MainActivity 中,我在 onCreate 中有以下内容来设置 NFC 适配器:

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
PendingIntent pendingIntent = PendingIntent.getActivity(
            this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mNfcPendingIntent = pendingIntent;
    // Intent filters for exchanging over p2p.
if(mNfcAdapter != null){
    IntentFilter ndefDetected = new
                       IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("application/*");    
    }
    catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    this.intentFiltersArray = new IntentFilter[] {ndef, };
    Log.d(this.toString(), "mNfcPendingIntent: ");
    mNdefExchangeFilters = new IntentFilter[] { ndefDetected,defendDetected };

}

我有以下 onNewIntent :

protected void onNewIntent(Intent intent) {
    // NDEF exchange mode
    Log.d("onNewIntent", intent.getAction());
    if (NfcAdapter.ACTION_NDEF_DISCOVERED == (intent.getAction())) {
        NdefMessage[] msgs = getNdefMessages(intent);
        for(int i = 0; i < msgs.length; i++){
            Log.d("onNewIntent", "found new NdefMessage");
        }
     }

    finish();
}

现在,我运行这个电话:

public void setIdleMessage(){
    InteractionHistory intHist = InteractionHistory.getInstance();
    NdefMessage attackNdefMessage = null;
    NdefRecord[] ndefRecords = new NdefRecord[10];
    ndefRecords[0] = NdefRecord.createMime("application/com.killerapprejji.NfcHandle", new String("attack,attacker:"
            + intHist.getDisplayName() 
            + ",attackerid:" + "1").getBytes());
    attackNdefMessage = new NdefMessage(ndefRecords[0]);
    // need to come up with a way to end if the above try/catch fails
    mNfcAdapter.setNdefPushMessage(attackNdefMessage, this);
}

期望它设置 NdefPushMessage。每当我触摸将两个设备置于 NFC 范围内时,我仍然只能获得可选的“Touch to beam”界面。

关于如何获取这些意图的任何想法,或者我的 NdefMessage 是否按预期发送?

【问题讨论】:

    标签: android nfc intentfilter ndef


    【解决方案1】:

    “Touch to Beam”消息是由 Google 定义的,用于推送您希望在 setNdefPushMessage 上共享的 NDEF 消息,因此在您推送该屏幕之前,不会向其他设备发送任何内容。

    事实上,您在 Android 中使用的那个 API 称为 Android BEAM

    另一方面,如果一个设备正在推送 NDEF 消息,直到你停止推送它才能接收到消息,所以你必须实现 NDefPushCallback 来捕捉成功发送您的 NDEF 消息,停止推送第一台设备,然后它将能够从另一台设备接收新的意图(收到 NDEF 消息)


    我建议您首先使用 URI mime 类型或纯文本(很简单)制作一个测试应用程序,以确保您的“共享逻辑”正常。确认后,您可以回滚到自己的 mime 类型

    【讨论】:

    • 你确定推送时收不到消息吗?链接到文档?
    • developer.android.com/guide/topics/connectivity/nfc/nfc.html 一个活动一次只能推送一条 NDEF 消息,因此 setNdefPushMessageCallback() 优先于 setNdefPushMessage() 发送数据的活动必须在前台。两台设备的屏幕都必须解锁。如果您的 Activity 启用了 Android Beam 并处于前台,则标准的 Intent 调度系统将被禁用。但是,如果您的 Activity 还启用了前台调度,那么它仍然可以扫描与前台调度中设置的意图过滤器匹配的标签。
    • 所以当我在一台设备上运行应用程序时,启动光束,应用程序会在空闲设备上弹出,这意味着我的意图正在被过滤?
    • 我不是 100% 确定,但是我在使用两个 Nexus S 为我开发一个小应用程序时遇到了同样的问题,并且在我停止 foregroundDispatch 之前,我从未收到关于推送 Nexus 的 ndef 消息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多