(Android)藍(lán)牙一對(duì)多設(shè)備連接實(shí)踐

該文檔只說(shuō)明一對(duì)多,藍(lán)牙其余操作略過(guò)

需求

教練帶七個(gè)學(xué)員訓(xùn)練,每個(gè)學(xué)員身上都有一套設(shè)備,該設(shè)備需要app通過(guò)ble控制

步驟

  1. 創(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

  1. 當(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();
    }
  1. 當(dāng)藍(lán)牙收到連接失敗回調(diào)時(shí)(onConnectionStateChange)
    private void onConnectStateFailure(BluetoothGatt gatt){
        String address = gatt.getDevice().getAddress();
        mBluetoothGattMap.remove(address);
    }
  1. 當(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);
                }
            }

        }
    }
  1. 斷開(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;
    }

  1. 寫數(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;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容