フォアグラウンドディスパッチを使用してMifareClassic1kカードのIDを読み取ろうとしています。ログからわかるように、フォアグラウンドディスパッチを有効にすることはできますが、onNewIntent()メソッドを呼び出すことはできません。任意の提案をいただければ幸いです。
MainActivity.java
...
@Override
protected void onResume() {
setupForegroundDispatch(this, mAdapter);
super.onResume();
}
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
System.out.println("Setup FGD."); // i can see this output.
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[1];
String[][] techList = new String[][]{};
// Notice that this is the same filter as in our manifest.
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
try {
filters[0].addDataType(MIME_TEXT_PLAIN);
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("Check your mime type.");
}
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
System.out.println("Enabled FGD."); // and this one.
}
protected void onNewIntent(Intent intent) {
System.out.println("Intent."); but i cannot see this one,
handleIntent(intent);
}
private void handleIntent(Intent intent) {
System.out.println("Handle."); // and this one.
String action = intent.getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
System.out.println("NDEF discovered.");
....
AndroidManifest
....
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<action android:name="android.nfc.action.TAG_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
....
すべてのMIFAREクラシックタグ(テキストレコード(またはMIMEタイプtext / plainを含むMIMEタイプレコードを含むものだけでなく)を検出できるようにする場合は、フォアグラウンドディスパッチを調整して、特定のタグテクノロジを検出する必要があります。特定のNDEFレコードタイプ:
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
final Intent pendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter[] filters = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
String[][] techList = new String[][] { new String[] { MifareClassic.class.getName() } };
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
BroadcomのNFCチップセットを搭載したデバイスでMIFAREClassicタグのUIDも取得する場合(NXP独自のテクノロジのライセンスの問題により、これらのデバイスはMIFAREClassicをMIFAREClassicとして検出できません)、代わりにすべてのNfcA
タグをフィルタリングできます(MIFARE ClassicNfcA
すべてのデバイスで同様に検出されるため、NfcA
とMifareClassic
)の両方をフィルタリングする必要はありません。
String[][] techList = new String[][] { new String[] { NfcA.class.getName() } };
最後に、マニフェストのインテントフィルタが、コードのインテントフィルタと一致しません。フォアグラウンドディスパッチの同等のインテントフィルターは次のようになります。
<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="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
付nfc_tech_filter.xml
含みます:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
</resources>
または(に一致する場合NfcA
):
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
</resources>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加