我正在做Android-Ble开发。我的问题是:当Ble设备断开连接时,程序将调用.startLeScan(回调)来查找Ble设备并重新连接。但有时,程序会尝试扫描,但找不到任何设备。我猜这是由于Ble协议栈混乱造成的。我说的对吗?如果Ble协议栈混乱了,那么会发生什么事情使Ble协议栈混乱呢?有人能给我点建议吗?谢谢。
-有扫描码:
private void startBLEScan() {
if (isBLEScanStarted) {
return;
}else {
isBLEScanStarted = true;
}
if (mBluetoothAdapter != null) {
boolean startResult = mBluetoothAdapter.startLeScan(mLeScanCallback);
System.out.println("BlueToothScan: startLeScan "+startResult);
startBLEScanTimeoutTimer(1000 * 60); //When scan time reach 60s , stop scan and rescan after 5s.
}
else {
System.out.println("BlueToothScan: adapter is null");
}
}-有gatt回调.OnConnectionStateChange:
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
mBluetoothGatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
if(mBluetoothGatt != null){
mBluetoothGatt.close();
mBluetoothGatt = null;
}
broadcastUpdate(intentAction);
}
}发布于 2015-08-27 22:16:55
我不知道这是不是同样的问题,因为这个问题是我更新到Android5.1之后才出现的。我可以连接和绑定设备到我的Android,但当涉及到重新连接时,扫描将找不到任何已经绑定的设备。
我的代码在Android5.0中运行得很好,蓝牙低能耗的唯一变化是扫描方法。现在我必须使用BluetoothScanner类,不再从BluetoothAdapter类调用startLeScan方法,因为它已被弃用。
问题可能来自数据包的长度,因为当设备绑定或未绑定时,通告名称会发生变化。另一个想法是,当涉及到连接已经绑定的设备时,谷歌会改变安全性,但通常情况下,这不会影响扫描发现。
https://stackoverflow.com/questions/32240195
复制相似问题