Zxing.net.mobile: Qr code scaning wont work after giving permissions on android.

Created on 30 Apr 2018  路  21Comments  路  Source: Redth/ZXing.Net.Mobile

HI I am using ZXing.Net.Mobile.Forms.Android (2.4.1.0) library in my app.After installation the Qr code page asking for permissions allow and deny. If I give permission as allow it is scanning continuously but not returning any results . To get the results I have to go back and open the page again.

Can any one suggest me is there any way to scan the qr code after giving permissions.

Thanks.

Most helpful comment

Adding await Task.Delay(1000); after manually requesting camera permissions (all prior to starting the scanner) works around the problem. What's the true fix?

All 21 comments

I have exactly the same problem. It does not work the first time, but it works the second time. Anyone have ideas about this?

Adding await Task.Delay(1000); after manually requesting camera permissions (all prior to starting the scanner) works around the problem. What's the true fix?

This issue was fixed in version 2.3.1 and works fine.
But in latest version 2.4.1 it does not work again...

So, if you download v2.3.1 and not the latest 2.4.1 you will be ok. It works to me!

The problem is fixed in 2.3.2 as well. I reverted to 2.3.2 from 2.4.1 when I observed the behavior reported in this thread.

Maybe I'm saying something obvious to all the people subscribed to this issue, but I thought I was affected by this bug at first, later I discovered that in a small refactoring of my app, I had removed by mistake the critical override bit that is mentioned in the README:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
    global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult (requestCode, permissions, grantResults);           
}

After adding this back, camera permissions started to work again immediately after granting them by the user, so I cannot reproduce this bug anymore, with the last version, 2.4.1.

@knocte - I'm not sure I understood your post, so I'm clarifying my observation. The problem I observed with ZXing version 2.4.1 was not due to the override (since I have this in both my iOS and Android specific code for my Xamarin.Forms PCL solution). What I observed with version 2.4.1 was that the camera was enabled and scan would occur after permissions were granted, but the first scan would always fail. During the first scan following a clean install of my app, I needed to tap \

@TonyC5 thanks for the info! I will test that and report back.

@TonyC5 tested. I can say that with 2.4.1 I can't reproduce this bug, I can do a successful scan immediately after granting Android's camera permissions.

@knocte - are you developing an Android app or a PCL App (portable for Android and iOS)? I ask, because I am observing this bug when building a PCL App with Xamarin.Forms for both Android and iOS.

I'm developing a crossplatform (iOS, Android, ...) Xamarin.Forms app with a .NET Standard library.

@knocte - Thanks! I will retest and post results. Maybe a recent Xamarin.Forms package update fixed the problem?

@knocte - I retested and found that the ZXing version 2.4.1 problem (first scan fails after clean app install) does not work. More specifically, on Android 6.0 where the user must first confirm use of the camera, the first scan fails after user confirms camera permission. User must tap \

I will revert to ZXing version 2.3.2 since the problem does not happen for me with that version. If you have any other ideas that might explain why you don't see the 2.4.1 problem, please post here. Thanks!

Did your last test involve a Xamarin.Forms app or a Xamarin.Android "raw" app?

Xamarin.Forms PCL build for both Android and iOS.

I have the same problem, version 2.4.1, Xamarin Forms, Android.
This problem only seems to occurr when then app is installed from an APK download.
I dont see this problem if I run the app from the IDE (Start Debugging/Start Without Debugging)

Is there a fix ?

when then app is installed from an APK download

How about when installed from AppStore? I don't have this bug when installed from AppStore.

This is also happening for me on 2.4.1, I've reverted to 2.3.2 for now as well. The same bug exists when downloading from Google Play, start debugging on device, start without debugging on device.

Use 2.4.1 version. Suffered half a day to defeat this issue, but only found a workaround. However, my experience may be useful for someone.

First of all - add init command into your OnCreate method of your first activity (splash for example).
MobileBarcodeScanner.Initialize(Application);
Then, if you trying to enable your scanner in OnResume - it will not work. I have suggestion why it so, scanner just doesn't have enough time to start camera in the same time while activity (fragment) trying perform something under the hood. Nevertheless, if you put a little delay before scanner activation code - it should works. So, I put only one line in my OnResume method before StartScan method invoked:
await Task.Delay(500);

global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult (requestCode, permissions, grantResults);

above single line in OnRequestPermissionsResult of MainActivity.cs worked like a charm..

people might be using below faulty one...
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

Maybe I'm saying something obvious to all the people subscribed to this issue, but I thought I was affected by this bug at first, later I discovered that in a small refactoring of my app, I had removed by mistake the critical override bit that is mentioned in the README:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
    global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult (requestCode, permissions, grantResults);           
}

After adding this back, camera permissions started to work again immediately after granting them by the user, so I cannot reproduce this bug anymore, with the last version, 2.4.1.

This works..

This should be fixed in 3.0.0-beta's. Please try and open a new issue if you still have problems. Thanks.

Was this page helpful?
0 / 5 - 0 ratings