CL So
CL So

Reputation: 3759

NFC unavailable when camera is open

This is my Torch app:

final Camera.Parameters p;
Camera camera=Camera.open();

camera.setPreviewTexture(new SurfaceTexture(0));
p = camera.getParameters();

p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();

When my app is running, some devices cannot detect NFC tags. I noticed this happens with the Nexus 5X, specifically.

It appears certain devices can't detect NFC when the camera is running.

Can this problem be solved programmatically?

Upvotes: 13

Views: 2925

Answers (2)

Martijn Coenen
Martijn Coenen

Reputation: 981

The reason is that pretty late in the development cycle of the 5X, it was found that the NFC controller polling introduced noise in the camera sensor. The only feasible fix at that time was to turn off NFC when the camera is opened.

The community states that those devices have the same problem (not exhaustive):

  • Nexus 5X
  • LG G5
  • Samsung XCover 4 (Android 8.1, Android 9.5)
  • OnePlus 5
  • HTC U11
  • Nokia 6.1
  • Pixel 1 XL
  • Samsung (All devices with Android 11)
  • Samsung S10e (Android 11)
  • Samsung Note10 (Android 11)
  • Samsung S20

Upvotes: 16

Alberto Bailac
Alberto Bailac

Reputation: 29

To solve this problem you can add this little code to your onStop, in the activity that uses the camera. If you need NFC, in some devices you need to release the camera.

@Override
protected void onStop() {
    super.onStop();
    try
    {
        android.hardware.Camera mCamera = android.hardware.Camera.open();
        mCamera.release();
        mCamera = null;
    }
    catch(RuntimeException e)
    {
        Log.e(TAG, "init_camera: " + e);
        return;
    }
}

Upvotes: -1

Related Questions