react-native-ble-manager: BleManagerDidUpdateValueForCharacteristic is not being triggered

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.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 17 (2 by maintainers)

Most upvoted comments

@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

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

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

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