Skip to content

Commit

Permalink
Upodated Write and Write With Response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hbldh committed Apr 12, 2019
1 parent ceee092 commit 894b5fa
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion bleak/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

__version__ = "0.4.0"
__version__ = "0.4.1a1"
15 changes: 5 additions & 10 deletions bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,39 +297,34 @@ async def read_gatt_descriptor(self, handle: int, **kwargs) -> bytearray:

async def write_gatt_char(
self, _uuid: str, data: bytearray, response: bool = False
) -> Any:
) -> None:
"""Perform a write operation on the specified GATT characteristic.
Args:
_uuid (str or UUID): The uuid of the characteristics to write to.
data (bytes or bytearray): The data to send.
response (bool): If write-with-response operation should be done. Defaults to `False`.
Returns:
None if not `response=True`, in which case a bytearray is returned.
"""
characteristic = self.services.get_characteristic(str(_uuid))
# TODO: Add OnValueUpdated handler for response=True?
await self._bus.callRemote(
characteristic.path,
"WriteValue",
interface=defs.GATT_CHARACTERISTIC_INTERFACE,
destination=defs.BLUEZ_SERVICE,
signature="aya{sv}",
body=[data, {}],
body=[data, {"type": "request" if response else "command"}],
returnSignature="",
).asFuture(self.loop)
logger.debug(
"Write Characteristic {0} | {1}: {2}".format(
_uuid, characteristic.path, data
)
)
if response:
return await self.read_gatt_char(_uuid)

async def write_gatt_descriptor(
self, handle: int, data: bytearray
) -> Any:
async def write_gatt_descriptor(self, handle: int, data: bytearray) -> None:

"""Perform a write operation on the specified GATT descriptor.
Args:
Expand Down
4 changes: 3 additions & 1 deletion bleak/backends/bluezdbus/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def characteristics(self) -> List[BleakGATTCharacteristicBlueZDBus]:
"""List of characteristics for this service"""
return self.__characteristics

def get_characteristic(self, _uuid) -> Union[BleakGATTCharacteristicBlueZDBus, None]:
def get_characteristic(
self, _uuid
) -> Union[BleakGATTCharacteristicBlueZDBus, None]:
"""Get a characteristic by UUID"""
raise NotImplementedError()

Expand Down
9 changes: 2 additions & 7 deletions bleak/backends/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,19 @@ async def read_gatt_descriptor(self, handle: int, **kwargs) -> bytearray:
@abc.abstractmethod
async def write_gatt_char(
self, _uuid: str, data: bytearray, response: bool = False
) -> Any:
) -> None:
"""Perform a write operation on the specified GATT characteristic.
Args:
_uuid (str or UUID): The uuid of the characteristics to write to.
data (bytes or bytearray): The data to send.
response (bool): If write-with-response operation should be done. Defaults to `False`.
Returns:
None if not `response=True`, in which case a bytearray is returned.
"""
raise NotImplementedError()

@abc.abstractmethod
async def write_gatt_descriptor(
self, handle: int, data: bytearray
) -> Any:
async def write_gatt_descriptor(self, handle: int, data: bytearray) -> None:
"""Perform a write operation on the specified GATT descriptor.
Args:
Expand Down
30 changes: 20 additions & 10 deletions bleak/backends/dotnet/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ async def read_gatt_char(self, _uuid: str, use_cached=False, **kwargs) -> bytear
read_result = await wrap_IAsyncOperation(
IAsyncOperation[GattReadResult](
characteristic.obj.ReadValueAsync(
BluetoothCacheMode.Cached if use_cached else BluetoothCacheMode.Uncached
BluetoothCacheMode.Cached
if use_cached
else BluetoothCacheMode.Uncached
)
),
return_type=GattReadResult,
Expand All @@ -269,7 +271,9 @@ async def read_gatt_char(self, _uuid: str, use_cached=False, **kwargs) -> bytear
)
return value

async def read_gatt_descriptor(self, handle: int, use_cached=False, **kwargs) -> bytearray:
async def read_gatt_descriptor(
self, handle: int, use_cached=False, **kwargs
) -> bytearray:
"""Perform read operation on the specified GATT descriptor.
Args:
Expand All @@ -288,7 +292,9 @@ async def read_gatt_descriptor(self, handle: int, use_cached=False, **kwargs) ->
read_result = await wrap_IAsyncOperation(
IAsyncOperation[GattReadResult](
descriptor.obj.ReadValueAsync(
BluetoothCacheMode.Cached if use_cached else BluetoothCacheMode.Uncached
BluetoothCacheMode.Cached
if use_cached
else BluetoothCacheMode.Uncached
)
),
return_type=GattReadResult,
Expand All @@ -311,7 +317,7 @@ async def read_gatt_descriptor(self, handle: int, use_cached=False, **kwargs) ->

async def write_gatt_char(
self, _uuid: str, data: bytearray, response: bool = False
) -> Any:
) -> None:
"""Perform a write operation of the specified GATT characteristic.
Args:
Expand All @@ -327,7 +333,6 @@ async def write_gatt_char(
writer = DataWriter()
writer.WriteBytes(Array[Byte](data))
if response:

write_result = await wrap_IAsyncOperation(
IAsyncOperation[GattWriteResult](
characteristic.obj.WriteValueWithResultAsync(writer.DetachBuffer())
Expand All @@ -336,6 +341,13 @@ async def write_gatt_char(
loop=self.loop,
)
status = write_result.Status
if status != GattCommunicationStatus.Success:
logger.error(
"Write-With-Results Protocol Error {0} for characteristic {1} : {2}".format(
int(write_result.ProtocolError), _uuid, data
)
)
return
else:
write_result = await wrap_IAsyncOperation(
IAsyncOperation[GattCommunicationStatus](
Expand All @@ -354,9 +366,8 @@ async def write_gatt_char(
)
)

async def write_gatt_descriptor(
self, handle: int, data: bytearray
) -> Any:
async def write_gatt_descriptor(self, handle: int, data: bytearray) -> None:

"""Perform a write operation on the specified GATT descriptor.
Args:
Expand Down Expand Up @@ -469,8 +480,7 @@ async def _start_notify(
GattCharacteristic, GattValueChangedEventArgs
](_notification_wrapper(callback))
self._bridge.AddValueChangedCallback(
characteristic_obj,
self._callbacks[characteristic_obj.Uuid.ToString()],
characteristic_obj, self._callbacks[characteristic_obj.Uuid.ToString()]
)
except Exception as e:
logger.debug("Start Notify problem: {0}".format(e))
Expand Down
1 change: 1 addition & 0 deletions bleak/backends/dotnet/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class BleakGATTServiceDotNet(BleakGATTService):
"""GATT Characteristic implementation for the .NET backend"""

def __init__(self, obj: GattDeviceService):
super().__init__(obj)
self.__characteristics = [
Expand Down

0 comments on commit 894b5fa

Please sign in to comment.