capacitor-community / barcode-scanner

A fast and efficient (QR) barcode scanner for Capacitor
MIT License
437 stars 169 forks source link

Barcode Scanner isn't working on Android #209

Closed diepoe closed 1 year ago

diepoe commented 1 year ago

Before describing my problem, I just wanna thank you for building such a nice product! It's working very performant on iOS and purely timesaving for my current school project! Thank you for your work!

Describe the bug Although the plugin is working fine on the iOS build of my app, the android version of my wouldn't show any sign of the plugin. This starts with the fact, that the app isn't asking the user to grant camera access, which results in a black screen at the qr code scanning view (I'm implementing this whole thing in Svelte btw).

To Reproduce Repository with the code: https://github.com/diepoe/heg-game

Steps to reproduce the behavior:

In the build app on android:

  1. Go to 'Im Gebäude'
  2. Click on 'Kamerazugriff erlauben' (App isn't responding on android) [corresponding source code: src/routes/real/01-start/permissions/+page.svelte]
  3. Click two times on the arrows at the bottom to get to the Scanning view
  4. Click on the start scan button (nothing will happen) [corresponding source code: src/routes/real/02-game/a-scanner/+page.svelte]

Expected behavior At step two, the android app should ask the user for granting camera access, on step 4 a proper qr code scan with the device camera should start

Screenshots

Version ^3.0.1

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context It's working fine on iOS, so I should be missing something on the android implementation I guess (although I already configured AndroidManifest.xml like documented)...

Thank you for any answer in advance :-)

thegnuu commented 1 year ago

@diepoe you are currently not requesting the permission ;)

BarcodeScanner.checkPermission({ force: true }); is missing in your requestCameraPermission() function. When adding this, it works on my Pixel 6 :) I guess iOS is requesting the permission itself once access to the camera API is requested, but on Android you have to do this manually...

function requestCameraPermission() {
    BarcodeScanner.prepare();
    BarcodeScanner.checkPermission({ force: true });
}
thegnuu commented 1 year ago

Ah, there is one more issue, you should request permission before you call prepare and await the response, otherwise, the prepare call will be kinda useless...

diepoe commented 1 year ago

Works now, thank you very much! 🙏