Skip to content

Commit

Permalink
Merge pull request #55 from kevincar/develop
Browse files Browse the repository at this point in the history
Develop new version 0.2.2
  • Loading branch information
kevincar authored Jul 18, 2021
2 parents 8cca9cc + 2567eea commit 8db03e3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bless/backends/bluezdbus/characteristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def init(self, service: "BlessGATTServiceBlueZDBus"):
# Add a Bleak Characteristic properties
self.gatt = gatt_char
super(BlessGATTCharacteristic, self).__init__(
dict_obj, gatt_char.path, service.uuid
dict_obj, gatt_char.path, service.uuid, 0
)

@property
Expand Down
3 changes: 2 additions & 1 deletion bless/backends/bluezdbus/dbus/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def __init__(
app : BlueZApp
A BlueZApp object that owns this service
"""
self.path: str = app.base_path + "/service" + str(index)
hex_index: str = hex(index)[2:].rjust(4, "0")
self.path: str = app.base_path + "/service" + hex_index
self.bus: client = app.bus
self.destination: str = app.destination
self.uuid: str = uuid
Expand Down
29 changes: 9 additions & 20 deletions bless/backends/corebluetooth/PeripheralManagerDelegate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import objc # type: ignore
import logging
import asyncio
import threading

from typing import Any, Dict, List, Optional, Callable

Expand Down Expand Up @@ -68,10 +69,14 @@ def init(self):
self._callbacks: Dict[str, Callable] = {}

# Events
self._powered_on_event: asyncio.Event = asyncio.Event()
self._powered_on_event: threading.Event = threading.Event()
self._advertisement_started_event: asyncio.Event = asyncio.Event()
self._services_added_events: Dict[str, asyncio.Event] = {}

# Documentation requires that no calls be made until we can validate
# that the bluetooth module is powered on
self._powered_on_event.wait()

self._central_subscriptions = {}

if not self.compliant():
Expand All @@ -96,18 +101,6 @@ def compliant(self) -> bool:
self.CBPeripheralManagerDelegate
)

@objc.python_method
async def wait_for_powered_on(self, timeout: float):
"""
Wait for ready state of the peripheralManager
Parameters
----------
timeout : float
How long to wait for the powered on event
"""
await asyncio.wait_for(self._powered_on_event.wait(), timeout)

def is_connected(self) -> bool:
"""
Determin whether any centrals have subscribed
Expand Down Expand Up @@ -217,8 +210,9 @@ def write_request_func(self, func: Callable):

# Protocol functions

@objc.python_method
def did_update_state(self, peripheral_manager: CBPeripheralManager):
def peripheralManagerDidUpdateState_( # noqa: N802
self, peripheral_manager: CBPeripheralManager
):
if peripheral_manager.state() == CBManagerStateUnknown:
logger.debug("Cannot detect bluetooth device")
elif peripheral_manager.state() == CBManagerStateResetting:
Expand All @@ -238,11 +232,6 @@ def did_update_state(self, peripheral_manager: CBPeripheralManager):
self._powered_on_event.clear()
self._advertisement_started_event.clear()

def peripheralManagerDidUpdateState_( # noqa: N802
self, peripheral_manager: CBPeripheralManager
):
self.event_loop.call_soon_threadsafe(self.did_update_state, peripheral_manager)

def peripheralManager_willRestoreState_( # noqa: N802
self, peripheral: CBPeripheralManager, d: dict
):
Expand Down
10 changes: 5 additions & 5 deletions bless/backends/corebluetooth/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
CBService,
CBPeripheralManager,
CBMutableCharacteristic,
CBAdvertisementDataLocalNameKey,
CBAdvertisementDataServiceUUIDsKey,
)

from bleak.backends.service import BleakGATTService # type: ignore
Expand Down Expand Up @@ -69,8 +71,6 @@ async def start(self, timeout: float = 10, **kwargs):
Floating point decimal in seconds for how long to wait for the
on-board bluetooth module to power on
"""
await self.peripheral_manager_delegate.wait_for_powered_on(timeout)

for service_uuid in self.services:
bleak_service: BleakGATTService = self.services[service_uuid]
service_obj: CBService = bleak_service.obj
Expand All @@ -81,10 +81,10 @@ async def start(self, timeout: float = 10, **kwargs):
raise BlessError("Callback functions must be initialized first")

advertisement_data = {
"kCBAdvDataServiceUUIDs": list(
CBAdvertisementDataLocalNameKey: self.name,
CBAdvertisementDataServiceUUIDsKey: list(
map(lambda x: self.services[x].obj.UUID(), self.services)
),
"kCBAdvDataLocalName": self.name,
)
}
logger.debug("Advertisement Data: {}".format(advertisement_data))
try:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="bless",
version="0.2.1",
version="0.2.2",
author="Kevin Davis",
author_email="[email protected]",
description="A Bluetooth Low Energy Server supplement to Bleak",
Expand Down

0 comments on commit 8db03e3

Please sign in to comment.