草庐IT

android - Ble-getting error code - 128 while writing to characteristic 写入特征

coder 2023-12-04 原文

我正在使用以下 react 库 react-native-ble-manager

我正在尝试在 BLE 设备上执行读取和写入操作。我能够成功执行读取操作。但是我在写入 BLE 设备时收到错误代码 128。

首先,我启用特征通知 -

BleManager.startNotification(peripheralId, serviceId, characteristicId)

写作是这样的-
将 'hex' 值转换为 base64 字符串 -

  const base64String = new Buffer('0x00B00050D0', 'hex').toString('base64');

  BleManager.write(peripheralId, serviceId, characteristicId, base64Value)

写操作返回错误码-128

:(

更新—— 这是开始通知和写入值的代码 fragment - 完整的文件可以在这里找到- BluetoothLeService.java

public void writeCharacteristic(BleCharacteristic bleCharacteristic, String inputValue) {

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }

    if (!bleCharacteristic.isNotificationStarted()) {
        Log.w(TAG, "Notification not started please start notification");
        return;
    }

    BluetoothGattCharacteristic bluetoothGattCharacteristic = bleCharacteristic.getBluetoothGattCharacteristic();
    bluetoothGattCharacteristic.setValue(inputValue);
    mBluetoothGatt.writeCharacteristic(bluetoothGattCharacteristic);
}

public void setCharacteristicNotification(BleCharacteristic bleCharacteristic) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    boolean enable = !bleCharacteristic.isNotificationStarted();
    Log.d(TAG, "setCharacteristicNotification(device=" + mBluetoothDeviceAddress + ", UUID="
            + bleCharacteristic.getUUID().toString() + ", enable=" + enable + " )");
    BluetoothGattCharacteristic characteristic = mBluetoothGatt.getService(bleCharacteristic.getServiceUUID()).getCharacteristic(bleCharacteristic.getUUID());
    mBluetoothGatt.setCharacteristicNotification(characteristic, enable);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
    descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[]{0x00, 0x00});
    boolean result = mBluetoothGatt.writeDescriptor(descriptor);
    bleCharacteristic.setNotificationStarted(result);
    Log.d(TAG, "setCharacteristicNotification(device=" + mBluetoothDeviceAddress + ", UUID="
            + bleCharacteristic.getUUID().toString() + ", enabled=" + result + " )");
}

最佳答案

这是一个没有资源的错误。你确定你写了描述符吗?如果您不设置描述符 (BluetoothGattDescriptor) 而只是调用 write,则会出现此错误。

这是一个例子:

protected static final UUID CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");

public boolean setCharacteristicNotification(BluetoothDevice device, UUID serviceUuid, UUID characteristicUuid,
        boolean enable) {
    if (IS_DEBUG)
        Log.d(TAG, "setCharacteristicNotification(device=" + device.getName() + device.getAddress() + ", UUID="
                + characteristicUuid + ", enable=" + enable + " )");
    BluetoothGatt gatt = mGattInstances.get(device.getAddress()); //I just hold the gatt instances I got from connect in this HashMap
    BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid).getCharacteristic(characteristicUuid);
    gatt.setCharacteristicNotification(characteristic, enable);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
    descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] { 0x00, 0x00 });
    return gatt.writeDescriptor(descriptor); //descriptor write operation successfully started? 
}

Source

关于android - Ble-getting error code - 128 while writing to characteristic 写入特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41016834/

有关android - Ble-getting error code - 128 while writing to characteristic 写入特征的更多相关文章

随机推荐