pylgbst icon indicating copy to clipboard operation
pylgbst copied to clipboard

To bleak or not to bleak...?

Open HotDog702 opened this issue 1 year ago • 3 comments

Hello. I have a win10 system with anaconda installed. I have a rather simple code to test bleak connection (as demo.py does not work)

import time
import asyncio
from bleak import BleakScanner, BleakClient
import pylgbst

# address = "80:6F:B0:A4:83:59"  # Handset
# address = "90:84:2B:18:8D:71"  # HUB NO.4
address = "00:16:53:AF:A1:FF"  # MoveHub

# [Service] 00001801-0000-1000-8000-00805f9b34fb: Generic Attribute Profile
# [Service] 00001800-0000-1000-8000-00805f9b34fb: Generic Access Profile
LEGO_Hub_Service = '00001623-1212-efde-1623-785feabcd123'
LEGO_Hub_Characteristic = '00001624-1212-efde-1623-785feabcd123'

async def main1(address):
    async with BleakClient(address) as client:
        print("Connected: {0}".format(client.is_connected))
        # await client.write_gatt_char(LEGO_Hub_Characteristic, bytearray([0x05, 0x00, 0x01, 0x01, 0x05]))
        for service in client.services:
            print("[Service] {0}: {1}".format(service.uuid, service.description))
            for char in service.characteristics:
                if "read" in char.properties:
                    try:
                        value = bytes(await client.read_gatt_char(char.uuid))
                    except Exception as e:
                        value = str(e).encode()
                else:
                    value = None
                print(
                    "\t[Characteristic] {0}: (Handle: {1}) ({2}) | Name: {3}, Value: {4} ".format(
                        char.uuid,
                        char.handle,
                        ",".join(char.properties),
                        char.description,
                        value,
                    )
                )
                print("Connected: {0}".format(client.is_connected))
                if False:  #char.uuid == LEGO_Hub_Characteristic:
                    print(f'\t\t{len(char.descriptors)} descriptor(s) found')
                    print("Connected: {0}".format(client.is_connected))
                    for descriptor in char.descriptors:
                        print(
                            "\t\t[Descriptor] {0}: (Handle: {1})".format(
                                descriptor.uuid, descriptor.handle)
                            )
                        # value = await client.read_gatt_descriptor(descriptor.handle)
                        # print(
                        #     "\t\t[Descriptor] {0}: (Handle: {1}) | Value: {2} ".format(
                        #         descriptor.uuid, descriptor.handle, bytes(value)
                        #     )
                        # )
asyncio.run(main1(address))

The problem is that with this code I can connect and read characteristics from Handset and SmartHub, but not MoveHub. MoveHub allows to read services, but reading characteristics returns "not connected" value:

[Characteristic] 00002a04-0000-1000-8000-00805f9b34fb: (Handle: 10) (read) | Name: Peripheral Preferred Connection Parameters, Value: b'Not connected'

So, trying to further read descriptors raises an exception, as MoveHub really breaks the connection. Any ideas, why this happens and how to fix?

P.S. I've bought BlueGiga dongle, with it demo.py works

HotDog702 avatar Mar 24 '23 21:03 HotDog702