Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to subscribe to multiple service characteristics of 'notify type' for a device #1243

Open
4 tasks done
smitha-2020 opened this issue Oct 21, 2024 · 1 comment
Labels

Comments

@smitha-2020
Copy link

smitha-2020 commented Oct 21, 2024

Prerequisites

  • I checked the documentation and FAQ without finding a solution
  • I checked to make sure that this issue has not already been filed
  • 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.
  • 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 smitha-2020 changed the title Is it possible to monitor multiple services of 'notify type' for a device Is it possible to subscribe to multiple services of 'notify type' for a device Oct 28, 2024
@smitha-2020 smitha-2020 changed the title Is it possible to subscribe to multiple services of 'notify type' for a device Is it possible to subscribe to multiple service characteristics of 'notify type' for a device Oct 31, 2024
@dariuszseweryn
Copy link
Collaborator

Yes, it is. Please remember that if you use Promise, it will resolve only once according to the contract. That's why monitorCharacteristic is callback based.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants