【发布时间】:2016-05-25 01:50:45
【问题描述】:
目前我正在使用cordova.plugins.diagnostic 检查蓝牙当前是打开还是关闭。如果蓝牙关闭,则会提示用户将其打开并禁用“继续按钮”。在它已经打开之后,我如何检测它已经打开并启用继续按钮。
以下是如何检测蓝牙启用/禁用的代码:
cordova.plugins.diagnostic.isBluetoothEnabled(function(enabled){
console.log("Bluetooth is " + (enabled ? "enabled" : "disabled"));
}, function(error){
console.error("The following error occurred: "+error);
});
然后,这是如何检查蓝牙状态更改的代码:
$ionicPlatform.ready(function() {
cordova.plugins.diagnostic.registerBluetoothStateChangeHandler(function(state ){
if(state === cordova.plugins.diagnostic.bluetoothState.POWERED_ON){
alert("Bluetooth is able to connect");
$scope.bluetoothIsEnabled = true;
}
else if(state === cordova.plugins.diagnostic.bluetoothState.POWERED_OFF){
alert("Bluetooth is Off");
$scope.bluetoothIsEnabled = false;
}
});
})
但是,如果我从 Off 到 On 或 On 到 Off 进行测试,则不会出现任何警报。似乎处理程序没有回调。
【问题讨论】:
标签: cordova bluetooth ionic-framework