Skip to content

Commit

Permalink
Add "DescriptorWriteNotAllowed" error to Android implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cierpliwy committed Sep 4, 2019
1 parent 8120be1 commit 3778307
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
11 changes: 9 additions & 2 deletions android/src/main/java/com/polidea/reactnativeble/BleModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,13 @@ private void safeWriteDescriptorForDevice(final Descriptor descriptor,
final String valueBase64,
final String transactionId,
final SafePromise promise) {
BluetoothGattDescriptor nativeDescriptor = descriptor.getNativeDescriptor();

if (nativeDescriptor.getUuid().equals(Characteristic.CLIENT_CHARACTERISTIC_CONFIG_UUID)) {
BleErrorUtils.descriptorWriteNotAllowed(UUIDConverter.fromUUID(nativeDescriptor.getUuid())).reject(promise);
return;
}

final RxBleConnection connection = getConnectionOrReject(descriptor.getCharacteristic().getService().getDevice(), promise);
if (connection == null) {
return;
Expand All @@ -1562,13 +1569,13 @@ private void safeWriteDescriptorForDevice(final Descriptor descriptor,
try {
value = Base64Converter.decode(valueBase64);
} catch (Throwable e) {
String uuid = UUIDConverter.fromUUID(descriptor.getNativeDescriptor().getUuid());
String uuid = UUIDConverter.fromUUID(nativeDescriptor.getUuid());
BleErrorUtils.invalidWriteDataForDescriptor(valueBase64, uuid).reject(promise);
return;
}

final Subscription subscription = connection
.writeDescriptor(descriptor.getNativeDescriptor(), value)
.writeDescriptor(nativeDescriptor, value)
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum BleErrorCode {
DescriptorNotFound(503),
DescriptorsNotDiscovered(504),
DescriptorInvalidDataFormat(505),
DescriptorWriteNotAllowed(506),

ScanStartFailed(600),
LocationServicesDisabled(601);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ static public BleError invalidWriteDataForDescriptor(String data, String uuid) {
return bleError;
}

static public BleError descriptorWriteNotAllowed(String uuid) {
BleError bleError = new BleError(BleErrorCode.DescriptorWriteNotAllowed, null, null);
bleError.descriptorUUID = uuid;
return bleError;
}

static public BleError serviceNotFound(String uuid) {
BleError bleError = new BleError(BleErrorCode.ServiceNotFound, null, null);
bleError.serviceUUID = uuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ private BleError toError(int code, String message, BleGattOperationType operatio
bleError.serviceUUID = serviceUUID;
bleError.characteristicUUID = characteristicUUID;
bleError.descriptorUUID = descriptorUUID;
return bleError;
} else if (BleGattOperationType.READ_RSSI == operationType) {
BleError bleError = new BleError(BleErrorCode.DeviceRSSIReadFailed, message, code);
bleError.deviceID = deviceID;
Expand Down
3 changes: 2 additions & 1 deletion src/BleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ export const BleErrorCodeMessage: BleErrorCodeMessageMapping = {
'Descriptors not discovered for device {deviceID}, service {serviceUUID} and characteristic {characteristicUUID}',
[BleErrorCode.DescriptorInvalidDataFormat]:
'Cannot write to descriptor {descriptorUUID} with invalid data format: {internalMessage}',
[BleErrorCode.DescriptorWriteNotAllowed]: "Cannot write to descriptor {descriptorUUID}. It's not allowed by iOS.",
[BleErrorCode.DescriptorWriteNotAllowed]:
"Cannot write to descriptor {descriptorUUID}. It's not allowed by iOS and therefore forbidden on Android as well.",

// Scanning
[BleErrorCode.ScanStartFailed]: 'Cannot start scanning operation',
Expand Down

0 comments on commit 3778307

Please sign in to comment.