1. 程式人生 > >【筆記】藍芽BLE開發記錄

【筆記】藍芽BLE開發記錄

一直沒有養成隨時記錄的習慣,這裡記錄一些藍芽BLE開發使用中遇到的問題。

BluetoothGatt status 133

這個錯誤狀態發生在連線時,每次連線數超上限的時候就會出現,一般在使用中用bluetoothGatt.close()釋放當前Gatt連線資源。

開啟通知時回撥onDescriptorWrite returns status 128

開發中需要根據裝置的屬性值bluetoothGattCharacteristic.getProperties()是判斷是支援通知(ENABLE_NOTIFICATION_VALUE)還是支援指示(ENABLE_INDICATION_VALUE)來設定描述符

    private BluetoothGatt mBluetoothGatt;
    BluetoothGattCharacteristic characteristic;
    boolean enabled;
    ...
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
    ...
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
            UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
    //根據特徵屬性設定
    if((characteristic.getProperties()&BluetoothGattCharacteristic.PROPERTY_NOTIFY)==0) {
        //支援指示
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    }else{
        //支援通知
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    }
    mBluetoothGatt.writeDescriptor(descriptor);

獲取服務後,獲取特徵值列表為空

找不到解決方法,重新掃描連接獲取服務再去獲取特徵值就又有了