peplin/pygatt

how to listen to indication?

pauledd opened this issue · 2 comments

Hi

I try to read stored data from a blood pressure meter.
I can do it with gatttool without problems this way:

gatttool -b 78:A5:04:XX:XX:XX --char-write-req -a 0x0013 -n 0200 --listen
Characteristic value was written successfully
Indication   handle = 0x0012 value: 1e 57 00 32 00 50 00 e4 07 05 01 09 26 00 4a 00 00 6a 00 
Indication   handle = 0x0012 value: 1e 57 00 32 00 3e 00 e4 07 05 01 10 37 00 4c 00 00 72 00 
Indication   handle = 0x0012 value: 1e 5d 00 2f 00 49 00 e4 07 05 08 0f 33 00 47 00 00 7d 00
... continues till 180 values have been read and stops 

Now with pygatt I was able to read only the first data item with this code:

import pygatt
import logging
from pygatt.util import uuid16_to_uuid

addr = '78:A5:04:XX:XX:XX'

logging.basicConfig()
logging.getLogger('pygatt').setLevel(logging.DEBUG)

adapter = pygatt.GATTToolBackend()

def handle_data(handle, value):
    print("handler called")

try:
    adapter.start()
    device = adapter.connect(addr,10)
    device.subscribe(uuid16_to_uuid(0x2a35), callback=handle_data, indication=True)
finally:
    adapter.stop()

then it simply stopped and this is the output:

DEBUG:pygatt.backends.gatttool.gatttool:gatttool_cmd=gatttool -i hci0 -I
INFO:pygatt.backends.gatttool.gatttool:Running...
INFO:pygatt.backends.gatttool.gatttool:Connecting to 78:A5:04:XX:XX:XX with timeout=10
DEBUG:pygatt.device:Looking up handle for characteristic 00002a35-0000-1000-8000-00805f9b34fb
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a00-0000-1000-8000-00805f9b34fb, value handle: 0x3
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a01-0000-1000-8000-00805f9b34fb, value handle: 0x5
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a02-0000-1000-8000-00805f9b34fb, value handle: 0x7
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a03-0000-1000-8000-00805f9b34fb, value handle: 0x9
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a04-0000-1000-8000-00805f9b34fb, value handle: 0xb
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a05-0000-1000-8000-00805f9b34fb, value handle: 0xe
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a35-0000-1000-8000-00805f9b34fb, value handle: 0x12
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a36-0000-1000-8000-00805f9b34fb, value handle: 0x16
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a49-0000-1000-8000-00805f9b34fb, value handle: 0x19
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a23-0000-1000-8000-00805f9b34fb, value handle: 0x1c
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a24-0000-1000-8000-00805f9b34fb, value handle: 0x1e
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a25-0000-1000-8000-00805f9b34fb, value handle: 0x20
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a26-0000-1000-8000-00805f9b34fb, value handle: 0x22
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a27-0000-1000-8000-00805f9b34fb, value handle: 0x24
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a28-0000-1000-8000-00805f9b34fb, value handle: 0x26
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a29-0000-1000-8000-00805f9b34fb, value handle: 0x28
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a2a-0000-1000-8000-00805f9b34fb, value handle: 0x2a
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a50-0000-1000-8000-00805f9b34fb, value handle: 0x2c
DEBUG:pygatt.backends.gatttool.gatttool:Found characteristic 00002a19-0000-1000-8000-00805f9b34fb, value handle: 0x2f
DEBUG:pygatt.device:Found <Characteristic uuid=00002a35-0000-1000-8000-00805f9b34fb handle=18>
DEBUG:pygatt.backends.gatttool.gatttool:Sending cmd=char-write-req 0x13 0200
INFO:pygatt.backends.gatttool.gatttool:Sent cmd=char-write-req 0x13 0200
INFO:pygatt.device:Subscribed to uuid=00002a35-0000-1000-8000-00805f9b34fb
INFO:pygatt.device:Received notification on handle=0x12, value=0xb'1e570032005000e40705010926004a00006a00'
handler called
INFO:pygatt.backends.gatttool.gatttool:Stopping
INFO:pygatt.backends.gatttool.gatttool:Listener thread finished

How can I repeatedly get data until the device stops sending it?

The call to device.subscribe() isn't blocking so your try block is being immediately followed by finally and stopping the adapter. A simple infinite loop after the subscribe should fix it:

while True:
  pass

This also renders the try block redundant unless you are going to check for subscribe errors. You could also use the while loop to perform error checking on the incoming data and terminate as appropriate.

while True:
  pass

Thanks, that works!