Skip to content

Commit

Permalink
Adding mtu_size client property (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
es617 authored May 14, 2021
1 parent fabdc9d commit 62f45b0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
Added
~~~~~

* add mtu_size property for clients
* WinRT backend added
* Added ``BleakScanner.discovered_devices`` property.

Expand Down
8 changes: 8 additions & 0 deletions bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,14 @@ def is_connected(self) -> bool:
False if self._bus is None else self._properties.get("Connected", False)
)

@property
def mtu_size(self) -> int:
"""Get ATT MTU size for active connection"""
warnings.warn(
"MTU size not supported with BlueZ; this function returns the default value of 23. Note that the actual MTU size might be larger"
)
return 23

# GATT services methods

async def get_services(self, **kwargs) -> BleakGATTServiceCollection:
Expand Down
15 changes: 14 additions & 1 deletion bleak/backends/corebluetooth/PeripheralDelegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
NSData,
NSError,
)
from CoreBluetooth import CBCharacteristicWriteWithResponse
from CoreBluetooth import (
CBCharacteristicWriteWithResponse,
CBCharacteristicWriteWithoutResponse,
)

from bleak.exc import BleakError

Expand Down Expand Up @@ -129,6 +132,16 @@ async def readCharacteristic_(
else:
return b""

def getMtuSize(self) -> int:
"""Use type CBCharacteristicWriteWithoutResponse to get maximum write value length based on the
the negotiated ATT MTU size. Add the ATT header length (+3) to get the actual ATT MTU size"""
return (
self.peripheral.maximumWriteValueLengthForType_(
CBCharacteristicWriteWithoutResponse
)
+ 3
)

async def readDescriptor_(
self, descriptor: CBDescriptor, use_cached=True
) -> NSData:
Expand Down
6 changes: 6 additions & 0 deletions bleak/backends/corebluetooth/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ def is_connected(self) -> bool:
False if manager is None else manager.isConnected
)

@property
def mtu_size(self) -> int:
"""Get ATT MTU size for active connection"""
manager = self._central_manager_delegate
return manager.connected_peripheral_delegate.getMtuSize()

async def pair(self, *args, **kwargs) -> bool:
"""Attempt to pair with a peripheral.
Expand Down
5 changes: 5 additions & 0 deletions bleak/backends/dotnet/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ def is_connected(self) -> bool:
else self._requester.ConnectionStatus == BluetoothConnectionStatus.Connected
)

@property
def mtu_size(self) -> int:
"""Get ATT MTU size for active connection"""
return self._session.MaxPduSize

async def pair(self, protection_level=None, **kwargs) -> bool:
"""Attempts to pair with the device.
Expand Down
5 changes: 5 additions & 0 deletions bleak/backends/winrt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ def is_connected(self) -> bool:
== BluetoothConnectionStatus.CONNECTED
)

@property
def mtu_size(self) -> int:
"""Get ATT MTU size for active connection"""
return self._session.max_pdu_size

async def pair(self, protection_level: int = None, **kwargs) -> bool:
"""Attempts to pair with the device.
Expand Down

0 comments on commit 62f45b0

Please sign in to comment.