2016-06-13 177 views
3

我使用https://github.com/mgcrea/cordova-plugin-brightness與離子framwork科爾多瓦亮度插件

我已經想通了setBrightness在離子應用工作。 但我無法弄清getBrightness的工作。任何指示如何得到它的工作表示讚賞。

這是我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();但是之後?我如何處理成功或失敗?

回答

5

getBrightness()應該處理成功和錯誤回調函數。成功回調將返回亮度設置的值。該值將是一個範圍從0到1的浮點數。在系統默認亮度的情況下,該值返回-1。

結帳我在香草科爾多瓦項目中嘗試這個簡單的示例代碼:

的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"); 
    } 
} 
+0

Thanks Gandhi!在Ionic下面發佈我的解決方案。 –

+0

@彼得有同樣的彼得。如果它有幫助,你也可以upvote。 – Gandhi

2

謝謝!將其轉換爲我的Ionic項目並使set函數也處理回調。

page.html中

<div class="item range range-light"> 
    <span class="smallA"><i class="ion-ios-sunny-outline"></i></span> 
    <input id="range" type="range" min="0" max="1000" ng-model="brightness" ng-change="setBrightness(brightness)"> 
    <span class="bigA"><i class="ion-ios-sunny"></i></span> 
</div> 

controller.js

和設置的功能

$scope.setBrightness = function (newBrightness) { 
    myBrightness = parseFloat(newBrightness)/1000; 
    if (window.cordova && window.cordova.plugins.brightness) { 
      var LightControl = cordova.plugins.brightness; 
      try { 
      LightControl.setBrightness(myBrightness, setsuccess, seterror); 
     } 
     catch(err) { 
      console.log("setBrightness", err); 
     } 
     function seterror(e) { 
     console.log("seterror", e); 
     } 

     function setsuccess(e) { 
     console.log("setsuccess", e); 
      var brightness = Math.round(e*1000); 
      $scope.brightness = brightness; 
     } 
    } 
} 

而get函數

可能會將此移至app.js,因爲我可能會在其他控制器中需要它。

$scope.$on('$ionicView.enter', function(){ 
if (window.cordova && window.cordova.plugins.brightness) { 
    var LightControl = cordova.plugins.brightness; 
    try { 
     LightControl.getBrightness(getsuccess, geterror); 
    } 
    catch(err) { 
     console.log("getBrightness", err); 
    } 

    function geterror(e) { 
     console.log("geterror", e); 
    } 

    function getsuccess(e) { 
    //alert("Brightness value - " + e); 
    var brightness = Math.round(e*1000); 
    //alert("Brightness value - " + brightness); 
     $scope.brightness = brightness; 
     } 
    } 
});