Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Android: NFC not working after Camera is unmounted #516

Closed
3 of 4 tasks
ldstein opened this issue Oct 14, 2021 · 11 comments · Fixed by #1466 or #2174
Closed
3 of 4 tasks

🐛 Android: NFC not working after Camera is unmounted #516

ldstein opened this issue Oct 14, 2021 · 11 comments · Fixed by #1466 or #2174
Labels
🐛 bug Something isn't working

Comments

@ldstein
Copy link

ldstein commented Oct 14, 2021

What were you trying to do?

A couple of our Android test handsets are unable to simultaneously receive data from the camera and NFC reader.

This is not that unusual. In some cases it's done to limit interference caused by the NFC chip when capturing images. In other cases, it's a limitation of the underlying hardware (number of pathways, SPI channels, etc).

https://stackoverflow.com/questions/34607168/nfc-unavailable-when-camera-is-open
https://stackoverflow.com/questions/68636728/usage-of-camera-blocks-nfc-reading-writing

For the affected handsets, when using RNVC, we noticed NFC remains permanently blocked after the Camera component is unmounted.

NFC starts working again after:

  • Changing focus to a different app, then returning.
  • Closing then relaunching the App.
  • Fire an intent to start MainActivity with flags ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK.

I think it's related to RNVC because we have tested a different camera component which does not exhibit this issue.

My hunch is maybe the Camera is not being fully released after the component is unmounted?

App here for testing simultaneous NFC + Camera access: https://github.com/ldstein/MeshCameraTest/tree/nfc-test

Reproduceable Code

See repo

What happened instead?

No crashes, no errors. Just no response from NFC reader.

Relevant log output

No response

Device

Huawei VOC-L29 P30 Pro (Android 10), Xiaomi Redmi Note 9 Pro (Android 10 M2007JI7C)

VisionCamera Version

2.9.1

Additional information

@ldstein ldstein added the 🐛 bug Something isn't working label Oct 14, 2021
@ldstein ldstein changed the title Android - NFC fails to work after Camera is unmounted🐛 Android - NFC not working after Camera is unmounted🐛 Oct 14, 2021
@mrousavy mrousavy changed the title Android - NFC not working after Camera is unmounted🐛 🐛 Android - NFC not working after Camera is unmounted Oct 14, 2021
@ldstein ldstein changed the title 🐛 Android - NFC not working after Camera is unmounted 🐛 Android: NFC not working after Camera is unmounted Oct 14, 2021
@mrousavy
Copy link
Owner

well that's a weird one for sure. Thank you for the detailed report and reproduceable sample!

I'll try to take a look at this soon, but last time I checked, the Camera component unmounts and cleans up correctly. 🤔

@kojoo112
Copy link

kojoo112 commented Jun 3, 2022

import {BackHandler} from 'react-native';

const [isActive, setIsActive] = useState(true);

useEffect(() => {
const getPermission = async () => {
const status = await Camera.requestCameraPermission();
setHasPermission(status === 'authorized');
};

const backAction = () => {
  setIsActive(false);
};

const backHandler = BackHandler.addEventListener(
  'hardwareBackPress',
  backAction,
);

getPermission();

return () => {
  backHandler.remove();
};
}, []);

i had a same problem, try this. it works!

@Chenbao2021
Copy link

Chenbao2021 commented Sep 23, 2022

I have just meet the same problem with an old samsung modele . But when i using a Redmi that works well . So i guess some model is incompatible to use nfc and camera in same time .

@florinr
Copy link

florinr commented Feb 18, 2023

Had the same problem on Samsung M33. I was using RNVC with frame processor to scan QR, and after to trigger NFC technology. For me it worked to set isActive as false on <Camera> before trying NFC.

          <Camera
            style={styles.camera}
            device={device}
            isActive={active}
            frameProcessor={frameProcessor}
            frameProcessorFps={2}
          />

@sommcz
Copy link

sommcz commented Mar 10, 2023

I've the same problem on Android 13 devices (A12 is not affected). Devices tested: Samsung E22, Samsung Flip 3 (A13), SamsungA13 5G....

Settings isActive to false works only when screen with camera is shown at the time when prop is set. If isActive is to false in screen's blur event, NFC is still blocked (probably because Camera behave differently when it is in view/out of view).

@alexrififi
Copy link

I confirm, on Samsung s20 ultra.
It helped setting isActive to false before closing the camera

@mrousavy
Copy link
Owner

Hey! I've rewritten the entire Android codebase of VisionCamera from CameraX to Camera2 in the efforts of ✨ VisionCamera V3.

I just now completed the Camera2 rewrite and I believe the core structure is running, but there might be some edge cases to iron out. Can you try and test the PR #1674 for me to see if you can still reproduce this issue here?

Here's an instruction on how you can test that: #1674 (comment)

If the issue cannot be reproduced with that version/PR anymore, then hoorayy, I fixed it! 🎉
Otherwise please let me know and I'll keep this issue open to keep track of it.

Thank you!

@sommcz
Copy link

sommcz commented Oct 7, 2023

I've tested this issue with V3 + codescanner & simple frameprocessor (instead of previous vision-camera-code-scanner frameprocessor) and NFC works normally without any workarounds/hacks. It looks fixed, good job!

@pasquale95
Copy link

pasquale95 commented Nov 7, 2023

I still experience this problem with V3.
As soon as the Camera is mounted, the NFC reader stops working. Setting isActive does not solve the problem.
Any hint for solutions?

Few useful info:

  • the problem does not happen if I use RNVC V2;
  • the problem appears as soon as I render the <Camera> component;
  • setting isActive to false or forcing to unmount the Camera does not solve the issue.

@mrousavy
Copy link
Owner

mrousavy commented Nov 9, 2023

the problem does not happen if I use RNVC V2;

Are you sure? The original poster said this problem happens on 2.9.x.

I believe this is just a Camera2 problem?

@mrousavy
Copy link
Owner

Hey! I just found out that I really forgot to close and dispose the locked Camera resources, so I just fixed that in this PR: #2174
Now the Camera fully & synchronously closes all resources (CameraSession, CameraDevice, OpenGL context, Video & Photo outputs, Photo Synchronizer) once the view gets removed from the React view hierarchy ("unmounted"), and things like Flash, Torch, NFC, and other Camera components should be working again.

There is still a small issue that causes once Camera component to turn into a blackscreen when navigating back and forth between two Camera components, that's a pretty rare edge case but I will still try to fix that soon when I have some free time.

If you appreciate my time, expertise and dedication to this project, pleas 💖 consider sponsoring me on GitHub 💖 to support the development of this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
None yet
8 participants