react-native-ble-manager icon indicating copy to clipboard operation
react-native-ble-manager copied to clipboard

BleManagerDidUpdateValueForCharacteristic is not being triggered

Open Srikanth-Enuguru opened this issue 1 year ago • 17 comments

Describe the bug I'm trying to listen for the changes happening on a characteristic. To achieve that I added a listener on BleManagerDidUpdateValueForCharacteristic event. But this handler is not being called when the characteristic value is changed.

Code changes

await BleManager.connect(peripheral.id);
await sleep(900);
await BleManager.retrieveServices(peripheral.id);

BleManager.startNotification(
          peripheral.id,
          "00001530-1212-efde-1523-785feabcd123",
          "00001531-1212-efde-1523-785feabcd123"
        )
          .then(async res => {
            console.log('started Notification');
            bleManagerEmitter.addListener(
              "BleManagerDidUpdateValueForCharacteristic",
              handleUpdateValueForCharacteristic,
            );
          })
          .catch((error) => {
            // Failure code
            console.log(error);
          });

BleManager.write(
            peripheral.id,
          "00001530-1212-efde-1523-785feabcd123",
          "00001531-1212-efde-1523-785feabcd123",
            buffer.toJSON().data
          )
            .then(async(data) => {
              // Success code
              console.log("Write: " + data);

              BleManager.readDescriptor(
                peripheral.id,
              "00001530-1212-efde-1523-785feabcd123",
              "00001531-1212-efde-1523-785feabcd123",
              "2902"
              )
                .then((data) => {
                  // Success code
                  console.log("Read after write 1: " + data);
                })
                .catch((error) => {
                  // Failure code
                  console.log(error);
                });
            })
            .catch((error) => {
              // Failure code
              console.log(error);
            });

const handleUpdateValueForCharacteristic = (
    data: BleManagerDidUpdateValueForCharacteristicEvent,
  ) => {
    console.debug(
      `[handleUpdateValueForCharacteristic] received data from '${data.peripheral}' with characteristic='${data.characteristic}' and value='${data.value}'`,
    );
  };

Expected behavior BleManagerDidUpdateValueForCharacteristic should be triggered whenever the Characteristic value is changed.

Screenshots Screenshot 2023-08-01 at 1 49 32 PM

Smartphone (please complete the following information):

  • Device: Android 12
  • react-native-ble-manager version: 10.1.2
  • react-native version: 0.71.2

Please help me in fixing this issue.

Srikanth-Enuguru avatar Aug 01 '23 08:08 Srikanth-Enuguru

@marcosinigaglia Could you please help me.

Srikanth-Enuguru avatar Aug 01 '23 08:08 Srikanth-Enuguru

I get the same error and not receiving the updated values from the device. I am using Galaxy S21 with Android version 13 and API 33 in my case what I get is

  1. startNotification (BLE Manager)
  2. registerNotify (BLE Manager)
  3. setCharacteristicNotification() - uuid:####### enable: true (BluetoothGatt)
  4. Characteristic ######## set NOTIFY (BLE Manager)
  5. setCharacteristicNotification() - uuid: ########## enable: true (BluetoothGatt)
  6. onDescriptorWrite success (BLE Manager)
  7. Command queue empty (BLE Manager)

After all the above steps it should call "onCharacteristicChanged: " (BLE Manager)

@marcosinigaglia can you please tell me what could be the issue?

UPDATE

  • Found that onCharacteristicChanged method was deprecated in API level 33 with two params
  • the new onCharacteristicChanged method has third param as byte[] value

@marcosinigaglia Can you please change in the code or let me know how can I make a change so moving forward Android API level >=33 will work

  • Just checked with other phone with android 13 and api level 33 and it works,, but for above phone Galaxy s21 it does not work

inventivemedia avatar Aug 04 '23 01:08 inventivemedia

I had the same issue. : ( Device: Android 12 react-native-ble-manager version: 10.1.2 react-native version: 0.71.2

BleManagerDidUpdateValueForCharacteristic did not trigger when startNotification

tonyle-geocomply avatar Aug 07 '23 16:08 tonyle-geocomply

@marcosinigaglia what could be the issue? onCharacteristicChanged is not getting fired but write descriptor gives success

inventivemedia avatar Aug 08 '23 00:08 inventivemedia

On IOS device BleManagerDidUpdateValueForCharacteristic can listen the data, but on Android device, I had this problem for the above phone Galaxy s21

tonyle-geocomply avatar Aug 08 '23 04:08 tonyle-geocomply

@marcosinigaglia is there a way we can debug and see what's the error? callback is not getting triggered

inventivemedia avatar Aug 10 '23 05:08 inventivemedia

@Srikanth-Enuguru, you still had issues with your Android device? Try another android version or update v.10.1.3 ? Device: Android 12 react-native-ble-manager version: 10.1.2 react-native version: 0.71.2

tonyle-geocomply avatar Aug 10 '23 06:08 tonyle-geocomply

Hi all, I test again on android 12/13 and is working fine. Check your permission and if your code is correct and if you can use a testing ble app to check if your device is working as expect.

marcosinigaglia avatar Aug 10 '23 08:08 marcosinigaglia

@marcosinigaglia thanks for your message. so what I have noticed is: I have used your example file for connection, notification etc...

  1. it connects
  2. get services
  3. read descriptor values
  4. start notification -> I get success but straight after I get BleManagerDisconnectPeripheral event (disconnected)

Thus data is not received in the app.

any idea? @tonyle-geocomply @Srikanth-Enuguru is that same happening with you guys?

inventivemedia avatar Aug 11 '23 04:08 inventivemedia

as soon as starting the notification it seems that GATT client closes the connection. "onConnectionStateChange" getting newState as 0 -> Disconnected

inventivemedia avatar Aug 11 '23 06:08 inventivemedia

@inventivemedia I resolved my problem. Just add requestMTU (Only on Android) before startNotification then it worked :D

BleManager.requestMTU(chosenPeripheral, 512) 
then 
BleManager.startNotification

Thank you @marcosinigaglia

toanleviet95 avatar Aug 13 '23 15:08 toanleviet95

@toanleviet95 I have tried but it still does not work, the maximum set MTU from the device I get is 150 bytes, do not think this would be an issue when getting data back from device... the problem is as soon as it starts notification mobile phone gets disconnected using bluetooth.

The phone has 5.2 bluetooth version

inventivemedia avatar Aug 14 '23 01:08 inventivemedia

@marcosinigaglia anything you could suggest?

inventivemedia avatar Aug 19 '23 08:08 inventivemedia

@marcosinigaglia checked with other phone, Google pixel 6 which has 5.2 bluetooth version, API level 33 and android 13. It does not work for it too! seems an issue with higher version, even changing MTU does not work..Can you please suggest? @Srikanth-Enuguru have you found any solution? @toanleviet95 which bluetooth version you have? and API level

inventivemedia avatar Aug 24 '23 06:08 inventivemedia

I'm on Android 13 using API 33 with a Samsung A04e and have the same issue as above. Logcat shows this....

startNotification (BLE Manager) registerNotify (BLE Manager) setCharacteristicNotification() - uuid:####### enable: true (BluetoothGatt) Characteristic ######## set NOTIFY (BLE Manager) setCharacteristicNotification() - uuid: ########## enable: true (BluetoothGatt) onDescriptorWrite success (BLE Manager) Command queue empty (BLE Manager)

Everything seems to be setup correctly, but onCharacteristicChanged callback never gets invoked.

@inventivemedia @Srikanth-Enuguru @toanleviet95 did you have any success in fixing this?

@marcosinigaglia anything you can recommend? Much appreciated. I have tried overloading the onCharacteristicChanged method with the new version in API 33 with the byte[] value parameter, but still have not had much success. I'm happy to help test any potential fixes if you have any guidance.

arod7533 avatar Oct 26 '23 21:10 arod7533

I'm on Android 13 using API 33 with a Samsung A04e and have the same issue as above. Logcat shows this....

startNotification (BLE Manager) registerNotify (BLE Manager) setCharacteristicNotification() - uuid:####### enable: true (BluetoothGatt) Characteristic ######## set NOTIFY (BLE Manager) setCharacteristicNotification() - uuid: ########## enable: true (BluetoothGatt) onDescriptorWrite success (BLE Manager) Command queue empty (BLE Manager)

Everything seems to be setup correctly, but onCharacteristicChanged callback never gets invoked.

@inventivemedia @Srikanth-Enuguru @toanleviet95 did you have any success in fixing this?

@marcosinigaglia anything you can recommend? Much appreciated. I have tried overloading the onCharacteristicChanged method with the new version in API 33 with the byte[] value parameter, but still have not had much success. I'm happy to help test any potential fixes if you have any guidance.

Did you check with other apps (like lightblue) if is working?

marcosinigaglia avatar Dec 04 '23 11:12 marcosinigaglia

BleManager.requestMTU(chosenPeripheral, 512) then BleManager.startNotification

My case was fixed by

BleManager.requestMTU(chosenPeripheral, 512) 
then 
BleManager.startNotification

tonyle-geocomply avatar Dec 06 '23 03:12 tonyle-geocomply

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

marcosinigaglia avatar Mar 06 '24 01:03 marcosinigaglia