woodemi / quick_blue

A cross-platform (Android/iOS/macOS/Windows/Linux) BluetoothLE plugin for Flutter
135 stars 70 forks source link

[Android] - setNotifiable throw PlatformException(error, Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothGattDescriptor.setValue(byte[])' #146

Open riorizki opened 1 year ago

riorizki commented 1 year ago

Hi,

i got ble device with 2 characteristic in 1 service each characteristic got different properties (1 for notify and 1 for write)

when i set the setNotifable to notification like code below i got error

QuickBlue.setNotifiable(
  ble.deviceId,
  service,
  '0000ffe1-0000-1000-8000-00805f9b34fb',
  BleInputProperty.notification,
);
E/MethodChannel#quick_blue/method(26826): Failed to handle method call
E/MethodChannel#quick_blue/method(26826): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothGattDescriptor.setValue(byte[])' on a null object reference
E/MethodChannel#quick_blue/method(26826):   at com.example.quick_blue.QuickBluePluginKt.setNotifiable(QuickBluePlugin.kt:296)
E/MethodChannel#quick_blue/method(26826):   at com.example.quick_blue.QuickBluePlugin.onMethodCall(QuickBluePlugin.kt:111)
E/MethodChannel#quick_blue/method(26826):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/MethodChannel#quick_blue/method(26826):   at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#quick_blue/method(26826):   at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/MethodChannel#quick_blue/method(26826):   at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#quick_blue/method(26826):   at android.os.Handler.handleCallback(Handler.java:883)
E/MethodChannel#quick_blue/method(26826):   at android.os.Handler.dispatchMessage(Handler.java:100)
E/MethodChannel#quick_blue/method(26826):   at android.os.Looper.loop(Looper.java:214)
E/MethodChannel#quick_blue/method(26826):   at android.app.ActivityThread.main(ActivityThread.java:7356)

E-flutter (26826): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothGattDescriptor.setValue(byte[])' on a null object reference, null, java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothGattDescriptor.setValue(byte[])' on a null object reference
E/flutter (26826):  at com.example.quick_blue.QuickBluePluginKt.setNotifiable(QuickBluePlugin.kt:296)
E/flutter (26826):  at com.example.quick_blue.QuickBluePlugin.onMethodCall(QuickBluePlugin.kt:111)
E/flutter (26826):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/flutter (26826):  at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/flutter (26826):  at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/flutter (26826):  at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/flutter (26826):  at android.os.Handler.handleCallback(Handler.java:883)
E/flutter (26826):  at android.os.Handler.dispatchMessage(Handler.java:100)
E/flutter (26826):  at android.os.Looper.loop(Looper.java:214)
E/flutter (26826):  at android.app.ActivityThread.main(ActivityThread.java:7356)
E/flutter (26826):  at java.lang.reflect.Method.invoke(Native Method)
E/flutter (26826):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
E/flutter (26826):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
E/flutter (26826): )
E/flutter (26826): #0      StandardMethodCodec.decodeEnvelope
message_codecs.dart:652
E/flutter (26826): #1      MethodChannel._invokeMethod
platform_channel.dart:310
E/flutter (26826): <asynchonous suspension>
E/flutter (26826):

this is the logcat of discover service

D/BluetoothGatt(25138): discoverServices() - device: 20:22:08:25:10:0C
D/BluetoothGatt(25138): onSearchComplete() = Device=20:22:08:25:10:0C Status=0
V/QuickBluePlugin(25138): onServicesDiscovered 20:22:08:25:10:0C 0
2 V/QuickBluePlugin(25138): Service 0000ffe0-0000-1000-8000-00805f9b34fb
V/QuickBluePlugin(25138):     Characteristic 0000ffe1-0000-1000-8000-00805f9b34fb
V/QuickBluePlugin(25138):         Descriptor 00002902-0000-1000-8000-00805f9b34fb

How can i setNotifiable so i can receive to value from ble ?

Thank you

SahilSharma2710 commented 1 year ago

hey @riorizki i have counter question QuickBlue.setNotifiable(deviceId, serviceId, characteristicId, true); , how we determine the values of serviceId & characteristics ?? I struggling with same problem i guess