edrosten / libblepp

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

Name not populated #36

Closed dmars1972 closed 6 years ago

dmars1972 commented 6 years ago

I can't find any way to force a wait on a lescan for device name... I can get the addresses, but the name doesn't appear to be populated (yet), but I can't find any way to say 'go get the name' or 'wait for the name to show up' in get_advertisements. Meaning (using lescan example):

vector<AdvertisingResponse> ads = scanner.get_advertisements();

for(const auto& ad: ads) {
    cout << "Found device: " << ad.address << endl;

    if( ! ad.local_name)
        _**ad.getLocalName();**_  // added this as what I'm trying to accomplish
     cout << ad.local_name->name << endl;

Is there a way to do this I'm not seeing? The issue for me is I'm working on a whole group of devices, so I can't depend on filtering by MAC (hoping to just look at them by a name prefix).

edrosten commented 6 years ago

OK, three things. First, you need to make sure the scanner is set up as active, not passive. This is because most BLE devices don't advertise the name by default. Usually they just send out some basic information (a service, whether they're connectable, if they support active scanning). In active scanning mode, the BLE adapter asks the device for its name and it replies, so make sure it's in active mode (that's a constructor parameter but I believe it's the default).

Second, that means the name is basically never the first advertisement you receive. So, your loop needs to keep fetching advertisements until the one you want arrives. It needs to be something like this:

for(;;) {
    auto ads = scanner.get_advertisements();
    for(const auto& ad: ads) {
        if(ad.local_name) {
            do stuff here!
        }
    }
}

Thirdly, you might not need to use the name. There's probably some info embedded in the first packet that identifies the device, such as a service ID.

dmars1972 commented 6 years ago

Thanks! The forever loop did it. I know precisely jack about bluetooth, so I've been using the name because at least I understand that. :)

edrosten commented 6 years ago

Glad to help. The name's a perfectly good way to do it.