美文网首页
(Android)蓝牙一对多设备连接实践

(Android)蓝牙一对多设备连接实践

作者: 西厌西厌 | 来源:发表于2019-05-06 10:32 被阅读0次

该文档只说明一对多,蓝牙其余操作略过

需求

教练带七个学员训练,每个学员身上都有一套设备,该设备需要app通过ble控制

步骤

  1. 创建以下map,用来存储必要的信息
    private Map<String, BluetoothGatt> mBluetoothGattMap = new ConcurrentHashMap<>(); //临时保存 BluetoothGatt
    private Map<String, BluetoothGattCharacteristic> mGattCharacteristicMap = new ConcurrentHashMap<>();// 临时保存蓝牙的特征值 Characteristic
    private Map<String, BluetoothGattCharacteristic> mGattCharacteristicNotifyMap = new ConcurrentHashMap<>();// 临时保存蓝牙的特征值 Characteristic

  1. 当蓝牙收到连接成功回调时(onConnectionStateChange)
    private void onConnectStateSuccess(BluetoothGatt gatt){
        BluetoothDevice device = gatt.getDevice();
        mBluetoothGattMap.put(device.getAddress(), gatt);//把 BluetoothGatt 以 key-value 的形式临时保存起来
        gatt.discoverServices();
    }
  1. 当蓝牙收到连接失败回调时(onConnectionStateChange)
    private void onConnectStateFailure(BluetoothGatt gatt){
        String address = gatt.getDevice().getAddress();
        mBluetoothGattMap.remove(address);
    }
  1. 当服务被找到时(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);
                }
            }

        }
    }
  1. 断开蓝牙
    //取消蓝牙配对
    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();

        }
    }
    
    //通过地址返回当前蓝牙
    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;
    }

  1. 写数据
    //往设备里写数据
    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;
    }

相关文章

网友评论

      本文标题:(Android)蓝牙一对多设备连接实践

      本文链接:https://www.haomeiwen.com/subject/wqtowqtx.html