Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

distinguish between read and notification for future completion #1080

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hgrf
Copy link

@hgrf hgrf commented Dec 16, 2024

Hi, I have run into an issue with flutter_blue_plus when I enable notifications for a characteristic and also call read operations. You might say "there is no reason to use read operations if you are using notifications" and I would accept that answer.

However, I did some digging and saw that the completion criteria for the future in

var responseStream = FlutterBluePlus._methodStream.stream
does not distinguish between the result of a read operation or a notification. Now for Android, this distinction is pretty simple to introduce, c.f. this PR. For iOS / macOS unfortunately I do not know how to do it, maybe you have a suggestion?

There are two problems that might happen when not doing this distinction:

  • the read value might correspond to a notification value (harmless in all cases where the values are "stable", i.e. there is no funky distinction between read and notify logic on the other side)
  • a subsequent operation might fail with a BUSY error, because the actual read has not completed, but its future was completed by the notification

For some context, I ran into this problem when doing subsequent calls to setNotifyValue() and read(), here is the corresponding debug log:

I/flutter (13262): BLE: INFO: 2024-12-16 13:36:42.321029: reading characteristic 6d1a6389-697f-657e-0000-000000000000...
D/[FBP-Android](13262): [FBP] onMethodCall: readCharacteristic
D/[FBP-Android](13262): [FBP] onCharacteristicRead:
D/[FBP-Android](13262): [FBP]   chr: 6d1a6389-697f-657e-0000-000000000000
D/[FBP-Android](13262): [FBP]   status: GATT_SUCCESS (0)
I/flutter (13262): BLE: INFO: 2024-12-16 13:36:42.365419: reading characteristic 6d166384-6985-6544-0000-000000000000...
D/[FBP-Android](13262): [FBP] onMethodCall: readCharacteristic
D/[FBP-Android](13262): [FBP] onCharacteristicRead:
D/[FBP-Android](13262): [FBP]   chr: 6d166384-6985-6544-0000-000000000000
D/[FBP-Android](13262): [FBP]   status: GATT_SUCCESS (0)
I/flutter (13262): BLE: INFO: 2024-12-16 13:36:42.397289: activating notification for 6408736b-6169-637b-0000-000000000000...
D/[FBP-Android](13262): [FBP] onMethodCall: setNotifyValue
D/BluetoothGatt(13262): setCharacteristicNotification() - uuid: 6408736b-6169-637b-0000-000000000000 enable: true
D/[FBP-Android](13262): [FBP] onDescriptorWrite:
D/[FBP-Android](13262): [FBP]   chr: 6408736b-6169-637b-0000-000000000000
D/[FBP-Android](13262): [FBP]   desc: 2902
D/[FBP-Android](13262): [FBP]   status: GATT_SUCCESS (0)
I/flutter (13262): BLE: INFO: 2024-12-16 13:36:42.426312: reading characteristic 6408736b-6169-637b-0000-000000000000...
D/[FBP-Android](13262): [FBP] onMethodCall: readCharacteristic
D/[FBP-Android](13262): [FBP] onCharacteristicChanged:
D/[FBP-Android](13262): [FBP]   chr: 6408736b-6169-637b-0000-000000000000
I/flutter (13262): BLE: INFO: 2024-12-16 13:36:42.456721: activating notification for 72047478-0076-0000-0000-000000000000...
D/[FBP-Android](13262): [FBP] onMethodCall: setNotifyValue
D/BluetoothGatt(13262): setCharacteristicNotification() - uuid: 72047478-0076-0000-0000-000000000000 enable: true
W/BluetoothGatt(13262): writeDescriptor() - prior command is not finished, mClient= 5
I/flutter (13262): BLE: SEVERE: 2024-12-16 13:36:42.465226: Error activating notification for 72047478-0076-0000-0000-000000000000: gatt.writeDescriptor() returned 201 : ERROR_GATT_WRITE_REQUEST_BUSY
D/[FBP-Android](13262): [FBP] onCharacteristicRead:
D/[FBP-Android](13262): [FBP]   chr: 6408736b-6169-637b-0000-000000000000
D/[FBP-Android](13262): [FBP]   status: GATT_SUCCESS (0)
D/[FBP-Android](13262): [FBP] onCharacteristicChanged:
D/[FBP-Android](13262): [FBP]   chr: 6408736b-6169-637b-0000-000000000000
D/[FBP-Android](13262): [FBP] onCharacteristicChanged:
D/[FBP-Android](13262): [FBP]   chr: 72047478-0076-0000-0000-000000000000
D/[FBP-Android](13262): [FBP] onCharacteristicChanged:
D/[FBP-Android](13262): [FBP]   chr: 6408736b-6169-637b-0000-000000000000
D/[FBP-Android](13262): [FBP] onCharacteristicChanged:
D/[FBP-Android](13262): [FBP]   chr: 72047478-0076-0000-0000-000000000000
D/[FBP-Android](13262): [FBP] onCharacteristicChanged:
D/[FBP-Android](13262): [FBP]   chr: 6408736b-6169-637b-0000-000000000000

@chipweinberger
Copy link
Owner

chipweinberger commented Dec 16, 2024

This is intentional.

And you're right, it's because it's impossible to distinguish on iOS.

In theory, we could do what you are describing, but you're still going to hit problem on iOS. So you still need to fix it "properly" in your peripheral firmware code. i.e. design your API so that it does not matter.

@chipweinberger
Copy link
Owner

chipweinberger commented Dec 16, 2024

im perhaps open to this PR, but I don't like the names.

from_read_operation -> (string) receiveEvent

enum receiveEvent {
    unknown // used for iOS
    read
    notify
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants