Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Cordova plugin for using torch or flashlight of your device

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.75k
    Comment on it

    Hello Readers,

    Cordova flashlight plugin allows you to access device flashlight in your app and you can access it from your app.

    This plugin works on iOS 5+, Android 2+, wp8 and for Android 6+ runtime permission handled automatically.

    First of all we need to install the plugin through CLI:

    Plugin from npm:

    $ cordova plugin add cordova-plugin-flashlight

     

    Plugin from github:

    $ cordova plugin add https://github.com/EddyVerbruggen/Flashlight-PhoneGap-Plugin

     

    For phoneGap build: This plugin also works on phoneGap build. To use the latest version of this plugin we need to add following lines in config.xml .

    <gap:plugin name="cordova-plugin-flashlight" source="npm" />

    After installation now, we will see the usage:

     

    This plugin creates window.plugins.flashlight object. For example:

    window.plugins.flashlight.available(function(isAvailable) {
      if (isAvailable) {
        // switch on
        window.plugins.flashlight.switchOn();
        // switch off after 3 seconds
        setTimeout(function() {
          window.plugins.flashlight.switchOff();
        }, 3000);
      } else {
        alert("Flashlight not available on this device");
      }
    });

    In the above code, if isAvailable is true then flashlight or torch will be on. window.plugins.flashlight.toggle() function is an alternative for switchOn() and switchOff() function.

     

    For Example:

    window.plugins.flashlight.available(function(isAvailable) {
           if (isAvailable) {
               window.plugins.flashlight.toggle();
           } 
           else 
           {
              alert("Flashlight not available on this device");
           }
    });

     

    To know if flashlight is On or Off we can call isSwitchedOn() function which returns true or false value.

    window.plugins.flashlight.isSwitchedOn();

     

    IMPORTANT:  For Android apps we will make sure that the torch is switched off when app is exited via backbutton.  Otherwise the camera can't be used by other apps.

    document.addEventListener("backbutton", function() {
      // pass exitApp as callbacks to the switchOff method
      window.plugins.flashlight.switchOff(exitApp, exitApp);
    }, false);
    
    function exitApp() {
      navigator.app.exitApp();
    }

     

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: