android蓝牙开启后会尝试自动连接,以编程方式配对后,Android会自动连接蓝牙设备...
我找到了解決方案.
首先,我需要一個BroadcastReceiver,如:
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
// CONNECT
}
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Discover new device
}
}
};
然后我需要注冊接收器如下:
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
context.registerReceiver(myReceiver, intentFilter);
通過這種方式,接收器正在偵聽ACTION_FOUND(發現新設備)和ACTION_BOND_STATE_CHANGED(設備更改其綁定狀態),然后我檢查新狀態是否為BOND_BOUNDED以及它是否與設備連接.
現在當我調用createBond方法(在問題中描述)并輸入引腳時,ACTION_BOND_STATE_CHANGED將觸發,而device.getBondState()== BluetoothDevice.BOND_BONDED將為True并且它將連接.
總結
以上是生活随笔為你收集整理的android蓝牙开启后会尝试自动连接,以编程方式配对后,Android会自动连接蓝牙设备...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xgboost算法 c语言,xgboos
- 下一篇: android profile分析器,A