Mar*_*och 8 android bluetooth-lowenergy android-bluetooth gatt android-ble
我有Android应用程序暴露BLE服务器.我连接BluetoothGattServer#connect.它有效 - 我的应用程序通过STATE_CONNECTED调用BluetoothGattServerCallback#onConnectionStateChange.当我完成客户端后,我尝试使用BluetoothGattServer#cancelConnection与我的应用程序断开连接.
但我没有接到对BluetoothGattServerCallback#onConnectionStateChange的调用,似乎连接仍然处于活动状态,因为我的BLE客户端没有开始做广告(当没有任何连接时它就会这样做).
在logcat中我只看到:
BluetoothGattServer: cancelConnection() - device: XX:XX:XX:XX:XX:XX
Run Code Online (Sandbox Code Playgroud)
有趣的是,我的应用程序在完全关闭BT后立即调用BluetoothGattServerCallback#onConnectionStateChange和STATE_DISCONNECTED.
小智 8
当 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);
}
}
Run Code Online (Sandbox Code Playgroud)
调用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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1251 次 |
| 最近记录: |