mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
* Cosmetics * Harmonograph demo * Cleanup * Original * Original * Cosmetics * Original file * Fixes * cosmetics * separate classes * Cosmetics * Cosmetics * fix tests * Remove plotter tests * Add bluegiga * Rename it * Progress * Fix tests * Cosmetics * Found a way for pygatt! * Playing with gatt * Fix hung subscribe * rename class * add test * skeleton for autodetect * safer order * Fix tests * Fix test * Add dbus install * another try * 2 * 3 * 34 * 6 * 7 * Isolate some tests * 8 * back to roots * Try more * 9 * Help * rep * site-packs * Fix? * Py3 come on * dbus * busss * dev null! * Fix test * Cleanup * Fix tests * Fix after review * add package * FIx package paths * Cosmetics * Update * More doc
62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
import logging
|
|
|
|
import pygatt
|
|
|
|
from pylgbst.comms import Connection, LEGO_MOVE_HUB
|
|
from pylgbst.constants import MOVE_HUB_HW_UUID_CHAR
|
|
from pylgbst.utilities import str2hex
|
|
|
|
log = logging.getLogger('comms-pygatt')
|
|
|
|
|
|
class GattoolConnection(Connection):
|
|
"""
|
|
Used for connecting to
|
|
|
|
:type _conn_hnd: pygatt.backends.bgapi.device.BGAPIBLEDevice
|
|
"""
|
|
|
|
def __init__(self, controller='hci0'):
|
|
Connection.__init__(self)
|
|
self.backend = lambda: pygatt.GATTToolBackend(hci_device=controller)
|
|
self._conn_hnd = None
|
|
|
|
def connect(self, hub_mac=None):
|
|
log.debug("Trying to connect client to MoveHub with MAC: %s", hub_mac)
|
|
adapter = self.backend()
|
|
adapter.start()
|
|
|
|
while not self._conn_hnd:
|
|
log.info("Discovering devices...")
|
|
devices = adapter.scan(1)
|
|
log.debug("Devices: %s", devices)
|
|
|
|
for dev in devices:
|
|
address = dev['address']
|
|
name = dev['name']
|
|
if name == LEGO_MOVE_HUB or hub_mac == address:
|
|
logging.info("Found %s at %s", name, address)
|
|
self._conn_hnd = adapter.connect(address)
|
|
break
|
|
|
|
if self._conn_hnd:
|
|
break
|
|
|
|
return self
|
|
|
|
def disconnect(self):
|
|
self._conn_hnd.disconnect()
|
|
|
|
def write(self, handle, data):
|
|
log.debug("Writing to handle %s: %s", handle, str2hex(data))
|
|
return self._conn_hnd.char_write_handle(handle, bytearray(data))
|
|
|
|
def set_notify_handler(self, handler):
|
|
self._conn_hnd.subscribe(MOVE_HUB_HW_UUID_CHAR, handler)
|
|
|
|
|
|
class BlueGigaConnection(GattoolConnection):
|
|
def __init__(self):
|
|
super(BlueGigaConnection, self).__init__()
|
|
self.backend = lambda: pygatt.GATTToolBackend()
|