question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android - first scan never works, works only after permissions are given.

See original GitHub issue

I’m running into this exact same issue as this on 2.4.1.

Here’s my scanning page

public class BarcodeScanningPage : ContentPage
    {
        ZXingScannerView ZXingView { get; set; }
        ZXingDefaultOverlay Overlay { get; set; }

        public BarcodeScanningPage() : base()
        {
            Title = "Scanning...";

            ZXingView = new ZXingScannerView
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                AutomationId = "zxingScannerView",
                Options = new ZXing.Mobile.MobileBarcodeScanningOptions()
                {
                    PossibleFormats = new List<ZXing.BarcodeFormat>() {
                        ZXing.BarcodeFormat.CODE_39, ZXing.BarcodeFormat.QR_CODE
                    }
                }
            };
            ZXingView.OnScanResult += (result) => Device.BeginInvokeOnMainThread(async () =>
            {
                // Stop analysis until we navigate away so we don't keep reading barcodes
                ZXingView.IsAnalyzing = false;

                await NavigationHelper.PushAsync(new SearchResultsPage(new ApiModels.SearchItemsParams()
                {
                    Keyword = result.Text,
                    ResultsPerPage = App.AppConfig.SearchResultsPerPage
                }, result.Text, true));

                //await NavigationHelper.PopAsync();
            });

            Overlay = new ZXingDefaultOverlay()
            {
                BottomText = "Scan Barcodes or QR Codes",
                ShowFlashButton = ZXingView.HasTorch,
                AutomationId = "zxingDefaultOverlay",
            };
            Overlay.FlashButtonClicked += (sender, e) =>
            {
                ZXingView.IsTorchOn = !ZXingView.IsTorchOn;
            };
            var grid = new Grid()
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
            };
            grid.Children.Add(ZXingView);
            grid.Children.Add(Overlay);

            // The root page of your application
            Content = grid;
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            ZXingView.IsScanning = true;
        }

        protected override void OnDisappearing()
        {
            ZXingView.IsScanning = false;

            base.OnDisappearing();
        }
}

Any ideas how I might get around this? Thanks! Great plugin.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Axemastacommented, Jul 20, 2018

Hi guys, this isn’t a fix but instead a work around.

If you use James Montemagno’s permission library to check for permissions before you present your scanner, the android device will scan on first time load.

2reactions
zlannincommented, Oct 25, 2018

I’m not familiar with Forms but Android needed the following code in my Activity. I’m running 2.4.1.

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Android 10 not working with BLE Bluetooth scanning
To make BLE scanning work on Android apps targeting Android 10 you need to ask the user for. ACCESS_BACKGROUND_LOCATION.
Read more >
Bluetooth permissions
Quickly bring your app to life with less code, using a modern declarative approach to UI, and the simplicity of Kotlin. ... Start...
Read more >
Android permissions - BLE - Android 12
When I try to run the app, I get a runtime error: Need android.permission.BLUETOOTH_SCAN permission for android.content.
Read more >
Change app permissions on your Android phone
Some of these steps work only on Android 11 and up. ... Allow only while using the app: The app can use the...
Read more >
Making Android BLE work — part 1
Scanning only works if you have the following permission AND you have Location Service activated. <uses-permission android:name="android.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found