Peter Sandström Peter Sandström - 5 months ago 28
Javascript Question

Cordova brightness plugin with ionic framwork

I'm using https://github.com/mgcrea/cordova-plugin-brightness

I have figured out the setBrightness to work in an Ionic app.
But I can't figure out the getBrightness to work. Any pointer on how got get it to work are appreciated.

This is my setBrightness:

$scope.changeBrightness = function (newBrightness) {
myBrightness = parseFloat(newBrightness)/1000;
if (window.cordova && window.cordova.plugins.brightness) {
var LightControl = cordova.plugins.brightness;
LightControl.setBrightness(myBrightness);
}
}


LightControl.getBrightness(); But then? How do I handle the success or fail?

Answer

getBrightness() should be dealt with success and error callback functions. Success callback will return the value of brightness set. This value will be a float number ranging from 0 to 1. It returns -1 in case of system default brightness.

Checkout this simple sample code which i tried in vanilla cordova project:

index.html

<!DOCTYPE html>
<html>
    <head>        
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Brightness Control</title>
    </head>
    <body>      
        <br>        
        <br>
        Set Brightness <input type="button" value="setbright" name="Wifi" id="setbright"/>   <br>
        Get Brightness <input type="button" value="getbright" name="Wifi" id="getbright"/>   <br>
        <script type="text/javascript" src="js/jquery.js"></script> 
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/app.js"></script>
    </body>
</html>

app.js

$(document).ready(function() {
    document.addEventListener("deviceready", onDeviceReady, false);
});

function onDeviceReady() {      

     $('#setbright').click( function() 
        {   
            try {               
                cordova.plugins.brightness.setBrightness(0.9, setsuccess, seterror);
            }
            catch(err) {
                alert("Plugin Error - " + err.message);
            }

        }); 

     $('#getbright').click( function() 
        {   
            try {               
                cordova.plugins.brightness.getBrightness(getsuccess, geterror);
            }
            catch(err) {
                alert("Plugin Error - " + err.message);
            }

        }); 

    function setsuccess(e) {        
        alert("Brightness set successfully");
    }

    function getsuccess(e) {                
        alert("Brightness value - " + e);
    }

    function seterror(e) {
        alert("Error setting brightness");
    }

    function geterror(e) {
        alert("Error getting brightness");
    }
}
Comments