hbldh / bleak

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

unable to use start_notify #1595

Closed gilcu3 closed 2 weeks ago

gilcu3 commented 2 weeks ago

Description

I am trying to connect to a smart scale. There is a single characteristic that has notifications enabled, and I am using as a starter the enable_notifications.py example from the repo.

What I Did

python enable_notifications.py --address dc:23:4d:2c:4e:b6 -d 00002b10-0000-1000-8000-00805f9b34fb

Using wireshark I see that the last packet sent is always:

Bluetooth Attribute Protocol
    Opcode: Read Request (0x0a)
        0... .... = Authentication Signature: False
        .0.. .... = Command: False
        ..00 1010 = Method: Read Request (0x0a)
    Handle: 0x0009 (Generic Access: Peripheral Preferred Connection Parameters)
        [Service UUID: Generic Access (0x1800)]
        [UUID: Peripheral Preferred Connection Parameters (0x2a04)]

for which the device never replies nor sends anything else.

Logs

log.txt

The error shown at the end is:

Traceback (most recent call last):
  File "/home/rey/Downloads/enable_notfications.py", line 98, in <module>
    asyncio.run(main(args))
  File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/rey/Downloads/enable_notfications.py", line 50, in main
    await client.start_notify(args.characteristic, notification_handler)
  File "/usr/lib/python3.12/site-packages/bleak/__init__.py", line 844, in start_notify
    await self._backend.start_notify(characteristic, wrapped_callback, **kwargs)
  File "/usr/lib/python3.12/site-packages/bleak/backends/bluezdbus/client.py", line 959, in start_notify
    assert_reply(reply)
  File "/usr/lib/python3.12/site-packages/bleak/backends/bluezdbus/utils.py", line 20, in assert_reply
    raise BleakDBusError(reply.error_name, reply.body)
bleak.exc.BleakDBusError: [org.bluez.Error.Failed] Operation failed with ATT error: 0x0e (Unlikely Error)
dlech commented 2 weeks ago

This error is coming from the peripheral. I would recommend sniffing Bluetooth packets to see how the official app communicates and compare that to what Bleak is doing.

gilcu3 commented 2 weeks ago

I did already, the most notable difference I can see is that a few generic packets are not sent, therefore my suspicious that one of those is not supported and blocks communication. Original trace: bt.pcap

dlech commented 2 weeks ago

I take it that bt.pcap is the working case from a non-Linux system?

If the peripheral says it has the Peripheral Preferred Connection Parameters characteristic but fails to respond to read requests, I'm not sure what could be done.

gilcu3 commented 2 weeks ago

Yes, it is the working case from Android. I think the device might have wrong characteristics, is it possible in bleak to disable some of that default behavior?

dlech commented 2 weeks ago

No, that part is controlled by BlueZ.

gilcu3 commented 2 weeks ago

Thank you, I guess I will need a lower level library then.