我有Android应用程序暴露BLE服务器。我与BluetoothGattServer#connect联系。它的工作原理 - 我的应用程序通过BluetoothGattServerCallback#onConnectionStateChange调用STATE_CONNECTED。当我完成客户端后,我尝试使用BluetoothGattServer#cancelConnection断开与我的应用程序的连接。
但是我没有接到BluetoothGattServerCallback#onConnectionStateChange的电话,似乎连接仍处于活动状态,因为我的BLE客户端没有开始做广告(当没有任何连接时它就会这样做)。
在logcat中我只看到:
BluetoothGattServer: cancelConnection() - device: XX:XX:XX:XX:XX:XX
有趣的是,一旦我完全关闭BT,我的应用程序就会调用BluetoothGattServerCallback#onConnectionStateChange和STATE_DISCONNECTED。
调用disconnect()方法时遇到相同的问题..我的BluetoothGattCallback中的onConnectionStateChange没有给出断开连接。
骑自行车蓝牙似乎是唯一可行的。
编辑:同样,在调用disconnect()和close()方法后,我依然按照以下代码连接:
public int getConnectedBLEDevices() {
int i = 0;
List<BluetoothDevice> devices = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
for(BluetoothDevice device : devices) {
if(device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
Logs.writeEvent(TAG+".getConnectedBLEDevices()", device.getAddress() + "\n"+ getStateAsString(mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT)));
i++;
}
}
return i;
}
当newState == BluetoothProfile.STATE_CONNECTED时,您必须调用BluetoothGattServer.connect();.
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
super.onConnectionStateChange(device, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED){
mDevice = device;
mBluetoothGattServer.connect(device, false);
}else {
mDevice = null;
}
}
private void cancelConnection(){
if (mDevice != null) {
mBluetoothGattServer.cancelConnection(mDevice);
}
}
请参阅https://issuetracker.google.com/issues/37127644
状态:无法修复(预期行为)您必须调用BluetoothGattServer.connect()将连接标记为已使用,然后将BluetoothGattServer.disconnect()标记为不再使用。然后在超时堆栈之后,如果没有其他人正在使用该连接,则可以决定断开与远程的连接。如果在建立连接后未调用BluetoothGattServer.connect(),则堆栈将保持连接,直到某个gatt客户端/服务器应用程序开始使用此连接。