gatttool and bluepy disconnections, no issues in bluetoothctl

374 Views Asked by At

Currently using on OH1 Heart rate sensor (tiny.cc/mom03y)

I'm looking to subscribe to HR notification using bluepy. I have got the notifications working but the OH1 device disconnects after about 20-30secs in both bluepy and gatttool (remote user terminated) but not in bluetoothctl.

Looking for a reason why the connection stays alive in bluetoothctl and not in bluepy or gatttool, code and hcidump below, using bluez 5.50 and bluepy 1.30 on rasbian 4.14.

Bluepy


#packet count
packets = 0

class hrCallback(btle.DefaultDelegate):
    def __init__(self):
        btle.DefaultDelegate.__init__(self)

    def handleNotification(self, cHandle, data):
        global packets 
        packets += 1
        print("packet: %s Handle: %s HR (bpm): %s " % (packets, cHandle, data[1]))

#connect to OH1
mac = "a0:9e:1a:4f:ef:8b"
oh1 = btle.Peripheral( mac )
oh1.setDelegate( hrCallback() )

#start hr notification
oh1.writeCharacteristic(38, b"\x01\x00", True)

#listen for notifications
while True:
    try:
        if oh1.waitForNotifications(1.0):
            continue
    except btle.BTLEDisconnectError:
            pass

hcidump

> HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Parameters (0x08|0x000b) ncmd 1
    status 0x00
> HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Enable (0x08|0x000c) ncmd 1
    status 0x00
> HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Enable (0x08|0x000c) ncmd 1
    status 0x00
> HCI Event: Command Status (0x0f) plen 4
    LE Create Connection (0x08|0x000d) status 0x00 ncmd 1
> HCI Event: Command Status (0x0f) plen 4
    LE Read Remote Used Features (0x08|0x0016) status 0x00 ncmd 1
> HCI Event: Disconn Complete (0x05) plen 4
    status 0x00 handle 64 reason 0x13
    Reason: Remote User Terminated Connection

1

There are 1 best solutions below

0
On

Which version of BlueZ are you using? several tools (e.g. gatttool, hcitool, hciconfig) was deprecated and replaced by bluetoothctl and btmgmt, and the recommendation from the BlueZ team is to use the new tools instead. Please have a look at the following link:-

Deprecated BlueZ Tools

The difference between the new tools and the old tools is that the old tools were able of interfacing directly with the kernel, whereas the new tools perform operations by interfacing with D-Bus instead.

Therefore, the recommendation is always to use bluetoothctl as old tools are not maintained and this is likely why you are seeing the issue.