Bluetooth Rabbit Hole Again

Trying to loop back in to zoneminder but my mind got distracted by Bluetooth again.

This time trying to get python to talk back and forth with my BLE device.

Learned a lot today, biggest was that something is wrong when running it from CentOS, and things work right when running from a Raspberry Pi 0 W.

So if you are following this, do it from a Pi.

Run the following command from your pi

gatttool -b ff:ff:c1:aa:bb:cc -I

Replace the MAC with whatever your MAC is for your device.

Bah, this is going to take too long here is the code.

#!/usr/bin/python3
import pygatt
from binascii import hexlify

adapter = pygatt.GATTToolBackend()

def handle_data(handle, value):
   print("Received data: %s" % hexlify(value))

try:
   adapter.start()
   device = adapter.connect('ff:ff:c1:aa:bb:cc')
   print("Device Connected")
   device.subscribe("0000ffe1-0000-1000-8000-00805f9b34fb",callback=handle_data,indication=False,wait_for_response=False)
   command = input("# ")
   while (command != "q"):
      print (command)
      if (command == "a"):
         #char-write-cmd 0x0b 01
         device.char_write("00002a06-0000-1000-8000-00805f9b34fb",bytearray([0x01,0x01]),wait_for_response=False)
      if (command == "b"):
         #char-write-cmd 0x0b 00
         device.char_write("00002a06-0000-1000-8000-00805f9b34fb",bytearray([0x00,0x00]),wait_for_response=False)
command = input("# ")

finally:
   adapter.stop()

Weight: 313.6

This entry was posted in Raspberry Pi, Technical, Training, Virtualization, Weigh In. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.