BarCodeScanner only scans on first mount Android

Please provide the following:

  1. SDK Version: 43.0.0
  2. Platforms: Android/iOS
  3. Add the appropriate “Tag” based on what Expo library you have a question on: BarCodeScanner - Expo Documentation

Hi,

I have an issue with the BarCodeScanner on Android. On first use of the Bar Code Scanner, the scanner works as expected and reads qr codes. Once I navigate way from the BarCodeScanner, then try to navigate back, the camera still works. However, the qr codes are no longer detected (callback onBarCodeScanned is not being invoked). The issue only appears on Android devices iOS works as expect.

I followed the documentations advice to ensure that the BarCodeScanner component is unmount when I navigate away from the BarCodeScanner screen.

My use Effect method, and snippet of the return method. I added the mounted.current in vain to try and solve the issue

  /* Demounting camera to save batter power
   * and to allow us to use the camera in other components,
   * two instances can't be mounted at once
   */
  useEffect(() => {
    mounted.current = true;

    return () => {
      setHasPermission(false)  
      mounted.current = false;
    };
}, [isFocused]);

.....
return (
    <SafeAreaView>
{hasPermission && (
          <BarCodeScanner
            accessibilityLabel={t('register.cameraAccessibility')}
            accessibilityHint={t('register.cameraAccessibilityHint')}
            type={BarCodeScanner.Constants.Type.back}
            barCodeTypes={[BarCodeScanner.Constants.BarCodeType.qr]}
            onBarCodeScanned={handleBarCodeScanned}
            style={{
              height: '100%',
              width: '100%'
            }}
          />
      )}
 </SafeAreaView>

Using a Pixel 4
Android Version 12.
SDK 43 (started after SDK upgrade)

Relieved to see this. I have the exact same problem but on Android only. I only get one scan. Hot reload doesn’t fix it either. I have to restart Expo Go and then it resets and I can get one barcode read only. Same application on iOS does NOT have this issue. iOS permissions in app.json:

  "NSCameraUsageDescription": "This app uses the camera to scan medication barcodes.",
        "NSRemindersUsageDescription": "This app uses the calendar to schedule medication dosages.",
        "NSLocationWhenInUseUsageDescription": "This app records the geographic location where the dose scan took place."

and for android:

 "permissions": [
        "CAMERA",
        "VIBRATE",
        "LOCATION"
      ],

To reproduce on agnostic code, try the Expo Barcode snack on Android. BarCodeScanner - Expo Documentation Since it doesn’t navigate away from the single screen you can do multiple scans. Then close that snack and reopen it in Expo Go (without closing Expo Go). You cannot scan anymore. Then close Expo Go and reload it and then the snack. You’ll get to scan again (until you close the snack).

1 Like

hey folks! thanks for the information. can you open an issue about this on Issues · expo/expo · GitHub? we’ll investigate shortly if so

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.