edrosten / libblepp

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

How do you avoid "trying to issue command mid state"? #50

Open NSExceptional opened 4 years ago

NSExceptional commented 4 years ago

(This is an API usage question, not a bug report or anything)

So I want to do this:

When I try to do the last step, I run into this error. I'm doing those two steps inside the setup_standard_scan callback as I find each characteristic. What should I do instead? Loop on is_idle() inside the callback before I write...?

Here is my (pseudo)code:

BLEGATTStateMachine gatt;

std::function<void(const PDUNotificationOrIndication&)> notifyCallback =
[&](const PDUNotificationOrIndication& n) {
    // ...
};    

std::function<void()> servicesAndCharsCallback = [&gatt, &notifyCallback]() {
    for (auto& service: gatt.primary_services) {
        for (auto& chara: service.characteristics) {
            if (chara.uuid == UUID("ABCD")) {
                chara.cb_notify_or_indicate = notifyCallback;
                chara.set_notify_and_indicate(true, false);
            }
            else if (chara.uuid == UUID("WXYZ")) {
                uint8_t buffer[5];
                // ...
                chara.write_command(buffer);
            }
        }
    }
};

gatt.setup_standard_scan(servicesAndCharsCallback);
gatt.connect_blocking("myaddress");
for (;;) {
    gatt.read_and_process_next();
}

Thanks in advance for taking the time to help if you do :)