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

Can't subscribe more than 1 characteristic at time #330

Open
alfo93 opened this issue Mar 4, 2024 · 0 comments
Open

Can't subscribe more than 1 characteristic at time #330

alfo93 opened this issue Mar 4, 2024 · 0 comments

Comments

@alfo93
Copy link

alfo93 commented Mar 4, 2024

`

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?

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

No branches or pull requests

1 participant