Skip to content

Commit 500b1d8

Browse files
committed
setNotifyValue now properly waits for descriptor to finish writing.
Before this fix, setNotifyValue was immediately returning and not waiting for the CCCD descriptor to be properly written to. If you had an app that needed to set notifications on multiple characteristics, it would error out as a "error when writing the descriptor", see issue #70.
1 parent 0de5698 commit 500b1d8

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

android/src/main/java/com/pauldemarco/flutterblue/FlutterBluePlugin.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class FlutterBluePlugin implements MethodCallHandler, RequestPermissionsR
5858
private static final String TAG = "FlutterBluePlugin";
5959
private static final String NAMESPACE = "plugins.pauldemarco.com/flutter_blue";
6060
private static final int REQUEST_COARSE_LOCATION_PERMISSIONS = 1452;
61-
static final private UUID CCCD_ID = UUID.fromString("000002902-0000-1000-8000-00805f9b34fb");
61+
static final private UUID CCCD_ID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
6262
private final Registrar registrar;
6363
private final MethodChannel channel;
6464
private final EventChannel stateChannel;
@@ -453,6 +453,10 @@ public void onMethodCall(MethodCall call, Result result) {
453453
value = BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE;
454454
}
455455

456+
if(!gattServer.setCharacteristicNotification(characteristic, request.getEnable())){
457+
result.error("set_notification_error", "could not set characteristic notifications to :" + request.getEnable(), null);
458+
return;
459+
}
456460

457461
if(!cccDescriptor.setValue(value)) {
458462
result.error("set_notification_error", "error when setting the descriptor value to: " + value, null);
@@ -464,18 +468,7 @@ public void onMethodCall(MethodCall call, Result result) {
464468
return;
465469
}
466470

467-
if(!gattServer.setCharacteristicNotification(characteristic, request.getEnable())){
468-
result.error("set_notification_error", "could not set characteristic notifications to :" + request.getEnable(), null);
469-
return;
470-
}
471-
472471
result.success(null);
473-
474-
// SetNotificationResponse
475-
Protos.SetNotificationResponse.Builder q = Protos.SetNotificationResponse.newBuilder();
476-
q.setRemoteId(gattServer.getDevice().getAddress());
477-
q.setCharacteristic(ProtoMaker.from(characteristic, gattServer));
478-
channel.invokeMethod("SetNotificationResponse", q.build().toByteArray());
479472
break;
480473
}
481474

@@ -803,7 +796,7 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
803796

804797
@Override
805798
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
806-
Log.d(TAG, "onDescriptorRead: ");
799+
Log.d(TAG, "onDescriptorRead: " + descriptor.getUuid().toString() + " status: " + status);
807800
if(descriptorReadSink != null) {
808801
// Rebuild the ReadAttributeRequest and send back along with response
809802
Protos.ReadDescriptorRequest.Builder q = Protos.ReadDescriptorRequest.newBuilder();
@@ -829,11 +822,12 @@ public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descrip
829822
p.setValue(ByteString.copyFrom(descriptor.getValue()));
830823
descriptorReadSink.success(p.build().toByteArray());
831824
}
825+
832826
}
833827

834828
@Override
835829
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
836-
Log.d(TAG, "onDescriptorWrite: ");
830+
Log.d(TAG, "onDescriptorWrite: " + descriptor.getUuid().toString() + " status: " + status);
837831
Protos.WriteDescriptorRequest.Builder request = Protos.WriteDescriptorRequest.newBuilder();
838832
request.setRemoteId(gatt.getDevice().getAddress());
839833
request.setDescriptorUuid(descriptor.getUuid().toString());
@@ -843,6 +837,14 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri
843837
p.setRequest(request);
844838
p.setSuccess(status == BluetoothGatt.GATT_SUCCESS);
845839
channel.invokeMethod("WriteDescriptorResponse", p.build().toByteArray());
840+
841+
if(descriptor.getUuid().compareTo(CCCD_ID) == 0) {
842+
// SetNotificationResponse
843+
Protos.SetNotificationResponse.Builder q = Protos.SetNotificationResponse.newBuilder();
844+
q.setRemoteId(gatt.getDevice().getAddress());
845+
q.setCharacteristic(ProtoMaker.from(descriptor.getCharacteristic(), gatt));
846+
channel.invokeMethod("SetNotificationResponse", q.build().toByteArray());
847+
}
846848
}
847849

848850
@Override

0 commit comments

Comments
 (0)