我正在使用以下反应库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
:(
更新 - 这是启动通知和写入值的代码段 - 完整文件可在此处找到 - 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 + " )");
}
答案 0 :(得分:2)
这是一个无资源错误。你确定你写了描述符吗?如果您没有设置描述符(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?
}