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(發(fā)現(xiàn)新設(shè)備)和ACTION_BOND_STATE_CHANGED(設(shè)備更改其綁定狀態(tài)),然后我檢查新狀態(tài)是否為BOND_BOUNDED以及它是否與設(shè)備連接.
現(xiàn)在當我調(diào)用createBond方法(在問題中描述)并輸入引腳時,ACTION_BOND_STATE_CHANGED將觸發(fā),而device.getBondState()== BluetoothDevice.BOND_BONDED將為True并且它將連接.
總結(jié)
以上是生活随笔為你收集整理的android蓝牙开启后会尝试自动连接,以编程方式配对后,Android会自动连接蓝牙设备...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xgboost算法 c语言,xgboos
- 下一篇: android profile分析器,A