labapart / gattlib

Library to access GATT information from BLE (Bluetooth Low Energy) devices
http://labapart.com/
455 stars 162 forks source link

Library is not complaint with multi devices with same GATT Profile #29

Closed Buguito closed 5 years ago

Buguito commented 7 years ago

I'm working with several devices that are exactly the same so they have the same profiles and same services and characteristics.

Unless i'm wrong, your static OrgBluezGattCharacteristic1 get_characteristic_from_uuid(const uuid_t uuid) at gattlib.c, just loops through the characteristics on the DBUS and picks the one that conforms to the handle passed, for example to read_char.c example.

So in my case even i have to devices with mac address: D5:A9:88:3E:CA:09 and D7:B9:CE:79:AA:FA, both have a same characteristic 0x2221 for certain service.

I've printf'ed the moment you decide that the method that gets the characteristic should returned in static OrgBluezGattCharacteristic1 get_characteristic_from_uuid(const uuid_t uuid)

To be more specific, here:

if (gattlib_uuid_cmp(uuid, &characteristic_uuid) == 0) { printf("PATH: %s\n",object_path); break; }

And what happens is that the object_path for the device I'M NOT CONNECTED TO is returned, instead of the object_path of the very same characteristic for the one I'M connected to. As a result the wrong characteristic is returned and the DBUS API call is made on the wrong device.

root@gizmo:~/gattlib/build/examples/read_write# ./read_write D7:B9:CE:79:AA:FA read 0x2221 Connecting.. Connected. UUID: 0x2221 PATH: /org/bluez/hci0/dev_D5_A9_88_3E_CA_09/service000c/char000d Error: GDBus.Error:org.bluez.Error.Failed: Not connected Ret: -1

I think this happens well, because as the function name states, it gets the characteristic_by_uuid, but there's not connection context used here, so the first match is returned.

It should be another way to get the characteristic but in the context of the current connected device or at very least, the mac_address.

The int gattlib_read_char_by_uuid(...) function used in the read_char.c example, actually receives a connection by parameter but never uses it (?).

Regards, Alan

oliviermartin commented 7 years ago

Thanks Alan for reporting the issue. I have not investigated it yet but your analysis seems to be correct.

I do not know when I will be able to fix it myself. But if you decide to send a pull-request with a fix I will be happy to review it and merge it :-)

PhilippFinke commented 5 years ago

This seems still to be an issue. I'm currently having the same problems.

oliviermartin commented 5 years ago

I close this issue for now as I assume @PhilippFinke's pull-request fixes this issue :+1: