From f9130e7fa1e73b7ed39a1d3e5434a1c4e27e6b4b Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Fri, 19 Jan 2018 13:39:21 -0500 Subject: [PATCH 1/2] Added example for a BLE thermometer profile indicate subscription. --- .../subscribe_indicate_thermometer_sample.py | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 samples/subscribe_indicate_thermometer_sample.py diff --git a/samples/subscribe_indicate_thermometer_sample.py b/samples/subscribe_indicate_thermometer_sample.py new file mode 100644 index 00000000..4bd2db48 --- /dev/null +++ b/samples/subscribe_indicate_thermometer_sample.py @@ -0,0 +1,60 @@ +""" + samples.subscribe_indicate_thermometer_sample + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + This is an example of subscribing to an indicate property of a + characteristic. This example was tested with the Health Thermometer Profile, + but can be easily modified to work with any other profile. +""" +import pygatt + + +def data_handler_cb(handle: int, value: bytearray): + """ + Indication and notification come asynchronously, we use this function to + handle them either one at the time as they come. + :param handle: + :param value: + :return: + """ + print("Data: {}".format(value.hex())) + print("Handle: {}".format(handle)) + + +def main(): + """ + Main function. The comments below try to explain what each section of + the code does. + """ + + # pygatt uses pexpect and if your device has a long list of characteristics, + # pexpect will not catch them all. We increase the search window to + # 2048 bytes for the this example. By default, it is 200. + # Note: We need an instance of GATToolBackend per each device connection + adapter = pygatt.GATTToolBackend(search_window_size=2048) + + try: + # Start the adapter + adapter.start() + # Connect to the device with that given parameter. + # For scanning, use adapter.scan() + device = adapter.connect("1C:87:74:00:E5:A0") + # Set the security level to medium + device.bond() + # Observes the given characteristics for indications. + # When a response is available, calls data_handle_cb + device.subscribe("00002a1c-0000-1000-8000-00805f9b34fb", + callback=data_handler_cb, + indication=True) + + input("Press enter to stop program...\n") + + finally: + # Stop the adapter session + adapter.stop() + + return 0 + + +if __name__ == '__main__': + exit(main()) From 2cab0a2e7acf376382cb458c7ce61c536c6adf65 Mon Sep 17 00:00:00 2001 From: Christopher Peplin Date: Sat, 30 Mar 2019 12:45:47 -0400 Subject: [PATCH 2/2] Don't use Python3 type hints --- samples/subscribe_indicate_thermometer_sample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/subscribe_indicate_thermometer_sample.py b/samples/subscribe_indicate_thermometer_sample.py index 4bd2db48..e2136176 100644 --- a/samples/subscribe_indicate_thermometer_sample.py +++ b/samples/subscribe_indicate_thermometer_sample.py @@ -9,7 +9,7 @@ import pygatt -def data_handler_cb(handle: int, value: bytearray): +def data_handler_cb(handle, value): """ Indication and notification come asynchronously, we use this function to handle them either one at the time as they come.