Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding mtu_size client #525

Merged
merged 10 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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