edrosten / libblepp

Modern clean C++ Bluetooth Low Energy on Linux without the Bluez DBUS API
Other
239 stars 62 forks source link

Use the same bluetooth connection to send more than one command receiving notifications? #52

Open isalinas8 opened 4 years ago

isalinas8 commented 4 years ago

Using the examples of the library I am able to send one command and recieve the notification from the device which I'm working with.

But currently I'm trying to reuse this connection and send more than one command so that is more efficient than using one connection to send each command.

The problem is that when I wait to recieve the answer from the first command (for example sleeping some seconds) nothing is recieved (if I dont wait and the function finishes the answer it is recieved). Mi idea is to send the next command after receiving the notification and checking that everything is OK.

Pseudocode:

std::function<void()> cb = [& gatt , & notify_cb ,  & enable] () {

        for (auto &service : gatt.primary_services)
        {
            //cout << "Setting up notifications..." << endl;
            if (service.uuid == UUID("UUID"))
            {
                for (auto &characteristic : service.characteristics){
                    if (characteristic.uuid == UUID("CHARACTERISTIC UUID"))
                    {
                        uint8_t buf[1] = {0X0000};
                        characteristic.cb_notify_or_indicate = notify_cb; // Function executed when a notification is recieved
                        characteristic.set_notify_and_indicate(.....);   

                        // Here I would like to wait for the notification 

                        buf[0] = uint8_t(0x0001);
                        characteristic.set_notify_and_indicate(.....); // Send another command

                        gatt.close(); // End 

                    }
                    else
                    {
                        cerr << "I am not able to execute the basic command" << endl;
                    }               
                }
            }
        }

    };

Is it possible to do this with the library? If so, could you explain how?

Thanks in advance for any help! :)