简体   繁体   中英

NFC stops working on Device after Camera Intent

I'm working on an App that takes photos and scans NFC Tags. The NFC Tag scanning works fine until I take a photo (via camera intent).

What I do:

In onResume() I call setupForegroundDispatch() and in onPause() I call stopForegroundDispatch().

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity, activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
    adapter.enableForegroundDispatch(activity, pendingIntent, null, null);
}

public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    adapter.disableForegroundDispatch(activity);
}

To start the camera I call:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                imageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);

To sum it up:

  • The activity starts and in onResume() setupForgroundDispatch() will be called.
  • scanning of NFC Tags works as expected
  • I click a Button to start the camera via Intent
    • If I don't take a photo and go back to my activity -> NFC works well
    • If I take a picture and onActivityResult() will be called -> NFC does not work for about 20-30 seconds and then starts working again. I mean NFC stops working on the whole device, not only in the app. I can take a photo, then resume to my activity, then remove the app from recents and NFC does not work for the mentioned 20-30 Seconds. Then starts working again.

What I tried

  • I checked that stopForegroundDispatch() will be called before the camera starts and that setupForegroundDispatch() will be called after the camera is done
  • I removed my code from onActivityResult() to ensure that any further code interferes with the NFC.

I think you may have run into this

NFC is not available when I opened camera

Maybe you should try with a different device, if you have one

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM