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

monitor.monitorCharacteristicForDevice callback stuck and not working at all

Open ivanyufen opened this issue 6 months ago • 3 comments

Prerequisites

  • [x] I checked the documentation and FAQ without finding a solution
  • [x] I checked to make sure that this issue has not already been filed
  • [x] I'm sure that question is related to the library itself and not Bluetooth Low Energy or Classic in general. If that so, please post your question on StackOverflow.
  • [x] I'm running the latest version

Question

device is already connected and discovered, but somehow the monitorCharacteristicForDevice callback not firing at all and im just stuck without response. I'm currently using react-native 0.73.9 with react-native-ble-plx 3.5.0

Is there anything that I did wrong? thank you in advance

Question related code

manager.startDeviceScan(null, null, (error, device) => {
      if (error) {
        // Handle error (scanning will be stopped automatically)
        setState({
          isSyncing: false,
        });
        return;
      }
      console.log(device?.name);
      // Check if it is a device you are looking for based on advertisement data
      // or other criteria.
      if (device && device.name && device.name.includes("WeLock")) {
        // Stop scanning as it's not necessary if you are scanning for one device.
        console.log("found welock device", device.name);

        manager.stopDeviceScan();

        manager
          .connectToDevice(device.id)
          .then((device) => {
            console.log("device connected", device);
            return device.discoverAllServicesAndCharacteristics();
          })
          .then((device) => {
            console.log("monitor...", device.id);
            // Once connected, read the specific characteristic value
            manager.monitorCharacteristicForDevice(
              device.id,
              SERVICE_UUID,
              NOTIFY_CHARACTER,
              (err, characteristic) => {
                console.log({ err, characteristic });
              }
            );

ivanyufen avatar Jul 14 '25 09:07 ivanyufen

Did you ever figure this out? I'm having this issue in a particular situation, while the rest of my command/response cycles work fine, and I cannot figure it out.

Tclha avatar Nov 12 '25 00:11 Tclha

Did you ever figure this out? I'm having this issue in a particular situation, while the rest of my command/response cycles work fine, and I cannot figure it out.

Hi, in my case you need to trigger it by sending a dummy write function first, after that the monitor listener will trigger the callback. Something like this: handleSubscribeNotification(); // this is the function that start monitor.monitorCharacteristicForDevice setTimeout(() => { manager.writeCharacteristicWithResponseForDevice( device.id, SERVICE_UUID, WRITE_CHARACTER, "VTAAAAAAAAAAAAAAAAAA" ); }, 1000);

ivanyufen avatar Nov 12 '25 02:11 ivanyufen

@ivanyufen I really appreciate the reply, unfortunately that didn't seem to work for me :(

UPDATE: Turns out it was a write issue for me, not a read issue, ie., my command wasn't getting written fully and hence the expected response never came.

This is due to the fact that my BLE device had an MTU of 23, and the packets were bigger than this.

This library silently fails when a packet is larger than the MTU and doesn't seem to handle chunking by itself or even tell you that you need to do something.

It was frustrating and took a few days to narrow this down, as the other popular library (react-native-ble-manager) seems to handle chunking intrinsically.

Tclha avatar Nov 12 '25 03:11 Tclha