cordova - Ionic - How To Check Bluetooth State Change -
currently i'm using cordova.plugins.diagnostic check whether bluetooth on or off. if bluetooth off prompt asking user turn on , 'continue button' disabled. after on, how can detect on , make continue button enable.
below code how detect bluetooth enable/disable:
cordova.plugins.diagnostic.isbluetoothenabled(function(enabled){ console.log("bluetooth " + (enabled ? "enabled" : "disabled")); }, function(error){ console.error("the following error occurred: "+error); });
then, code how check changes made bluetooth state:
$ionicplatform.ready(function() { cordova.plugins.diagnostic.registerbluetoothstatechangehandler(function(state ){ if(state === cordova.plugins.diagnostic.bluetoothstate.powered_on){ alert("bluetooth able connect"); $scope.bluetoothisenabled = true; } else if(state === cordova.plugins.diagnostic.bluetoothstate.powered_off){ alert("bluetooth off"); $scope.bluetoothisenabled = false; } });
})
but, if test off on or on off, there no alert appear. seems handler not call back.
cordova.plugins.diagnostic.isbluetoothenabled(function(enabled){ if (enabled) { // bluetooth on } else { // bluetooth off } }, function(error){ console.error("the following error occurred: "+error); }); cordova.plugins.diagnostic.setbluetoothstate(function(){ console.log("bluetooth enabled"); }, function(error){ console.error("the following error occurred: "+error); }, true);
html makeup
<button class="button" ng-disabled="!bluetoothisenabled" on-tap="yourfunction($event)"></button>
bluetooth state listen
cordova.plugins.diagnostic.registerbluetoothstatechangehandler(function(state){ // "unknown", "resetting", "unsupported", "unauthorized", "powered_off", "powered_on" if (state == "powered_on") { $scope.bluetoothisenabled = true; } else { $scope.bluetoothisenabled = false; } });
Comments
Post a Comment