We need a Flashlight toggle switches when barcode scanner on



  • Hi all,

    I am developing a Monaca app which uses a barcode scanner plugin to scan. I used https://github.com/phonegap/phonegap-plugin-barcodescanner for implementing barcode scanner and it works fine.

    I implemented the flashlight plugin with https://github.com/EddyVerbruggen/Flashlight-PhoneGap-Plugin.

    The functionality that I need to implement is, the flashlight of the mobile should automatically switch on when the scanning starts and should automatically switch off once the scan is complete.

    I tried the following code:

    <img class="mrg-bt" src="img/qr-scanner.png" onclick="scan();">
    
    function scan(){
      scanlight(true); //switch on flashlight
            window.plugins.barcodeScanner.scan(function(result){
            //scan success code
             scanlight(false);//switch off flashlight
       });
     }
    //flashlight function call
    function scanlight(enable) {
    window.plugins.flashlight.available(function (isAvailable) {
       if (isAvailable) {
          if (enable) {
             window.plugins.flashlight.switchOn(
                function () { },
                 function () { },  
                 { intensity: 0.3}   
             );
          } else {
             window.plugins.flashlight.switchOff();
          }
       }
    });
    

    }
    The issue is, the functionality is happening quite opposite to my requirement. When I hit the scan button the flashlight gets on and after the camera opens the flashlight goes off and the flashlight gets on once the camera is closed.

    Someone please help me to get rid of this. Thanks in advance.



  • @Amirtha I believe this is due to the camera wanting the flashlight for its flash functionality. I did find this on SO though: http://stackoverflow.com/questions/31839030/barcodescanner-cordova-with-led-light