該文檔只說(shuō)明一對(duì)多,藍(lán)牙其余操作略過(guò)
需求
教練帶七個(gè)學(xué)員訓(xùn)練,每個(gè)學(xué)員身上都有一套設(shè)備,該設(shè)備需要app通過(guò)ble控制
步驟
- 創(chuàng)建以下map,用來(lái)存儲(chǔ)必要的信息
private Map<String, BluetoothGatt> mBluetoothGattMap = new ConcurrentHashMap<>(); //臨時(shí)保存 BluetoothGatt
private Map<String, BluetoothGattCharacteristic> mGattCharacteristicMap = new ConcurrentHashMap<>();// 臨時(shí)保存藍(lán)牙的特征值 Characteristic
private Map<String, BluetoothGattCharacteristic> mGattCharacteristicNotifyMap = new ConcurrentHashMap<>();// 臨時(shí)保存藍(lán)牙的特征值 Characteristic
- 當(dāng)藍(lán)牙收到連接成功回調(diào)時(shí)(onConnectionStateChange)
private void onConnectStateSuccess(BluetoothGatt gatt){
BluetoothDevice device = gatt.getDevice();
mBluetoothGattMap.put(device.getAddress(), gatt);//把 BluetoothGatt 以 key-value 的形式臨時(shí)保存起來(lái)
gatt.discoverServices();
}
- 當(dāng)藍(lán)牙收到連接失敗回調(diào)時(shí)(onConnectionStateChange)
private void onConnectStateFailure(BluetoothGatt gatt){
String address = gatt.getDevice().getAddress();
mBluetoothGattMap.remove(address);
}
- 當(dāng)服務(wù)被找到時(shí)(onServicesDiscovered)
private void onServiceDiscoveredSuccess() {
for (Map.Entry<String, BluetoothGatt> s : mBluetoothGattMap.entrySet()) {
BluetoothGatt curGatt = s.getValue();
BluetoothGattService bluetoothGattService = curGatt.getService(Constants.DeviceUUID.uuid);
if (bluetoothGattService != null) {
BluetoothGattCharacteristic bluetoothGattCharacteristic = bluetoothGattService.getCharacteristic(Constants.DeviceUUID.UUID_READ_WRITE);
BluetoothGattCharacteristic mCharacteristicNotify = bluetoothGattService.getCharacteristic(Constants.DeviceUUID.UUID_NOTIFY);
mGattCharacteristicMap.put(s.getKey(), bluetoothGattCharacteristic);
mGattCharacteristicNotifyMap.put(s.getKey(), mCharacteristicNotify);
BluetoothGattCharacteristic characteristic = mGattCharacteristicNotifyMap.get(s.getKey());
curGatt.setCharacteristicNotification(characteristic, true);
List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
for (BluetoothGattDescriptor dp : descriptors) {
dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
curGatt.writeDescriptor(dp);
}
}
}
}
- 斷開(kāi)藍(lán)牙
//取消藍(lán)牙配對(duì)
public void disconnectBle(String bluetoothdeviceAddress) {
if (mBluetoothAdapter == null || TextUtils.isEmpty(bluetoothdeviceAddress)) {
return;
}
BluetoothGatt bluetoothGatt = getBleGattByAddress(bluetoothdeviceAddress);
if (bluetoothGatt != null && getAvailableBle() != null) {
bluetoothGatt.disconnect();
mBluetoothGattMap.remove(bluetoothdeviceAddress);
bluetoothGatt.close();
}
}
//通過(guò)地址返回當(dāng)前藍(lán)牙
public BluetoothGatt getBleGattByAddress(String address) {
for (Map.Entry<String, BluetoothGatt> s : mBluetoothGattMap.entrySet()) {
BluetoothGatt curGatt = s.getValue();
String key = s.getKey();
if (address.equals(key)) {
return curGatt;
}
}
return null;
}
- 寫數(shù)據(jù)
//往設(shè)備里寫數(shù)據(jù)
public boolean writeCharacteristic(final byte[] data, boolean needResponse) {
if (mBluetoothAdapter == null || mBluetoothGattMap.size() == 0 || data == null) {
return false;
}
boolean flag = false;
for (Map.Entry<String, BluetoothGatt> s : mBluetoothGattMap.entrySet()) {
final BluetoothGatt curGatt = s.getValue();
final BluetoothGattCharacteristic characteristic = mGattCharacteristicMap.get(s.getKey());
if (characteristic == null) {
return false;
}
characteristic.setValue(data);
if (!needResponse) {
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
} else {
curGatt.setCharacteristicNotification(characteristic, true);
curGatt.readCharacteristic(characteristic);
}
flag = curGatt.writeCharacteristic(characteristic);
}
return flag;
}