[英]Ble-getting error code - 128 while writing to characterstic
I am using following react library react-native-ble-manager 我正在使用以下反应库react-native-ble-manager
I am trying to perform read and write operations on BLE device.I am successfully able to perform read operation. 我正在尝试在BLE设备上执行读写操作。我能够成功执行读取操作。 But I am getting error code 128 while writing to BLE device. 但是我在写入BLE设备时收到错误代码128。
first, I am enabling notification for characteristic - 首先,我启用特征通知-
BleManager.startNotification(peripheralId, serviceId, characteristicId)
Writing is like this - 写作是这样的-
converting 'hex' value to base64 string - 将'hex'值转换为base64字符串-
const base64String = new Buffer('0x00B00050D0', 'hex').toString('base64');
BleManager.write(peripheralId, serviceId, characteristicId, base64Value)
Write operation return error code -128 写操作返回错误代码-128
:( :(
UPDATE -- This is code snippet to start notification and write value- complete file can be found here- BluetoothLeService.java 更新-这是代码片段开始通知和写值-完整的文件可以发现这里- 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 + " )");
}
This is a no resources error. 这是没有资源的错误。 Are you sure you wrote the descriptor? 您确定写了描述符吗? If you don't set the descriptor (BluetoothGattDescriptor) and just call write, you'll get this error. 如果您不设置描述符(BluetoothGattDescriptor)而是仅调用write,则会收到此错误。
Here is an example: 这是一个例子:
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?
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.