Webview and QR Scanner through a button over webview Android c# Visual Studio 2019

  android, barcode-scanner, c#, qr-code, webview

I have an android webview app in VS2019. I am trying to call a JS function which in turn invokes a JS Interface and opens the QR scanner. What i am unable to do it reach JS interface code through remote URL…

Here is my code…

MainActivity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Webkit;
using Vintasoft.XamarinBarcode;
using ZXing.Mobile;
using Java.Lang;
using System.Threading.Tasks;
using Java.Interop;
using Android.Content;

namespace XFAS_Android
{
    [Activity(Label = "XFAS Delivery", Icon = "@drawable/icon", MainLauncher = true)]
    public class MainActivity : Activity
    {
        WebView _webview;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource

            MobileBarcodeScanner.Initialize(Application);
            SetContentView(Resource.Layout.activity_main);

            _webview = FindViewById<WebView>(Resource.Id.webview);
            _webview.SetWebViewClient(new WebViewClient());
            _webview.Settings.JavaScriptEnabled = true;
            _webview.Settings.AllowFileAccessFromFileURLs = true;
            _webview.Settings.AllowUniversalAccessFromFileURLs = true;
            _webview.Settings.AllowFileAccess = true;
            _webview.Settings.AllowContentAccess = true;
            _webview.Settings.DomStorageEnabled = true;

            _webview.AddJavascriptInterface(new QRScannerJSInterface(_webview), "QRScannerJSInterface");

            _webview.LoadUrl("https://m.priorityconnections.in");

            Button btnScan = FindViewById<Button>(Resource.Id.btnScan);
            btnScan.Click += Button_Click;
        }

        private void Button_Click(object sender, System.EventArgs e)
        {

        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

        public class QRScannerJSInterface : Java.Lang.Object
        {
            QRScanner qrScanner;
            WebView webView;

            public QRScannerJSInterface(WebView webView)
            {
                this.webView = webView;
                qrScanner = new QRScanner();
            }

            [Export("ScanQR")]
            public void ScanQR()
            {
                qrScanner.ScanQR()
                    .ContinueWith((t) =>
                    {
                    if (t.Status == TaskStatus.RanToCompletion)
                            webView.LoadUrl(@"javascript:getQRValue('" + t.Result + "')");
                    });
            }
        }
    }
}

QRScanner.cs

namespace XFAS_Android
{
    class QRScanner
    {
        MobileBarcodeScanner scanner;

        public QRScanner()
        {
            scanner = new MobileBarcodeScanner();
        }

        public async Task<string> ScanQR()
        {
            scanner.UseCustomOverlay = false;
            scanner.TopText = "Scanning for barcode";
            var result = await scanner.Scan();
            return result.ToString();
        }
    }
}

Remote URL Scan button code…

function scan() {
            try
            {
                QRScannerJSInterface.ScanQR();
            }
            catch(err) {
                ShowRedMsgLarge('Error', err.message);
            }
        };

When invoking the QRScannerJSInterface.ScanQR i always get an alert saying function not defined.
Need help.

Code is inspired from … Launching a QR scanner from Android web view and returning the scanned result using Xamarin

what could i be doing wrong here?

Source: Android Questions

LEAVE A COMMENT