IanHarvey / bluepy

Python interface to Bluetooth LE on Linux
Other
1.59k stars 491 forks source link

Help with notification for Inkbird #391

Closed pauledd closed 4 years ago

pauledd commented 4 years ago

Hi there

I have a Inkbird (IBT-2x) Thermometer and want to read its temperature values. I was able to get notifications from it just by sending this with gatttool:

gatttool -b 0C:AE:7D:XX:XX:XX --char-write-req -a 0x0029 -n 2107060504030201b8220000000000 &&
gatttool -b 0C:AE:7D:XX:XX:XX --char-write-req -a 0x0034 -n 0b0100000000 --listen
Characteristic value was written successfully
Characteristic value was written successfully
Notification handle = 0x0025 value: ff 0b 00 00 00 00 
Notification handle = 0x0030 value: c8 00 00 00 
Notification handle = 0x0030 value: c8 00 00 00 
...
...

The "c8 00" is the temperature in hex and in that case its 20°C. How can I get this values with bluepy? I tried to replicate the gatttool commands with this:

from bluepy.btle import *
import time

class MyDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleNotification(self, cHandle, data):
        print(cHandle,data)

dev = Peripheral("0c:ae:7d:e7:9e:3d")
dev.setDelegate(MyDelegate())

dev.writeCharacteristic(0x0029,bytearray.fromhex("2107060504030201b8220000000000"))
dev.writeCharacteristic(0x0034,b"\x01\x00\x00\x00\x00")

i=0
while i<10:
        if dev.waitForNotifications(1.0):
                print("Notifi")
                continue
        i+=1
        print("waiting")

but this gives only back:

$ ./ink_blue2.py 
37 b'!\x00\x00\x00\x00\x00'
Notifi
waiting
waiting
waiting
...

any help would appreciated Ps:

Services:
$ gatttool -b 0C:AE:7D:XX:XX:XX --primary
attr handle = 0x0001, end grp handle = 0x000b uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle = 0x000c, end grp handle = 0x000f uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle = 0x0010, end grp handle = 0x0022 uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle = 0x0023, end grp handle = 0xffff uuid: 0000fff0-0000-1000-8000-00805f9b34fb

Characteristics:
$ gatttool -b 0C:AE:7D:XX:XX:XX --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 = 0x0a, char value handle = 0x0007, uuid = 00002a02-0000-1000-8000-00805f9b34fb
handle = 0x0008, char properties = 0x08, char value handle = 0x0009, uuid = 00002a03-0000-1000-8000-00805f9b34fb
handle = 0x000a, char properties = 0x02, char value handle = 0x000b, uuid = 00002a04-0000-1000-8000-00805f9b34fb
handle = 0x000d, char properties = 0x20, char value handle = 0x000e, uuid = 00002a05-0000-1000-8000-00805f9b34fb
handle = 0x0011, char properties = 0x02, char value handle = 0x0012, uuid = 00002a23-0000-1000-8000-00805f9b34fb
handle = 0x0013, char properties = 0x02, char value handle = 0x0014, uuid = 00002a24-0000-1000-8000-00805f9b34fb
handle = 0x0015, char properties = 0x02, char value handle = 0x0016, uuid = 00002a25-0000-1000-8000-00805f9b34fb
handle = 0x0017, char properties = 0x02, char value handle = 0x0018, uuid = 00002a26-0000-1000-8000-00805f9b34fb
handle = 0x0019, char properties = 0x02, char value handle = 0x001a, uuid = 00002a27-0000-1000-8000-00805f9b34fb
handle = 0x001b, char properties = 0x02, char value handle = 0x001c, uuid = 00002a28-0000-1000-8000-00805f9b34fb
handle = 0x001d, char properties = 0x02, char value handle = 0x001e, uuid = 00002a29-0000-1000-8000-00805f9b34fb
handle = 0x001f, char properties = 0x02, char value handle = 0x0020, uuid = 00002a2a-0000-1000-8000-00805f9b34fb
handle = 0x0021, char properties = 0x02, char value handle = 0x0022, uuid = 00002a50-0000-1000-8000-00805f9b34fb
handle = 0x0024, char properties = 0x10, char value handle = 0x0025, uuid = 0000fff1-0000-1000-8000-00805f9b34fb
handle = 0x0028, char properties = 0x08, char value handle = 0x0029, uuid = 0000fff2-0000-1000-8000-00805f9b34fb
handle = 0x002b, char properties = 0x10, char value handle = 0x002c, uuid = 0000fff3-0000-1000-8000-00805f9b34fb
handle = 0x002f, char properties = 0x10, char value handle = 0x0030, uuid = 0000fff4-0000-1000-8000-00805f9b34fb
handle = 0x0033, char properties = 0x08, char value handle = 0x0034, uuid = 0000fff5-0000-1000-8000-00805f9b34fb

Characteristics Decriptor:
$ gatttool -b 0C:AE:7D:XX:XX:XX --char-desc
handle = 0x0001, uuid = 00002800-0000-1000-8000-00805f9b34fb
handle = 0x0002, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0003, uuid = 00002a00-0000-1000-8000-00805f9b34fb
handle = 0x0004, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0005, uuid = 00002a01-0000-1000-8000-00805f9b34fb
handle = 0x0006, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0007, uuid = 00002a02-0000-1000-8000-00805f9b34fb
handle = 0x0008, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0009, uuid = 00002a03-0000-1000-8000-00805f9b34fb
handle = 0x000a, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x000b, uuid = 00002a04-0000-1000-8000-00805f9b34fb
handle = 0x000c, uuid = 00002800-0000-1000-8000-00805f9b34fb
handle = 0x000d, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x000e, uuid = 00002a05-0000-1000-8000-00805f9b34fb
handle = 0x000f, uuid = 00002902-0000-1000-8000-00805f9b34fb
handle = 0x0010, uuid = 00002800-0000-1000-8000-00805f9b34fb
handle = 0x0011, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0012, uuid = 00002a23-0000-1000-8000-00805f9b34fb
handle = 0x0013, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0014, uuid = 00002a24-0000-1000-8000-00805f9b34fb
handle = 0x0015, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0016, uuid = 00002a25-0000-1000-8000-00805f9b34fb
handle = 0x0017, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0018, uuid = 00002a26-0000-1000-8000-00805f9b34fb
handle = 0x0019, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x001a, uuid = 00002a27-0000-1000-8000-00805f9b34fb
handle = 0x001b, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x001c, uuid = 00002a28-0000-1000-8000-00805f9b34fb
handle = 0x001d, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x001e, uuid = 00002a29-0000-1000-8000-00805f9b34fb
handle = 0x001f, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0020, uuid = 00002a2a-0000-1000-8000-00805f9b34fb
handle = 0x0021, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0022, uuid = 00002a50-0000-1000-8000-00805f9b34fb
handle = 0x0023, uuid = 00002800-0000-1000-8000-00805f9b34fb
handle = 0x0024, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0025, uuid = 0000fff1-0000-1000-8000-00805f9b34fb
handle = 0x0026, uuid = 00002902-0000-1000-8000-00805f9b34fb
handle = 0x0027, uuid = 00002901-0000-1000-8000-00805f9b34fb
handle = 0x0028, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0029, uuid = 0000fff2-0000-1000-8000-00805f9b34fb
handle = 0x002a, uuid = 00002901-0000-1000-8000-00805f9b34fb
handle = 0x002b, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x002c, uuid = 0000fff3-0000-1000-8000-00805f9b34fb
handle = 0x002d, uuid = 00002902-0000-1000-8000-00805f9b34fb
handle = 0x002e, uuid = 00002901-0000-1000-8000-00805f9b34fb
handle = 0x002f, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0030, uuid = 0000fff4-0000-1000-8000-00805f9b34fb
handle = 0x0031, uuid = 00002902-0000-1000-8000-00805f9b34fb
handle = 0x0032, uuid = 00002901-0000-1000-8000-00805f9b34fb
handle = 0x0033, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0034, uuid = 0000fff5-0000-1000-8000-00805f9b34fb
handle = 0x0035, uuid = 00002901-0000-1000-8000-00805f9b34f
pauledd commented 4 years ago

Got it working:

class MyDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleNotification(self, cHandle, data):
        print(cHandle,data)

dev = Peripheral("0c:ae:7d:xx:xx:xx")
dev.setDelegate(MyDelegate())

# authenticate
z = dev.getCharacteristics(0x0028,0x0029)
dev.writeCharacteristic(0x0029,bytearray.fromhex("2107060504030201b8220000000000",),1)

# Enable realtime data collection
s = dev.getCharacteristics(0x0033,0x0034)
dev.writeCharacteristic(0x0034,bytearray.fromhex("0B0100000000"),1)

# Subscribe to realtime data collectionx
service = dev.getServiceByUUID(UUID("0000fff0-0000-1000-8000-00805f9b34fb"))
service.getCharacteristics(UUID("0000fff4-0000-1000-8000-00805f9b34fb"))

i=0
while i<10:
        if dev.waitForNotifications(2.0):
                print("Notifi")
                continue
        i+=1
        print("waiting")