From b16d7c3a6f90cdb70381dff1c351a49fca59a46b Mon Sep 17 00:00:00 2001 From: Henrik Blidh Date: Fri, 2 Aug 2019 19:39:43 +0200 Subject: [PATCH] Final modifications for #3 before 0.5.0 release. Implemented new method stumps (due to abstract methods) from #86 in macOS and Windows backends as well. Modified example files to show how Core Bluetooth backend handles addresses. Removed corebleak.py, since I do not think it is needed any more either. --- bleak/backends/corebluetooth/client.py | 13 +++++++++++++ bleak/backends/corebluetooth/corebleak.py | 14 -------------- bleak/backends/dotnet/client.py | 13 +++++++++++++ examples/enable_notifications.py | 7 ++++++- examples/get_services.py | 7 ++++++- examples/sensortag.py | 2 +- examples/service_explorer.py | 2 +- 7 files changed, 40 insertions(+), 18 deletions(-) delete mode 100644 bleak/backends/corebluetooth/corebleak.py diff --git a/bleak/backends/corebluetooth/client.py b/bleak/backends/corebluetooth/client.py index cb46d153..6f2d7781 100644 --- a/bleak/backends/corebluetooth/client.py +++ b/bleak/backends/corebluetooth/client.py @@ -78,6 +78,19 @@ async def is_connected(self) -> bool: """Checks for current active connection""" return cbapp.central_manager_delegate.isConnected + def set_disconnected_callback( + self, callback: Callable[[BaseBleakClient], None], **kwargs + ) -> None: + """Set the disconnected callback. + + N.B. This is not implemented in the Core Bluetooth backend yet. + + Args: + callback: callback to be called on disconnection. + + """ + raise NotImplementedError("This is not implemented in the Core Bluetooth backend yet") + async def get_services(self) -> BleakGATTServiceCollection: """Get all services registered for this GATT server. diff --git a/bleak/backends/corebluetooth/corebleak.py b/bleak/backends/corebluetooth/corebleak.py deleted file mode 100644 index 4a71f8bc..00000000 --- a/bleak/backends/corebluetooth/corebleak.py +++ /dev/null @@ -1,14 +0,0 @@ -import os - -import objc - -THIS_FILE = os.path.realpath(__file__) -DIR_PATH = os.path.dirname(THIS_FILE) -FRAMEWORK_PATH = os.path.join(DIR_PATH, "corebleak.framework") - -__bundle__ = objc.initFrameworkWrapper( - "corebleak.framework", - frameworkIdentifier="com.kcdvs.corebleak", - frameworkPath=objc.pathForFramework(FRAMEWORK_PATH), - globals=globals(), -) diff --git a/bleak/backends/dotnet/client.py b/bleak/backends/dotnet/client.py index a3567f9b..1e484bc9 100644 --- a/bleak/backends/dotnet/client.py +++ b/bleak/backends/dotnet/client.py @@ -187,6 +187,19 @@ async def is_connected(self) -> bool: else: return False + def set_disconnected_callback( + self, callback: Callable[[BaseBleakClient], None], **kwargs + ) -> None: + """Set the disconnected callback. + + N.B. This is not implemented in the .NET backend yet. + + Args: + callback: callback to be called on disconnection. + + """ + raise NotImplementedError("This is not implemented in the .NET backend yet") + # GATT services methods async def get_services(self) -> BleakGATTServiceCollection: diff --git a/examples/enable_notifications.py b/examples/enable_notifications.py index dc6ce0a6..dda2cf75 100644 --- a/examples/enable_notifications.py +++ b/examples/enable_notifications.py @@ -11,6 +11,7 @@ import logging import asyncio +import platform from bleak import BleakClient from bleak import _logger as logger @@ -51,6 +52,10 @@ async def run(address, loop, debug=False): import os os.environ["PYTHONASYNCIODEBUG"] = str(1) - address = "24:71:89:cc:09:05" # <--- Change to your device's address + address = ( + "24:71:89:cc:09:05" # <--- Change to your device's address here if you are using Windows or Linux + if platform.system() != "Darwin" + else "243E23AE-4A99-406C-B317-18F1BD7B4CBE" # <--- Change to your device's address here if you are using macOS + ) loop = asyncio.get_event_loop() loop.run_until_complete(run(address, loop, True)) diff --git a/examples/get_services.py b/examples/get_services.py index afc9884d..746aef61 100644 --- a/examples/get_services.py +++ b/examples/get_services.py @@ -9,6 +9,7 @@ """ import asyncio +import platform from bleak import BleakClient @@ -19,6 +20,10 @@ async def print_services(mac_addr: str, loop: asyncio.AbstractEventLoop): print("Services:", svcs) -mac_addr = "ff:50:35:82:3b:5a" +mac_addr = ( + "24:71:89:cc:09:05" + if platform.system() != "Darwin" + else "243E23AE-4A99-406C-B317-18F1BD7B4CBE" +) loop = asyncio.get_event_loop() loop.run_until_complete(print_services(mac_addr, loop)) diff --git a/examples/sensortag.py b/examples/sensortag.py index 82ce4cc1..dbd153ab 100644 --- a/examples/sensortag.py +++ b/examples/sensortag.py @@ -94,7 +94,7 @@ async def run(address, loop, debug=False): if debug: import sys - # loop.set_debug(True) + loop.set_debug(True) l = logging.getLogger("asyncio") l.setLevel(logging.DEBUG) h = logging.StreamHandler(sys.stdout) diff --git a/examples/service_explorer.py b/examples/service_explorer.py index 9cc6e6af..753343bf 100644 --- a/examples/service_explorer.py +++ b/examples/service_explorer.py @@ -20,7 +20,7 @@ async def run(address, loop, debug=False): if debug: import sys - # loop.set_debug(True) + loop.set_debug(True) log.setLevel(logging.DEBUG) h = logging.StreamHandler(sys.stdout) h.setLevel(logging.DEBUG)