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

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

需求

教练带七个学员训练,每个学员身上都有一套设备,该设备需要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;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容