Cordova barcode scanner on Android - cancel scan a

2019-07-12 02:28发布

问题:

We have a Cordova app, intended for Android devices, which uses the bar code scanner plugin. The app itself as an overwrite to the default Android back button upon device ready:

document.addEventListener("backbutton", onBackKeyDown, false);

The issue is that when the user cancels the scan by pressing the back button, The camera closes and the app display the webview, and then launches the "backbutton" event (i.e. invoking the onBackKeyDown function). As if the back button was pressed on the webview itself and not on the scan activity. we have tried some alternatives, for example - before starting the scan, remove the event listener:

function startScan() {

    document.removeEventListener("backbutton", onBackKeyDown, false);   
    cordova.plugins.barcodeScanner.scan(    
....

but it didn't help. We cannot override it on the plugin itself, since it's not an activity. Meaning we must do it on the JavaScript.

Any solution is mostly appreciated.

回答1:

Are you still on this? I'm having issues with the back button myself... I use the barcode scanner plugin for phonegap too, and have an event listener for the back button.

What I ended up doing is adding a flag that I set to true on every scan and then whenever calling the onBackKeyDown function - if it is true then reset to back false without executing the rest of the function...

var in_barcode_scan = false;
function onBackKeyDown() {
    if (in_barcode_scan) {
        in_barcode_scan = false;
    } else {
        //do whatever you need when a legit back button is triggered.
    }
}
function startScan() {
    in_barcode_scan = true;
    cordova.plugins.barcodeScanner.scan(.....
}

I'm now having other issues myself (cancelling the barcode scanner kills some other event listeners), but this should probably do the trick for you...

Jospeh.



回答2:

This works for me, I added an messaje that the scanner was cancelled only with an alert validating the field "result.text]" like this:

function fileViewSuccess(result) {
console.log("We got a barcode Result: " + result.text);
if(result.cancelled == true){
alert("Was cancelled");
}else{
///do something...
}
}


回答3:

If someone is still facing this (with android 9 and barcode scanner) I managed to by pass it by registering a "backbutton" event listener when a cancel is received, and release the event listener after a second. My handler for the "backbutton" dosen't do anything, it just returns false, so the navigation doesn't happens. This listener is working on android only, which is good for this problem.

So just call the following "blockBack" function, when getting a "cancel" from the barcode plugin.

function blockBack(){

// stop back button (for 1 s)
// used by barcode camera (when canceling and returnin back)
// was sending the back event to the router, and left the screen

document.addEventListener("backbutton", onBackKeyDown, false);

setTimeout(function(){
  document.removeEventListener("backbutton", onBackKeyDown, false)
}, 1000)

function onBackKeyDown() {
   // swallow the back button - do nothing
   return false;
}

}