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

Is it possible to monitor multiple services of 'notify type' for a device

Open smitha-2020 opened this issue 1 year ago • 0 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

I am able to connect to the device. Also monitorCharacteristicForDevice for device works for one serviceUUID and characteristicUUID but i want to get it working for 5 services and charecterisitcs. Is it possible to do using react-native-ble-plx library?

Question related code

const monitorMultipleCharacteristics = async () => {
    try {

      const monitorPromises = uuids.map(({ serviceUUID, characteristicUUID }) => {

        return new Promise((resolve, reject) => {
          BLEService.monitorCharacteristicForDevice(
            serviceUUID,
            characteristicUUID,
            async data => {

                case UUID:
                  const temperature = parse(data.value)
                  break

                case UUID:
                  console.log('dispatch service')
                  break

                default:
                  console.log('Unknown UUID', data.uuid)
              }
              setTimeout(resolve, 100, data)
              console.info('success', new Date().getTime())
            },
            async error => {
              console.error(error)
              await BLEService.finishMonitor()
              reject()
            }
          )
          // Store the subscription so it can be cleaned up later
          //subscriptions.push(subscription)
        })

        // Wait for each monitor to be set up
      })

      Promise.all(monitorPromises)
        .then(data => {
          console.log(data)
        })
        .catch(error => {
          console.error('Error in one of the monitors:', error)
        })
    } catch (error) {
      console.error('Error setting up characteristic monitoring:', error)
    }

    // Optionally return the subscriptions if you need to stop monitoring later
    //return subscriptions
  }

smitha-2020 avatar Oct 21 '24 13:10 smitha-2020