Skip to content

Commit

Permalink
CoreBluetooth: drop return True in PeripheralDelegate
Browse files Browse the repository at this point in the history
This removes `return True` from several PeripheralDelegate methods.
These methods unconditionally return `True`, so we can simplify the
code by removing it.
  • Loading branch information
dlech committed Oct 23, 2021
1 parent 30c7192 commit 3f7dd22
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 48 deletions.
16 changes: 4 additions & 12 deletions bleak/backends/corebluetooth/PeripheralDelegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async def write_characteristic(
characteristic: CBCharacteristic,
value: NSData,
response: CBCharacteristicWriteType,
) -> bool:
) -> None:
# in CoreBluetooth there is no indication of success or failure of
# CBCharacteristicWriteWithoutResponse
if response == CBCharacteristicWriteWithResponse:
Expand All @@ -180,10 +180,8 @@ async def write_characteristic(
value, characteristic, response
)

return True

@objc.python_method
async def write_descriptor(self, descriptor: CBDescriptor, value: NSData) -> bool:
async def write_descriptor(self, descriptor: CBDescriptor, value: NSData) -> None:
future = self._event_loop.create_future()

self._descriptor_write_futures[descriptor.handle()] = future
Expand All @@ -193,12 +191,10 @@ async def write_descriptor(self, descriptor: CBDescriptor, value: NSData) -> boo
finally:
del self._descriptor_write_futures[descriptor.handle()]

return True

@objc.python_method
async def start_notifications(
self, characteristic: CBCharacteristic, callback: Callable[[str, Any], Any]
) -> bool:
) -> None:
c_handle = characteristic.handle()
if c_handle in self._characteristic_notify_callbacks:
raise ValueError("Characteristic notifications already started")
Expand All @@ -214,10 +210,8 @@ async def start_notifications(
finally:
del self._characteristic_notify_change_futures[c_handle]

return True

@objc.python_method
async def stop_notifications(self, characteristic: CBCharacteristic) -> bool:
async def stop_notifications(self, characteristic: CBCharacteristic) -> None:
c_handle = characteristic.handle()
if c_handle not in self._characteristic_notify_callbacks:
raise ValueError("Characteristic notification never started")
Expand All @@ -233,8 +227,6 @@ async def stop_notifications(self, characteristic: CBCharacteristic) -> bool:

self._characteristic_notify_callbacks.pop(c_handle)

return True

@objc.python_method
async def read_rssi(self) -> NSNumber:
future = self._event_loop.create_future()
Expand Down
44 changes: 8 additions & 36 deletions bleak/backends/corebluetooth/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ async def read_gatt_char(
self,
char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID],
use_cached=False,
**kwargs
**kwargs,
) -> bytearray:
"""Perform read operation on the specified GATT characteristic.
Expand Down Expand Up @@ -316,23 +316,14 @@ async def write_gatt_char(
raise BleakError("Characteristic {} was not found!".format(char_specifier))

value = NSData.alloc().initWithBytes_length_(data, len(data))
success = await self._delegate.write_characteristic(
await self._delegate.write_characteristic(
characteristic.obj,
value,
CBCharacteristicWriteWithResponse
if response
else CBCharacteristicWriteWithoutResponse,
)
if success:
logger.debug(
"Write Characteristic {0} : {1}".format(characteristic.uuid, data)
)
else:
raise BleakError(
"Could not write value {0} to characteristic {1}: {2}".format(
data, characteristic.uuid, success
)
)
logger.debug(f"Write Characteristic {characteristic.uuid} : {data}")

async def write_gatt_descriptor(
self, handle: int, data: Union[bytes, bytearray, memoryview]
Expand All @@ -349,21 +340,14 @@ async def write_gatt_descriptor(
raise BleakError("Descriptor {} was not found!".format(handle))

value = NSData.alloc().initWithBytes_length_(data, len(data))
success = await self._delegate.write_descriptor(descriptor.obj, value)
if success:
logger.debug("Write Descriptor {0} : {1}".format(handle, data))
else:
raise BleakError(
"Could not write value {0} to descriptor {1}: {2}".format(
data, descriptor.uuid, success
)
)
await self._delegate.write_descriptor(descriptor.obj, value)
logger.debug("Write Descriptor {0} : {1}".format(handle, data))

async def start_notify(
self,
char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID],
callback: Callable[[int, bytearray], None],
**kwargs
**kwargs,
) -> None:
"""Activate notifications/indications on a characteristic.
Expand Down Expand Up @@ -398,15 +382,7 @@ def bleak_callback(s, d):
if not characteristic:
raise BleakError("Characteristic {0} not found!".format(char_specifier))

success = await self._delegate.start_notifications(
characteristic.obj, bleak_callback
)
if not success:
raise BleakError(
"Could not start notify on {0}: {1}".format(
characteristic.uuid, success
)
)
await self._delegate.start_notifications(characteristic.obj, bleak_callback)

async def stop_notify(
self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID]
Expand All @@ -427,11 +403,7 @@ async def stop_notify(
if not characteristic:
raise BleakError("Characteristic {} not found!".format(char_specifier))

success = await self._delegate.stop_notifications(characteristic.obj)
if not success:
raise BleakError(
"Could not stop notify on {0}: {1}".format(characteristic.uuid, success)
)
await self._delegate.stop_notifications(characteristic.obj)

async def get_rssi(self) -> int:
"""To get RSSI value in dBm of the connected Peripheral"""
Expand Down

0 comments on commit 3f7dd22

Please sign in to comment.