1

NFC を使用してデバイスからデバイスへのタッチを登録する Android アプリの作成に取り組んでいます。テスト用に 2 台の Nexus 7 を使用しています。

理想的な使用例は、アプリを 1 つのデバイスでアクティブにし、他のデバイスではアクティブにしないことです。アクティブ デバイスは、パッシブ デバイス アプリが処理するデータを含むレコードを含む 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 では、NFC アダプターをセットアップするために onCreate に次のものがあります。

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 を設定することを期待しています。2 つのデバイスをタッチして NFC の範囲内に置くと、オプションの「Touch to beam」インターフェイスしか表示されません。

これらのインテントを取得する方法、または NdefMessage が期待どおりに送信されているかどうかについてのアイデアはありますか?

4

1 に答える 1

1

「Touch to Beam」メッセージは、setNdefPushMessage で共有する予定の NDEF メッセージをプッシュするために Google によって定義されたメッセージです。そのため、その画面をプッシュするまで、他のデバイスには何も送信されません。

実際、Android から使用している API は Android BEAM と呼ばれます。

逆に、1 つのデバイスが NDEF メッセージをプッシュしている場合、そのデバイスはプッシュを停止するまでメッセージを受信できません。そのため、 NDefPushCallbackを実装して、送信された NDEF メッセージの成功をキャッチし、最初のデバイスへのプッシュを停止してから、他のデバイスから新しいインテント (受信した NDEF メッセージ) を受信できるようになります


「Share Logic」が正常であることを確認するために、最初に URI MIME タイプまたはプレーン テキスト (簡単なもの) を使用してテスト アプリを作成することをお勧めします。それを確認したら、独自の MIME タイプにロールバックできます

于 2012-12-04T12:51:26.337 に答える