Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Allow `detection_callback` and `notifying_callback` to be asynchronous.
Add `mtu_size` and `address` properties to `Client`
  • Loading branch information
MarkusPiotrowski authored Apr 29, 2024
1 parent 00a1590 commit ce37f2b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
30 changes: 22 additions & 8 deletions bleekWare/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import asyncio
import functools
import inspect

from java import jarray, jbyte, jclass, jint, jvoid, Override, static_proxy
from java.util import UUID
Expand Down Expand Up @@ -103,8 +104,6 @@ def onMtuChanged(self, gatt, mtu, status):
"""
if status == BluetoothGatt.GATT_SUCCESS:
self.client.mtu = mtu
else:
print("Failed to read characteristic")


class Client:
Expand All @@ -126,12 +125,12 @@ def __init__(
).singletonThis

if isinstance(address_or_ble_device, BLEDevice):
self.address = address_or_ble_device.address
self._address = address_or_ble_device.address
self.device = address_or_ble_device.details
else:
self.address = address_or_ble_device
self._address = address_or_ble_device
self.device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(
self.address
self._address
)

self.disconnected_callback = (
Expand All @@ -146,6 +145,9 @@ def __init__(
self.services = []
self.mtu = 23

def __str__(self):
return f'{self.__class__.__name__}, {self.address}'

async def __aenter__(self):
await self.connect()
return self
Expand Down Expand Up @@ -179,9 +181,8 @@ async def connect(self, **kwargs):
await asyncio.sleep(0.1)
self.services = await self.get_services()

# Ask for max Mtu size
self.gatt.requestMtu(517)
await asyncio.sleep(1)
print(f'\n\nmtu: {self.mtu}\n\n')

async def disconnect(self):
"""Disconnect from connected GATT server."""
Expand Down Expand Up @@ -239,7 +240,12 @@ async def start_notify(self, char_specifier, callback, *kwargs):
while self.notification_callback:
if received_data:
data = received_data.pop()
callback(char_specifier, bytearray(data))
if inspect.iscoroutinefunction(callback):
asyncio.create_task(
callback(char_specifier, bytearray(data))
)
else:
callback(char_specifier, bytearray(data))
await asyncio.sleep(0.1)

async def stop_notify(self, char_specifier):
Expand Down Expand Up @@ -309,6 +315,14 @@ async def write_gatt_char(self, uuid, data, response=None):
def is_connected(self):
return False if self.gatt is None else True

@property
def mtu_size(self):
return self.mtu

@property
def address(self):
return self._address

def _find_characteristic(self, uuid):
"""Find and return characteristic object by UUID. PRIVATE."""
for service in self.services:
Expand Down
8 changes: 7 additions & 1 deletion bleekWare/Scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import asyncio
import inspect
import time

from java import jclass, jint, jvoid, Override, static_proxy
Expand Down Expand Up @@ -94,7 +95,12 @@ def onScanResult(self, callbackType, scanResult):

scan_result[address] = (new_device, advertisement)
if self.scanner.detection_callback:
self.scanner.detection_callback(new_device, advertisement)
if inspect.iscoroutinefunction(self.scanner.detection_callback):
asyncio.create_task(
self.scanner.detection_callback(new_device, advertisement)
)
else:
self.scanner.detection_callback(new_device, advertisement)


class AdvertisementData:
Expand Down
4 changes: 2 additions & 2 deletions bleekWare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class bleekWareError(Exception):

class bleekWareCharacteristicNotFoundError(bleekWareError):
"""A characteristic is not supported by a device."""

def __init__(self, uuid):
"""
uuid (str): UUID of the characteristic which was not found
Expand All @@ -52,7 +52,7 @@ def __init__(self, uuid):

class bleekWareDeviceNotFoundError(bleekWareError):
"""A device couldn't be found."""

def __init__(self, identifier, *args):
"""
Args:
Expand Down

0 comments on commit ce37f2b

Please sign in to comment.