chipweinberger / flutter_blue_plus

Flutter plugin for connecting and communicationg with Bluetooth Low Energy devices, on Android, iOS, macOS
Other
787 stars 475 forks source link

[Help]: PlatformException(iosException, Client Characteristic Configuration descriptors must be configured using setNotifyValue:forCharacteristic:, in flutter_blue_plus #1025

Closed EINS-SD-MOHAMMED closed 1 month ago

EINS-SD-MOHAMMED commented 1 month ago

Requirements

Have you checked this problem on the example app?

No

FlutterBluePlus Version

1.33.5

Flutter Version

3.24.3

What OS?

iOS

OS Version

18.0.1

Bluetooth Module

Nedap Nvite Reader

What is your problem?

I am trying to connect to the Nedap Nvite reader using the Flutter Blue Plus package in my mobile app. The connection process works correctly on Android, allowing me to communicate with the device and enable notifications without issues.

However, on iOS, after successfully connecting to the Nedap Nvite reader and retrieving the characteristic properties, I encounter an error when attempting to enable notifications. Specifically, after calling setNotifyValue(true), when I try to write to the descriptor, I receive the following error:

PlatformException(iosException, Client Characteristic Configuration descriptors must be configured using setNotifyValue:forCharacteristic:, in flutter_blue_plus)

This error indicates that the notification configuration is not being handled correctly on iOS, while the same steps work fine on Android.

I have also gone through the closed issues related to this problem but have not been able to resolve the issue.

For a better understanding of the issue, I will be sharing the logs from both Android and iOS. Any guidance on resolving this iOS-specific issue would be greatly appreciated!

Logs

iOS Logs
flutter: 080000000000000123
flutter: 4b76a404f0cdd23c93ac540cc89148e7
[FBP-iOS] handleMethodCall: startScan
flutter: BluetoothDevice{remoteId: 3FDD4B45-0565-AF43-DE53-6BA68C1203E8, platformName: EINS, services: null}
flutter: EINS found! rssi: -78
[FBP-iOS] handleMethodCall: stopScan
[FBP-iOS] handleMethodCall: connect
[FBP-iOS] didConnectPeripheral
[FBP-iOS] handleMethodCall: discoverServices
[FBP-iOS] didDiscoverServices:
[FBP-iOS]   svc: 180a
[FBP-iOS]   svc: 87b1de8d-e7cb-4ea8-a8e4-290209522c83
[FBP-iOS] didDiscoverCharacteristicsForService:
[FBP-iOS]   svc: 180a
[FBP-iOS]     chr: 2a25
[FBP-iOS]     chr: 2a27
[FBP-iOS]     chr: 2a26
[FBP-iOS]     chr: 2a28
[FBP-iOS]     chr: 2a29
[FBP-iOS]     chr: 2a24
[FBP-iOS] didDiscoverCharacteristicsForService:
[FBP-iOS]   svc: 87b1de8d-e7cb-4ea8-a8e4-290209522c83
[FBP-iOS]     chr: e68a5c09-aef8-4447-8f10-f3339898dee9
[FBP-iOS]     chr: 54080bd6-d573-11e5-ab30-625662870761
[FBP-iOS]     chr: 540810c2-d573-11e5-ab30-625662870761
[FBP-iOS] didDiscoverDescriptorsForCharacteristic:
[FBP-iOS]   chr: 2a25
[FBP-iOS] didDiscoverDescriptorsForCharacteristic:
[FBP-iOS]   chr: 2a27
[FBP-iOS] didDiscoverDescriptorsForCharacteristic:
[FBP-iOS]   chr: 2a26
[FBP-iOS] didDiscoverDescriptorsForCharacteristic:
[FBP-iOS]   chr: 2a28
[FBP-iOS] didDiscoverDescriptorsForCharacteristic:
[FBP-iOS]   chr: 2a29
[FBP-iOS] didDiscoverDescriptorsForCharacteristic:
[FBP-iOS]   chr: 2a24
[FBP-iOS] didDiscoverDescriptorsForCharacteristic:
[FBP-iOS]   chr: e68a5c09-aef8-4447-8f10-f3339898dee9
[FBP-iOS] didDiscoverDescriptorsForCharacteristic:
[FBP-iOS]   chr: 54080bd6-d573-11e5-ab30-625662870761
[FBP-iOS] didDiscoverDescriptorsForCharacteristic:
[FBP-iOS]   chr: 540810c2-d573-11e5-ab30-625662870761
[FBP-iOS]     desc: 2902
[FBP-iOS] handleMethodCall: setNotifyValue
[FBP-iOS] didUpdateNotificationStateForCharacteristic:
[FBP-iOS]   chr: 540810c2-d573-11e5-ab30-625662870761
flutter: The status of setNotifyValue true
flutter: MACE_CHALLENGE characteristic properties: CharacteristicProperties{broadcast: false, read: true, writeWithoutResponse: false, write: false, notify: true, indicate: false, authenticatedSignedWrites: false, extendedProperties: false, notifyEncryptionRequired: false, indicateEncryptionRequired: false}
flutter: Just before the descriptor write
[FBP-iOS] handleMethodCall: writeDescriptor
*** Assertion failure in -[CBPeripheral writeValue:forDescriptor:], CBPeripheral.m:590
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(iosException, Client Characteristic Configuration descriptors must be configured using setNotifyValue:forCharacteristic:, {stackTrace: 0   CoreFoundation                      0x0000000188dc1098 4250D45C-51A9-3AC5-8C3D-69DE936FAD46 + 540824
1   libobjc.A.dylib                     0x00000001860c32e4 objc_exception_throw + 88
2   Foundation                          0x000000018819215c 6D058A2D-05CF-3A29-8BEB-B1030AE97282 + 8528220
3   CoreBluetooth                       0x00000001b10b52f8 686BB4AF-A663-38E6-9590-B2CD01CE1CA3 + 307960
4   flutter_blue_plus                   0x0000000102c798e8 -[FlutterBluePlusPlugin handleMethodCall:result:] + 21588
5   Flutter                             0x0000000106797210 __45-[FlutterMethodChannel setMethodCallHandler:]_block_invoke + 164
6   Flutter                             0x0000000106218d88 ___ZN7flutter25PlatformMessageHandlerIos21HandlePlatformMessageENSt3_fl10unique_ptrINS_15PlatformMessageENS1_14default_deleteIS3_EEEE_block_invoke + 116
7   libdispatch.dylib                   0x0000000103574a30 _dispatch_call_block_and_release + 32
8   libdispatch.dylib                   0x000000010357671c _dispatch_client_callout + 20
9   libdispatch.dylib                   0x0000000103586de8 _dispatch_main_queue_drain + 1076
10  libdispatch.dylib                   0x00000001035869a4 _dispatch_main_queue_callback_4CF + 44
11  CoreFoundation                      0x0000000188d93f64 4250D45C-51A9-3AC5-8C3D-69DE936FAD46 + 356196
12  CoreFoundation                      0x0000000188d91188 4250D45C-51A9-3AC5-8C3D-69DE936FAD46 + 344456
13  CoreFoundation                      0x0000000188d905b8 CFRunLoopRunSpecific + 572
14  GraphicsServices                    0x00000001d48261c4 GSEventRunModal + 164
15  UIKitCore                           0x000000018b8e62c0 187C890E-8E4C-32F2-A97E-FA905A55F094 + 4055744
16  UIKitCore                           0x000000018b994ddc UIApplicationMain + 340
17  UIKitCore                           0x000000018bcc1b20 187C890E-8E4C-32F2-A97E-FA905A55F094 + 8100640
18  Runner.debug.dylib                  0x0000000102c3860c $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 120
19  Runner.debug.dylib                  0x0000000102c38584 $s6Runner11AppDelegateC5$mainyyFZ + 44
20  Runner.debug.dylib                  0x0000000102c38688 __debug_main_executable_dylib_entry_point + 28
21  dyld                                0x00000001ae564d34 5DB83988-2EE6-3756-BD07-B8D67B1133A5 + 212276}, null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
<asynchronous suspension>
#2      FlutterBluePlus._invokeMethod (package:flutter_blue_plus/src/flutter_blue_plus.dart:614:13)
<asynchronous suspension>
#3      BluetoothDescriptor.write (package:flutter_blue_plus/src/bluetooth_descriptor.dart:163:7)
<asynchronous suspension>
#4      ScanScreenState.discoverServiceCall (package:nedap_blue_ui/scan_screen/scan_screen.dart:152:17)
<asynchronous suspension>
[FBP-iOS] didDisconnectPeripheral:
[FBP-iOS]   error: The specified device has disconnected from us.

Android Logs
D/[FBP-Android](15805): [FBP] onMethodCall: connect
D/[FBP-Android](15805): [FBP] onMethodCall: getAdapterState
D/[FBP-Android](15805): [FBP] onConnectionStateChange:connected
D/[FBP-Android](15805): [FBP]   status: SUCCESS
D/[FBP-Android](15805): [FBP] onMethodCall: discoverServices
D/[FBP-Android](15805): [FBP] onServicesDiscovered:
D/[FBP-Android](15805): [FBP]   count: 4
D/[FBP-Android](15805): [FBP]   status: 0GATT_SUCCESS
D/[FBP-Android](15805): [FBP] onMethodCall: setNotifyValue
D/[FBP-Android](15805): [FBP] onDescriptorWrite:
D/[FBP-Android](15805): [FBP]   chr: 2a05
D/[FBP-Android](15805): [FBP]   desc: 2902
D/[FBP-Android](15805): [FBP]   status: GATT_SUCCESS (0)
D/[FBP-Android](15805): [FBP] onMethodCall: setNotifyValue
D/[FBP-Android](15805): [FBP] onCharacteristicChanged:
D/[FBP-Android](15805): [FBP]   chr: 540810c2-d573-11e5-ab30-625662870761
D/[FBP-Android](15805): [FBP] onDescriptorWrite:
D/[FBP-Android](15805): [FBP]   chr: 540810c2-d573-11e5-ab30-625662870761
D/[FBP-Android](15805): [FBP]   desc: 2902
D/[FBP-Android](15805): [FBP]   status: GATT_SUCCESS (0)
I/flutter (15805): The status of setNotifyValue true
I/flutter (15805): MACE_CHALLENGE characteristic properties: CharacteristicProperties{broadcast: false, read: true, writeWithoutResponse: false, write: false, notify: true, indicate: false, authenticatedSignedWrites: false, extendedProperties: false, notifyEncryptionRequired: false, indicateEncryptionRequired: false}
I/flutter (15805): Just before the descriptor write
D/[FBP-Android](15805): [FBP] onMethodCall: writeDescriptor
D/[FBP-Android](15805): [FBP] onCharacteristicChanged:
D/[FBP-Android](15805): [FBP]   chr: 540810c2-d573-11e5-ab30-625662870761
D/[FBP-Android](15805): [FBP] onDescriptorWrite:
D/[FBP-Android](15805): [FBP]   chr: 540810c2-d573-11e5-ab30-625662870761
D/[FBP-Android](15805): [FBP]   desc: 2902
D/[FBP-Android](15805): [FBP]   status: GATT_SUCCESS (0)
I/flutter (15805): Notifications enabled for MACE_CHALLENGE characteristic.
D/[FBP-Android](15805): [FBP] onMethodCall: writeCharacteristic
D/[FBP-Android](15805): [FBP] onCharacteristicChanged:
D/[FBP-Android](15805): [FBP]   chr: 540810c2-d573-11e5-ab30-625662870761
D/[FBP-Android](15805): [FBP] onCharacteristicWrite:
D/[FBP-Android](15805): [FBP]   chr: e68a5c09-aef8-4447-8f10-f3339898dee9
D/[FBP-Android](15805): [FBP]   status: GATT_SUCCESS (0)
I/flutter (15805): inside
I/flutter (15805): 4b76a404f0cdd23c93ac540cc89148e7
I/flutter (15805): [87, 115, 207, 107, 71, 99, 191, 123]
I/flutter (15805): RNDA: e5250dde15c6d92e
I/flutter (15805): RNDB*: 73cf6b4763bf7b57
I/flutter (15805): RNDA + RNDB*: e5250dde15c6d92e73cf6b4763bf7b57
I/flutter (15805): 4b76a404f0cdd23c93ac540cc89148e7
I/flutter (15805): 95c294e5e565c949fcd55abf9f03976a
I/flutter (15805): Encrypted message: 95c294e5e565c949fcd55abf9f03976a
I/flutter (15805): 080000000000000123 is of chart
D/[FBP-Android](15805): [FBP] onMethodCall: writeCharacteristic
D/[FBP-Android](15805): [FBP] onCharacteristicChanged:
D/[FBP-Android](15805): [FBP]   chr: 540810c2-d573-11e5-ab30-625662870761
D/[FBP-Android](15805): [FBP] onCharacteristicWrite:
D/[FBP-Android](15805): [FBP]   chr: 54080bd6-d573-11e5-ab30-625662870761
D/[FBP-Android](15805): [FBP]   status: GATT_SUCCESS (0)
D/[FBP-Android](15805): [FBP] onConnectionStateChange:disconnected
D/[FBP-Android](15805): [FBP]   status: REMOTE_USER_TERMINATED_CONNECTION
chipweinberger commented 1 month ago

android and iOS have slightly different rules.

on Android you can use desc.write(..) for the CCCD, on iOS: you must you chr.setNotifyValue.

ideally we would prevent this on Android as well, so they both behave the same.

If you want, you can open a PR to prevent this on Android (with a flag to allow it). But otherwise, I will close this since I do not intend to fix it.

something like

desc.write(allowCccdWritesOnAndroid: true)

EINS-SD-MOHAMMED commented 1 month ago

It seems that on iOS, we're not receiving any data, even though the BLE device has sent it. We’re getting a List with a length of zero, which suggests that the notifications are not being properly received or processed. We have also used chr.setNotifyValue on the characteristics, but the issue persists.The same is working correctly on Android even after removing the desc.write(..) and just calling the chr.setNotifyValue