hbldh / bleak

A cross platform Bluetooth Low Energy Client for Python using asyncio
MIT License
1.73k stars 291 forks source link

Bleak is not returning Generic Access Service (0x1800) and looks like the handles are wrong numbered (Linux implementation) #250

Closed aleksmarcelo closed 4 years ago

aleksmarcelo commented 4 years ago

Description

When you simply list services and characteristics ( I used a Mi Band Fit)

What I Did

Bleak version and results

import asyncio
from bleak import BleakClient

async def run(address, loop):
    client = BleakClient(address, loop=loop)

    x = await client.connect()

    for service in client.services:
        print(service.uuid)
        for c in service.characteristics:
            print(f"\t[Characteristic] {c.uuid}:  Handle: {hex(c.handle)} ")

if __name__ == "__main__":
    address = "DB:6E:07:2F:66:38"
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(address, loop))

Results Bleak:

00003802-0000-1000-8000-00805f9b34fb
        00004a02-0000-1000-8000-00805f9b34fb:  Handle: 0x74 
0000fee1-0000-1000-8000-00805f9b34fb
        0000fec1-0000-3512-2118-0009af100700:  Handle: 0x70 
        0000fed3-0000-1000-8000-00805f9b34fb:  Handle: 0x6e 
        0000fed2-0000-1000-8000-00805f9b34fb:  Handle: 0x6c 
        0000fed1-0000-1000-8000-00805f9b34fb:  Handle: 0x6a 
        0000fed0-0000-1000-8000-00805f9b34fb:  Handle: 0x68 
        0000fedf-0000-1000-8000-00805f9b34fb:  Handle: 0x66 
        0000fede-0000-1000-8000-00805f9b34fb:  Handle: 0x64 
        0000fedd-0000-1000-8000-00805f9b34fb:  Handle: 0x62 
        00000009-0000-3512-2118-0009af100700:  Handle: 0x5f 
0000fee0-0000-1000-8000-00805f9b34fb
        00000013-0000-3512-2118-0009af100700:  Handle: 0x5b 
        00000012-0000-3512-2118-0009af100700:  Handle: 0x58 
        00000011-0000-3512-2118-0009af100700:  Handle: 0x55 
        0000000f-0000-3512-2118-0009af100700:  Handle: 0x52 
        0000000e-0000-3512-2118-0009af100700:  Handle: 0x4f 
        00000020-0000-3512-2118-0009af100700:  Handle: 0x4c 
        00000010-0000-3512-2118-0009af100700:  Handle: 0x49 
        00000008-0000-3512-2118-0009af100700:  Handle: 0x46 
        00000007-0000-3512-2118-0009af100700:  Handle: 0x43 
        00000006-0000-3512-2118-0009af100700:  Handle: 0x40 
        00000005-0000-3512-2118-0009af100700:  Handle: 0x3d 
        00000004-0000-3512-2118-0009af100700:  Handle: 0x3a 
        00002a04-0000-1000-8000-00805f9b34fb:  Handle: 0x37 
        00000003-0000-3512-2118-0009af100700:  Handle: 0x34 
        00000002-0000-3512-2118-0009af100700:  Handle: 0x31 
        00000001-0000-3512-2118-0009af100700:  Handle: 0x2e 
        00002a2b-0000-1000-8000-00805f9b34fb:  Handle: 0x2b 
0000180d-0000-1000-8000-00805f9b34fb
        00002a39-0000-1000-8000-00805f9b34fb:  Handle: 0x28 
        00002a37-0000-1000-8000-00805f9b34fb:  Handle: 0x25 
00001802-0000-1000-8000-00805f9b34fb
        00002a06-0000-1000-8000-00805f9b34fb:  Handle: 0x22 
00001811-0000-1000-8000-00805f9b34fb
        00002a44-0000-1000-8000-00805f9b34fb:  Handle: 0x1e 
        00002a46-0000-1000-8000-00805f9b34fb:  Handle: 0x1b 
00001530-0000-3512-2118-0009af100700
        00001532-0000-3512-2118-0009af100700:  Handle: 0x18 
        00001531-0000-3512-2118-0009af100700:  Handle: 0x15 
0000180a-0000-1000-8000-00805f9b34fb
        00002a50-0000-1000-8000-00805f9b34fb:  Handle: 0x12 
        00002a23-0000-1000-8000-00805f9b34fb:  Handle: 0x10 
        00002a28-0000-1000-8000-00805f9b34fb:  Handle: 0xe 
        00002a27-0000-1000-8000-00805f9b34fb:  Handle: 0xc 
        00002a25-0000-1000-8000-00805f9b34fb:  Handle: 0xa 
00001801-0000-1000-8000-00805f9b34fb

Using gatttol to list services:

aleks iotfever-posts$ gatttool -i hci0 -b DB:6E:07:2F:66:38 --primary
attr handle = 0x0001, end grp handle = 0x0007 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle = 0x0008, end grp handle = 0x0008 uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle = 0x0009, end grp handle = 0x0013 uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle = 0x0014, end grp handle = 0x0019 uuid: 00001530-0000-3512-2118-0009af100700
attr handle = 0x001a, end grp handle = 0x0020 uuid: 00001811-0000-1000-8000-00805f9b34fb
attr handle = 0x0021, end grp handle = 0x0023 uuid: 00001802-0000-1000-8000-00805f9b34fb
attr handle = 0x0024, end grp handle = 0x0029 uuid: 0000180d-0000-1000-8000-00805f9b34fb
attr handle = 0x002a, end grp handle = 0x005d uuid: 0000fee0-0000-1000-8000-00805f9b34fb
attr handle = 0x005e, end grp handle = 0x0072 uuid: 0000fee1-0000-1000-8000-00805f9b34fb
attr handle = 0x0073, end grp handle = 0x0076 uuid: 00003802-0000-1000-8000-00805f9b34fb
aleks iotfever-posts$ 

Using gatttol to list all characteristics:

 aleks iotfever-posts$ gatttool -i hci0 -b DB:6E:07:2F:66:38 --characteristics
handle = 0x0002, char properties = 0x02, char value handle = 0x0003, uuid = 00002a00-0000-1000-8000-00805f9b34fb
handle = 0x0004, char properties = 0x02, char value handle = 0x0005, uuid = 00002a01-0000-1000-8000-00805f9b34fb
handle = 0x0006, char properties = 0x02, char value handle = 0x0007, uuid = 00002a04-0000-1000-8000-00805f9b34fb
handle = 0x000a, char properties = 0x02, char value handle = 0x000b, uuid = 00002a25-0000-1000-8000-00805f9b34fb
handle = 0x000c, char properties = 0x02, char value handle = 0x000d, uuid = 00002a27-0000-1000-8000-00805f9b34fb
handle = 0x000e, char properties = 0x02, char value handle = 0x000f, uuid = 00002a28-0000-1000-8000-00805f9b34fb
handle = 0x0010, char properties = 0x02, char value handle = 0x0011, uuid = 00002a23-0000-1000-8000-00805f9b34fb
handle = 0x0012, char properties = 0x02, char value handle = 0x0013, uuid = 00002a50-0000-1000-8000-00805f9b34fb
handle = 0x0015, char properties = 0x18, char value handle = 0x0016, uuid = 00001531-0000-3512-2118-0009af100700
handle = 0x0018, char properties = 0x04, char value handle = 0x0019, uuid = 00001532-0000-3512-2118-0009af100700
handle = 0x001b, char properties = 0x0a, char value handle = 0x001c, uuid = 00002a46-0000-1000-8000-00805f9b34fb
handle = 0x001e, char properties = 0x1a, char value handle = 0x001f, uuid = 00002a44-0000-1000-8000-00805f9b34fb
handle = 0x0022, char properties = 0x04, char value handle = 0x0023, uuid = 00002a06-0000-1000-8000-00805f9b34fb
handle = 0x0025, char properties = 0x10, char value handle = 0x0026, uuid = 00002a37-0000-1000-8000-00805f9b34fb
handle = 0x0028, char properties = 0x0a, char value handle = 0x0029, uuid = 00002a39-0000-1000-8000-00805f9b34fb
handle = 0x002b, char properties = 0x1a, char value handle = 0x002c, uuid = 00002a2b-0000-1000-8000-00805f9b34fb
handle = 0x002e, char properties = 0x14, char value handle = 0x002f, uuid = 00000001-0000-3512-2118-0009af100700
handle = 0x0031, char properties = 0x10, char value handle = 0x0032, uuid = 00000002-0000-3512-2118-0009af100700
handle = 0x0034, char properties = 0x14, char value handle = 0x0035, uuid = 00000003-0000-3512-2118-0009af100700
handle = 0x0037, char properties = 0x16, char value handle = 0x0038, uuid = 00002a04-0000-1000-8000-00805f9b34fb
handle = 0x003a, char properties = 0x14, char value handle = 0x003b, uuid = 00000004-0000-3512-2118-0009af100700
handle = 0x003d, char properties = 0x10, char value handle = 0x003e, uuid = 00000005-0000-3512-2118-0009af100700
handle = 0x0040, char properties = 0x12, char value handle = 0x0041, uuid = 00000006-0000-3512-2118-0009af100700
handle = 0x0043, char properties = 0x12, char value handle = 0x0044, uuid = 00000007-0000-3512-2118-0009af100700
handle = 0x0046, char properties = 0x18, char value handle = 0x0047, uuid = 00000008-0000-3512-2118-0009af100700
handle = 0x0049, char properties = 0x10, char value handle = 0x004a, uuid = 00000010-0000-3512-2118-0009af100700
handle = 0x004c, char properties = 0x16, char value handle = 0x004d, uuid = 00000020-0000-3512-2118-0009af100700
handle = 0x004f, char properties = 0x08, char value handle = 0x0050, uuid = 0000000e-0000-3512-2118-0009af100700
handle = 0x0052, char properties = 0x14, char value handle = 0x0053, uuid = 0000000f-0000-3512-2118-0009af100700
handle = 0x0055, char properties = 0x16, char value handle = 0x0056, uuid = 00000011-0000-3512-2118-0009af100700
handle = 0x0058, char properties = 0x16, char value handle = 0x0059, uuid = 00000012-0000-3512-2118-0009af100700
handle = 0x005b, char properties = 0x1a, char value handle = 0x005c, uuid = 00000013-0000-3512-2118-0009af100700
handle = 0x005f, char properties = 0x16, char value handle = 0x0060, uuid = 00000009-0000-3512-2118-0009af100700
handle = 0x0062, char properties = 0x08, char value handle = 0x0063, uuid = 0000fedd-0000-1000-8000-00805f9b34fb
handle = 0x0064, char properties = 0x02, char value handle = 0x0065, uuid = 0000fede-0000-1000-8000-00805f9b34fb
handle = 0x0066, char properties = 0x02, char value handle = 0x0067, uuid = 0000fedf-0000-1000-8000-00805f9b34fb
handle = 0x0068, char properties = 0x0a, char value handle = 0x0069, uuid = 0000fed0-0000-1000-8000-00805f9b34fb
handle = 0x006a, char properties = 0x0a, char value handle = 0x006b, uuid = 0000fed1-0000-1000-8000-00805f9b34fb
handle = 0x006c, char properties = 0x02, char value handle = 0x006d, uuid = 0000fed2-0000-1000-8000-00805f9b34fb
handle = 0x006e, char properties = 0x0a, char value handle = 0x006f, uuid = 0000fed3-0000-1000-8000-00805f9b34fb
handle = 0x0070, char properties = 0x1a, char value handle = 0x0071, uuid = 0000fec1-0000-3512-2118-0009af100700
handle = 0x0074, char properties = 0x1a, char value handle = 0x0075, uuid = 00004a02-0000-1000-8000-00805f9b34fb
aleks iotfever-posts$ 

bluepy sample:

from bluepy.btle import Scanner, DefaultDelegate, Peripheral

p = Peripheral("DB:6E:07:2F:66:38")
for s in p.getServices():
    print(f"{s.uuid} ")
    for c in s.getCharacteristics():
        print(f"\t{c.uuid}  handle:{hex(c.getHandle())} ")

bluepy results:

00001800-0000-1000-8000-00805f9b34fb 
        00002a00-0000-1000-8000-00805f9b34fb  handle:0x3 
        00002a01-0000-1000-8000-00805f9b34fb  handle:0x5 
        00002a04-0000-1000-8000-00805f9b34fb  handle:0x7 
00001801-0000-1000-8000-00805f9b34fb 
0000180a-0000-1000-8000-00805f9b34fb 
        00002a25-0000-1000-8000-00805f9b34fb  handle:0xb 
        00002a27-0000-1000-8000-00805f9b34fb  handle:0xd 
        00002a28-0000-1000-8000-00805f9b34fb  handle:0xf 
        00002a23-0000-1000-8000-00805f9b34fb  handle:0x11 
        00002a50-0000-1000-8000-00805f9b34fb  handle:0x13 
00001530-0000-3512-2118-0009af100700 
        00001531-0000-3512-2118-0009af100700  handle:0x16 
        00001532-0000-3512-2118-0009af100700  handle:0x19 
00001811-0000-1000-8000-00805f9b34fb 
        00002a46-0000-1000-8000-00805f9b34fb  handle:0x1c 
        00002a44-0000-1000-8000-00805f9b34fb  handle:0x1f 
00001802-0000-1000-8000-00805f9b34fb 
        00002a06-0000-1000-8000-00805f9b34fb  handle:0x23 
0000180d-0000-1000-8000-00805f9b34fb 
        00002a37-0000-1000-8000-00805f9b34fb  handle:0x26 
        00002a39-0000-1000-8000-00805f9b34fb  handle:0x29 
0000fee0-0000-1000-8000-00805f9b34fb 
        00002a2b-0000-1000-8000-00805f9b34fb  handle:0x2c 
        00000001-0000-3512-2118-0009af100700  handle:0x2f 
        00000002-0000-3512-2118-0009af100700  handle:0x32 
        00000003-0000-3512-2118-0009af100700  handle:0x35 
        00002a04-0000-1000-8000-00805f9b34fb  handle:0x38 
        00000004-0000-3512-2118-0009af100700  handle:0x3b 
        00000005-0000-3512-2118-0009af100700  handle:0x3e 
        00000006-0000-3512-2118-0009af100700  handle:0x41 
        00000007-0000-3512-2118-0009af100700  handle:0x44 
        00000008-0000-3512-2118-0009af100700  handle:0x47 
        00000010-0000-3512-2118-0009af100700  handle:0x4a 
        00000020-0000-3512-2118-0009af100700  handle:0x4d 
        0000000e-0000-3512-2118-0009af100700  handle:0x50 
        0000000f-0000-3512-2118-0009af100700  handle:0x53 
        00000011-0000-3512-2118-0009af100700  handle:0x56 
        00000012-0000-3512-2118-0009af100700  handle:0x59 
        00000013-0000-3512-2118-0009af100700  handle:0x5c 
0000fee1-0000-1000-8000-00805f9b34fb 
        00000009-0000-3512-2118-0009af100700  handle:0x60 
        0000fedd-0000-1000-8000-00805f9b34fb  handle:0x63 
        0000fede-0000-1000-8000-00805f9b34fb  handle:0x65 
        0000fedf-0000-1000-8000-00805f9b34fb  handle:0x67 
        0000fed0-0000-1000-8000-00805f9b34fb  handle:0x69 
        0000fed1-0000-1000-8000-00805f9b34fb  handle:0x6b 
        0000fed2-0000-1000-8000-00805f9b34fb  handle:0x6d 
        0000fed3-0000-1000-8000-00805f9b34fb  handle:0x6f 
        0000fec1-0000-3512-2118-0009af100700  handle:0x71 
00003802-0000-1000-8000-00805f9b34fb 
        00004a02-0000-1000-8000-00805f9b34fb  handle:0x75 

You can observe that the service 0x1800 is not returned by bleak and the handle id is not the right one pointing to the characteristic. Anyway, correcting some issues Bleak will be the most one!

hbldh commented 4 years ago

The BlueZ DBus API does things a bit differently from e.g. gatttool and previous BlueZ API solutions. The Battery Service (0x180F) is instead served under the Battery DBus interface, making that service not available in get_services calls. That service has, I believe, been relocated to the Device interface and is available on the device object instead. See here: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt

The device you have connected to is available in object you get when doing discovery. Check the details attribute of the BLEDevice object.

Bleak tries its hardest to be identical over all OS:es, but some things are not possible to do, and I think this is one of them. The BlueZ DBus API is not comparable to gatttool. Are the handles you use giving you the wrong data? Verify that first. If they differ it might be due to BlueZ readressing this internally, I cannot be certain. Bleak merely uses what handles that are presented by the BlueZ DBus API.

hbldh commented 4 years ago

There has been some changes regarding handles in version 0.8.0. It might solve this, if it even was an issue to start with. I get correct results using the handles that Bleak provides, at least for my devices. They are the handles provided by the BlueZ DBus API and I cannot use anything else than them.

Will close this issue. Feel free to reopen if new data is dicovered.