小编典典
我想知道如何实现
要实现多个BLE连接,您必须存储多个BluetoothGatt对象并将这些对象用于不同的设备。要存储多个连接对象,BluetoothGatt可以使用Map<>
private Map connectedDeviceMap;
在服务上onCreate初始化Map
connectedDeviceMap = new HashMap();
然后,在呼叫device.connectGatt(this, false, mGattCallbacks);连接到 GATT Server
之前,请检查该设备是否已经连接。
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
if(connectionState == BluetoothProfile.STATE_DISCONNECTED ){
// connect your device
device.connectGatt(this, false, mGattCallbacks);
}else if( connectionState == BluetoothProfile.STATE_CONNECTED ){
// already connected . send Broadcast if needed
}
上BluetoothGattCallback,如果连接状态 CONNECTED
然后存储B