2016-05-25 20 views
0

現在、私はcordova.plugins.diagnosticを使用して、Bluetoothが現在オンかオフかをチェックしています。ブルートゥースがオフの場合、ユーザにそれをオンにするよう要求するプロンプトが表示され、「継続ボタン」が無効になります。それが既にオンになっている場合、どうすればそれが既にオンであるかを検出し、継続ボタンを有効にすることができます。Ionic - ブルートゥースの状態変更を確認する方法

以下

はBluetoothが有効/無効で検出する方法コードである:

cordova.plugins.diagnostic.isBluetoothEnabled(function(enabled){ 
    console.log("Bluetooth is " + (enabled ? "enabled" : "disabled")); 
}, function(error){ 
    console.error("The following error occurred: "+error); 
}); 

そして、これはBluetoothの状態に対して行われた変更を確認する方法コードである:

$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; 
    } 
}); 

})

ただし、オフからオンまたはオフからテストすると、アラートが表示されません。ハンドラがコールバックしていないようです。

答えて

1
cordova.plugins.diagnostic.isBluetoothEnabled(function(enabled){ 
    if (enabled) { 
     // bluetooth already on 
    } else { 
     // bluetooth off 
    } 
}, function(error){ 
    console.error("The following error occurred: "+error); 
}); 

cordova.plugins.diagnostic.setBluetoothState(function(){ 
    console.log("Bluetooth was enabled"); 
}, function(error){ 
    console.error("The following error occurred: "+error); 
}, true); 

HTML化粧

<button class="button" ng-disabled="!bluetoothIsEnabled" on-tap="yourFunction($event)"></button> 

bluetoothの状態

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

感謝を聞いて、あなたは "ボタンを続行" イベントで 'cordova.plugins.diagnostic.setBluetoothState'を置くことによって意味ですか?だから私はそのボタンを作ることができますブルートゥースがオンの後に有効になりますか? –

+0

「ボタン無効」=「ボタン無効」=「!bluetoothIsEnabled」オンタップ=「yourFunction($イベント)」>「 –

+0

はい、私はそれを理解していますが、 bluetoothIsEnabledがfalseの場合は有効になります。なぜなら、私のシナリオからは、ブルートゥースが消える前に言及しているからです。ユーザがブルートゥースを有効にした後、この 'cordova.plugins.diagnostic.isBluetoothEnabled'はコールバックではなく、値' bluetoothIsEnabled'をtrueに変更します。 –

関連する問題