Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Multiple services/characteristics with the same UUID should not be an error #362

@AGlass0fMilk

Description

@AGlass0fMilk
  • bleak version: 0.9.1
  • Python version: 3.9.0
  • Operating System: Ubuntu 20.04
  • BlueZ version (bluetoothctl -v) in case of Linux: 5.53

Description

There is no universal requirement in the BT specification that a GATT Server cannot have multiple instances of the same service in its profile, or multiple instances of the same characteristic in a service. This restriction may be specified by a GATT Service specification, but it is not the default nature of GATT services.

Currently, bleak stores services in the client object inside a dictionary using the UUID as the key. This innately disallows multiple services with the same UUID. In fact, it raises an exception here:

def add_service(self, service: BleakGATTService):
"""Add a :py:class:`~BleakGATTService` to the service collection.
Should not be used by end user, but rather by `bleak` itself.
"""
if service.uuid not in self.__services:
self.__services[service.uuid] = service
else:
raise BleakError(
"This service is already present in this BleakGATTServiceCollection!"
)

I am implementing a DFU Service for Mbed OS and I was planning to use bleak as the platform for a desktop-based update tool. Through our discussion of the DFU Service specification, we want to allow multiple DFU Services to exist on the GATT Server so that different embedded devices within the same product may be updated over BLE.

This limitation of bleak will make it impossible to support multiple DFU services on the same GATT server. Another example of a service (one that is BT SIG specified, in fact) that allows multiple instances in this way is the common Battery Service:

ble-battery-svc

The suggested fix would be to:

  1. Change from storing any attributes (services/characteristics/descriptors, etc) in a map using their UUID as a key. The key should be the handle of the attribute.
  2. Implement new functions get_services_by_uuid(), get_characteristics_by_uuid(), and so on to return Lists of BleakGattService, BleakGattCharacteristic, ...
  3. Deprecate the get_service(), get_descriptor(), etc functions and in the mean time have them return the first occurrence of a service/descriptor by UUID (ie: return `get_services_by_uuid(uuid)[0])

Metadata

Metadata

Assignees

Labels

Backend: BlueZIssues and PRs relating to the BlueZ backendBackend: Core BluetoothIssues and PRs relating to the Core Bluetooth backendBackend: pythonnetIssues or PRs relating to the .NET/pythonnet backendbugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions