woodemi / quick_blue

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

[Android] - setNotifiable throw a java.lang.NullPointerException #59

Closed benji-bou closed 2 years ago

benji-bou commented 2 years ago

Hello,

Thank you for this package! When using on Android, the QuickBlue.setNotifiable function , it throws a java error:

E/MethodChannel#quick_blue/method( 4902): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothGattDescriptor.setValue(byte[])' on a null object reference
E/MethodChannel#quick_blue/method( 4902):   at com.example.quick_blue.QuickBluePluginKt.setNotifiable(QuickBluePlugin.kt:296)
E/MethodChannel#quick_blue/method( 4902):   at com.example.quick_blue.QuickBluePlugin.onMethodCall(QuickBluePlugin.kt:111)
E/MethodChannel#quick_blue/method( 4902):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/MethodChannel#quick_blue/method( 4902):   at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:296)
E/MethodChannel#quick_blue/method( 4902):   at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$DartMessenger(DartMessenger.java:320)
E/MethodChannel#quick_blue/method( 4902):   at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$TsixYUB5E6FpKhMtCSQVHKE89gQ.run(Unknown Source:12)
E/MethodChannel#quick_blue/method( 4902):   at android.os.Handler.handleCallback(Handler.java:938)
E/MethodChannel#quick_blue/method( 4902):   at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#quick_blue/method( 4902):   at android.os.Looper.loop(Looper.java:223)
E/MethodChannel#quick_blue/method( 4902):   at android.app.ActivityThread.main(ActivityThread.java:7664)
E/MethodChannel#quick_blue/method( 4902):   at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#quick_blue/method( 4902):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/MethodChannel#quick_blue/method( 4902):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

The responsible code is:

  QuickBlue.setConnectionHandler(
        (String deviceId, BlueConnectionState state) {
      if (state == BlueConnectionState.connected) {
        QuickBlue.setValueHandler((deviceId, characteristicId, value) {
          ctrl.sink.add(value);
        });

        //TODO: Waiting for [PR-52](https://github.com/woodemi/quick_blue/pull/52)
        QuickBlue.setServiceHandler(
            (deviceId, serviceId /*, characteristicsId*/) {
          if (serviceId ==
                  TestQuickBlue.SERVICE
                      .toLowerCase() /* &&
          characteristicsId ==
              DeviceBloc.NOTIFY_CHARACTERISTICS.toLowerCase()*/
              ) {
            //CRASH
            QuickBlue.setNotifiable(
                deviceId,
                TestQuickBlue.SERVICE.toLowerCase(),
                TestQuickBlue.NOTIFY_CHARACTERISTICS.toLowerCase(),
                BleInputProperty.notification);
          }
        });
        QuickBlue.discoverServices(deviceId);
      }
    });
    QuickBlue.connect(deviceId);

Do you have an idea of the problem? Best

Sunbreak commented 2 years ago

Could post the logcat of below?

    override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
      Log.v(TAG, "onServicesDiscovered ${gatt.device.address} $status")
      if (status != BluetoothGatt.GATT_SUCCESS) return

      gatt.services?.forEach { service ->
        Log.v(TAG, "Service " + service.uuid)
        service.characteristics.forEach { characteristic ->
          Log.v(TAG, "    Characteristic ${characteristic.uuid}")
          characteristic.descriptors.forEach {
            Log.v(TAG, "        Descriptor ${it.uuid}")
          }
        }
      }

      sendMessage(messageConnector, mapOf(
        "deviceId" to gatt.device.address,
        "ServiceState" to "discovered",
        "services" to gatt.services.map { it.uuid.toString() }
      ))
    }
Sunbreak commented 2 years ago

Probably the TestQuickBlue.NOTIFY_CHARACTERISTICS doesn't have a private val DESC__CLIENT_CHAR_CONFIGURATION = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")

benji-bou commented 2 years ago

Thx for the response. It was indeed a mis spelling CharacteristicID