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 が期待どおりに送信されているかどうかについてのアイデアはありますか?