Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bleak connection for MacOS 12+ #116

Merged
merged 1 commit into from
Feb 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions pylgbst/comms/cbleak.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import asyncio
import logging
import platform
import queue
import threading
import time

import bleak

from pylgbst.comms import Connection, MOVE_HUB_HW_UUID_CHAR
from pylgbst.comms import Connection, MOVE_HUB_HW_UUID_CHAR, MOVE_HUB_HW_UUID_SERV

log = logging.getLogger('comms-bleak')

Expand Down Expand Up @@ -57,7 +58,11 @@ def enable_notifications(self):

async def _bleak_thread(self):
bleak = BleakConnection()
await bleak.connect(self.hub_mac, self.hub_name)
# For MacOS 12+ the service_uuids kwarg is required for scanning
kwargs = None
if "Darwin" == platform.system() and int(platform.mac_ver()[0].split(".")[0]) >= 12:
kwargs = {"service_uuids" : [MOVE_HUB_HW_UUID_SERV]}
await bleak.connect(self.hub_mac, self.hub_name, **kwargs)
await bleak.set_notify_handler((self._safe_handler, self.resp_queue))
# After connecting, need to send any data or hub will drop the connection,
# below command is Advertising name request update
Expand Down Expand Up @@ -129,17 +134,20 @@ def __init__(self):
logging.getLogger('bleak.backends.dotnet.client').setLevel(logging.WARNING)
logging.getLogger('bleak.backends.bluezdbus.client').setLevel(logging.WARNING)

async def connect(self, hub_mac=None, hub_name=None):
async def connect(self, hub_mac=None, hub_name=None, **kwargs):
"""
Connect to device.

:param hub_mac: Optional Lego HUB MAC to connect to.
:param hub_mac: Optional Lego HUB MAC to connect to
:param hub_name: Optional Lego Hub name to connect to
:kwargs: Optional parameters for bleak.discover

:raises ConnectionError: When cannot connect to given MAC or name matching fails.
:return: None
"""
log.info("Discovering devices... Press green button on Hub")
for i in range(0, 30):
devices = await bleak.discover(timeout=1)
devices = await bleak.discover(timeout=1, **kwargs)
log.debug("Devices: %s", devices)
for dev in devices:
log.debug(dev)
Expand All @@ -156,7 +164,7 @@ async def connect(self, hub_mac=None, hub_name=None):
else:
raise ConnectionError('Device not found.')

self._client = bleak.BleakClient(self._device.address)
self._client = bleak.BleakClient(self._device)
status = await self._client.connect()
log.debug('Connection status: {status}'.format(status=status))

Expand Down