Reputation: 261
I have a problem with my app. It is using nfc tags for some actions (opening/locking door). The app has a intent-filter at the default activity:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/de.juwei.myapplication" />
</intent-filter>
But it seems it is ignoring the mimeType setting complety because it is starting even if i attach an empty tag to the phone and also if i try to beam data from another phone.
The code simply doing this:
public class MainActivity extends ActionBarActivity {
private NfcAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = NfcAdapter.getDefaultAdapter(this);
Intent intent = getIntent();
// see if app was started from a tag
if (mAdapter != null && intent.getType() != null && intent.getType().equals("application/de.juwei.myapplication")) {
}
}
@Override
protected void onResume() {
super.onResume();
if (mAdapter != null) {
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
}
@Override
protected void onPause() {
super.onPause();
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
}
}
@Override
public void onNewIntent(Intent intent) {
if (mAdapter != null && intent.getType() != null && intent.getType().equals("application/de.juwei.myapplication")) {
// ... reading tag
}
}
}
getIntent() is null if the tag doesn't has my mimetype. So the app is just starting if for example holding to smartphones together trying to beam some data. The activity is also starting if i hold my sony smartwatch 3 to my phone...
The very strange thing is - if i try to reproduce this on a new app with just that simple code, the app is not starting on every nfc command.
But in my main app, there are no more nfc specific methods.
I am completly lost. Does anyone know how to track/debug why the app is opening by every piece of nfc data?
Best regards, Juergen
Upvotes: 1
Views: 509
Reputation: 7653
In enableForegroundDispatch method you have to add filters and techLists
For instance:
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*"); /* Handles all MIME based dispatches.
You should specify only the ones that you need. */
}
catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mFilters = new IntentFilter[] {
ndef,
};
mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
NfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
mTechLists);
Upvotes: 1
Reputation: 261
Looks like the problem was the enableForegroundDispatch. I commented out both calls at onResume and onPause and changed Manifest to android:launchMode="singleTask" and it seems to work. Must be some nfc bug, i don't know.
However, within my first tests it seems to work now, the app isn't starting every time i hold a nfc tag on back of the device.
Upvotes: 0