I am building an application which scans NFC/Mifare cards and gets their unique identifier. Unfortunately, despite following the Android NFC api tutorial, the intent as declared both in the manifest and as a foreground dispatcher is not being registered with the device and so Android chooses to open up the default NFC application instead. My manifest is as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myname.application">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.NFC"/>
<uses-feature
android:name="android.hardware.nfc"
android:required ="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Scanner">
<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.NDEF_DISCOVERED"/>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter"></meta-data>
</activity>
</application>
</manifest>
and my foreground dispatcher is as follows:
@Override
protected void onResume() {
super.onResume();
setupForegroundDispatch(this, mNfcAdapter);
}
@Override
protected void onPause() {
stopForegroundDispatch(this, mNfcAdapter);
super.onPause();
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
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);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[1];
String[][] techList = new String[][]{};
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
try {
filters[0].addDataType(MIMETYPE_TEXT_PLAIN);
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("Check your mime type.");
}
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}
public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
adapter.disableForegroundDispatch(activity);
}
As I'm not sure if it makes a difference, the platform that the application is currently being run on is a Huawei P10 Plus. I've had a few compatibility issues with it in the past so this may be relevant.
I do this successfully but without declare anything about NFC in the manifest, only the NFC permissions. I don't pass to the foregroundDispath the filters and the techList and I verify that the intent that I received is a nfc intent.
@Override
protected void onPause() {
super.onPause();
mAdapter = NfcAdapter.getDefaultAdapter(context);
if(mAdapter != null){
mAdapter.disableForegroundDispatch(activity);
}
}
@Override
protected void onResume() {
super.onResume();
mAdapter = NfcAdapter.getDefaultAdapter(context);
if(mAdapter != null) {
mPendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mAdapter.enableForegroundDispatch(activity, mPendingIntent, null, null);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if ( NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) ) {
readNFC(intent);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.