noble icon indicating copy to clipboard operation
noble copied to clipboard

Can't subscribe more than 1 characteristic at time

Open alfo93 opened this issue 1 year ago • 0 comments

`

noble.on('discover', peri => {
 const ad = peri.advertisement;
    
if (regex.test(ad.localName)) {
    console.log("Found device:", ad.localName);
    
  peri.connect(error => {
        if (error) {
            console.error("Error connecting:", error);
            return;
        }

        console.log("Connected");

        peri.discoverServices(['00000000000111e19ab40002a5d5c51b'], (serviceError, services) => {
            if (serviceError) {
                console.error("Error discovering services:", serviceError);
                return;
            }

            services.forEach(service => {
                service.discoverCharacteristics([], (charaError, charas) => {
                    if (charaError) {
                        console.error("Error discovering characteristics:", charaError);
                        return;
                    }

                    if (charas.length === 0) {
                        console.log("No characteristics");
                        return;
                    }

                    charas.forEach(chara => {
                        if (chara.properties.includes('read')) {
                            try {
                                chara.read((readError, data) => {
                                    if (readError) {
                                        console.error(`Error reading characteristic ${chara.uuid}:`, readError);
                                    } else {
                                        publishData(mqttClient, ad, chara, data);
                                    }
                                });
                            } catch (readException) {
                                console.error(`Exception reading characteristic ${chara.uuid}:`, readException);
                            }
                        }

                        if (chara.properties.includes('notify')) {
                            chara.subscribe(subscribeError => {
                                if (subscribeError) {
                                    console.error(`Error setting notify on ${chara.uuid}:`, subscribeError);
                                }
                            });

                            chara.on('data', (data, isNotification) => {
                                if (isNotification) {
                                    publishData(mqttClient, ad, chara, data);
                                }
                            });
                        }
                    });
                });
            });
        });
    });
}

});

`

Once the script is connected to the device, subscribed to the first characteristics of the selected service, the other doesn't update or send any data and the only data send is related to the first characteristic.

There is any particular reason or am I doing something wrong?

alfo93 avatar Mar 04 '24 17:03 alfo93