我在使用通过 BT LE(蓝牙低功耗)连接的多个设备时遇到问题。
处理步骤: 1.扫描所有蓝牙设备以获取地址。
BluetoothAdapter btAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
BluetoothLeScanner bluetoothLeScanner = mBtAdapter.getBluetoothLeScanner();
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
if (btAdapter .isEnabled()) {
bluetoothLeScanner.startScan(filters, settings, mScanCallback);
}
2.循环所有设备按步骤1获取。对于每个设备:
BluetoothDevice btDevice = bluetoothAdapter.getRemoteDevice(deviceAddress);
BluetoothGatt bluetoothGatt = mBluetoothDevice.connectGatt(context, false, btCallback);
在 btCallBack 的 onConnectionStateChange() 上,我调用 bluetoothGatt .discoverServices(); 当连接状态为 STATE_CONNECTED 时。
与设备进行数据交换后。我打电话
bluetoothGatt.disconnect();
Thread.sleep(500);
bluetoothGatt.close();
处理下一个设备
问题是当我调用 bluetoothGatt.disconnect() 和 bluetoothGatt.close() 但第一个蓝牙加密狗连接总是挂起并且蓝牙加密狗的蓝色 LED 永远不会闪烁,即使我杀死了应用程序。
工作完成后,只有最后一个蓝牙加密狗可以释放连接。
我的问题是为什么我调用 disconnect() 和 close() 但它不起作用?
build.gradle:minSdkVersion 21,targetSdkVersion 28
否则,此问题仅发生在某些类型的 Android 设备上,而不是所有设备上。例如,它适用于三星设备(Android 7),但不适用于其他设备。
任何帮助,将不胜感激。