Skip to content

Commit 2a40a43

Browse files
authored
Merge pull request #167 from akiller/master
Add optional primary flag when creating a service.
2 parents 78da60f + 13a2287 commit 2a40a43

File tree

9 files changed

+62
-22
lines changed

9 files changed

+62
-22
lines changed

bless/backends/bluezdbus/dbus/application.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ def _ensure_bluez_available() -> None:
9090
LOGGER.warning(message)
9191
raise RuntimeError("BlueZ is not available on this system")
9292

93-
async def add_service(self, uuid: str) -> BlueZGattService: # noqa: F821
93+
async def add_service(
94+
self, uuid: str, primary: Optional[bool] = None
95+
) -> BlueZGattService: # noqa: F821
9496
"""
9597
Add a service to the application
9698
The first service to be added will be the primary service
@@ -99,15 +101,18 @@ async def add_service(self, uuid: str) -> BlueZGattService: # noqa: F821
99101
----------
100102
uuid : str
101103
The string representation of the uuid for the service to create
104+
primary : Optional[bool]
105+
True if this is a primary service, False otherwise. If None,
106+
the first service added will be primary, all others will be secondary
102107
103108
Returns
104109
-------
105110
BlueZGattService
106111
Returns and instance of the service object
107112
"""
108113
index: int = len(self.services) + 1
109-
primary: bool = index == 1
110-
service: BlueZGattService = BlueZGattService(uuid, primary, index, self)
114+
is_primary: bool = primary if primary is not None else index == 1
115+
service: BlueZGattService = BlueZGattService(uuid, is_primary, index, self)
111116
self.services.append(service)
112117
self.bus.export(service.path, service)
113118
return service

bless/backends/bluezdbus/server.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,21 @@ async def is_advertising(self) -> bool:
156156
await self.setup_task
157157
return await self.app.is_advertising(self.adapter)
158158

159-
async def add_new_service(self, uuid: str):
159+
async def add_new_service(self, uuid: str, primary: Optional[bool] = None):
160160
"""
161161
Add a new GATT service to be hosted by the server
162162
163163
Parameters
164164
----------
165165
uuid : str
166166
The UUID for the service to add
167+
primary : Optional[bool]
168+
True if this is a primary service, False otherwise. If None,
169+
default behavior of the backend is used which is only the first
170+
service added is primary.
167171
"""
168172
await self.setup_task
169-
service: BlessGATTServiceBlueZDBus = BlessGATTServiceBlueZDBus(uuid)
173+
service: BlessGATTServiceBlueZDBus = BlessGATTServiceBlueZDBus(uuid, primary)
170174
await service.init(self)
171175
self.services[service.uuid] = service
172176

bless/backends/bluezdbus/service.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from uuid import UUID
2-
from typing import Mapping, Union, cast, TYPE_CHECKING
2+
from typing import Mapping, Optional, Union, cast, TYPE_CHECKING
33

44
from bleak.backends.service import BleakGATTService # type: ignore
55
from bless.backends.bluezdbus.dbus.service import BlueZGattService
@@ -17,16 +17,20 @@ class BlessGATTServiceBlueZDBus(BaseBlessGATTService, BleakGATTService):
1717
GATT service implementation for the BlueZ backend
1818
"""
1919

20-
def __init__(self, uuid: Union[str, UUID]):
20+
def __init__(self, uuid: Union[str, UUID], primary: Optional[bool] = None):
2121
"""
2222
Initialize the Bless GATT Service
2323
2424
Parameters
2525
----------
2626
uuid : Union[str, UUID]
2727
The UUID to assign to the service
28+
primary : Optional[bool]
29+
True if this is a primary service, False otherwise. If None,
30+
default behavior of the backend is used which is only the first
31+
service added is primary.
2832
"""
29-
BaseBlessGATTService.__init__(self, uuid)
33+
BaseBlessGATTService.__init__(self, uuid, primary)
3034
self._characteristics: Mapping[int, BlessGATTCharacteristic] = (
3135
{} # type: ignore[assignment]
3236
)
@@ -43,7 +47,9 @@ async def init(self, server: "BaseBlessServer"):
4347
The server to assign the service to
4448
"""
4549
bluez_server: "BlessServerBlueZDBus" = cast("BlessServerBlueZDBus", server)
46-
gatt_service: BlueZGattService = await bluez_server.app.add_service(self._uuid)
50+
gatt_service: BlueZGattService = await bluez_server.app.add_service(
51+
self._uuid, primary=self._primary
52+
)
4753

4854
# Store the BlueZ GATT service
4955
self.gatt = gatt_service

bless/backends/corebluetooth/server.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,22 @@ async def is_advertising(self) -> bool:
171171
"""
172172
return self.peripheral_manager_delegate.is_advertising() == 1
173173

174-
async def add_new_service(self, uuid: str):
174+
async def add_new_service(self, uuid: str, primary: Optional[bool] = None):
175175
"""
176176
Add a service and all it's characteristics to be advertised
177177
178178
Parameters
179179
----------
180180
uuid : str
181181
The string representation of the UUID of the service to be added
182+
primary : Optional[bool]
183+
True if this is a primary service, False otherwise. If None, default
184+
behavior of the backend is used which is that all services are primary.
182185
"""
183186
logger.debug("Creating a new service with uuid: {}".format(uuid))
184-
service: BlessGATTServiceCoreBluetooth = BlessGATTServiceCoreBluetooth(uuid)
187+
service: BlessGATTServiceCoreBluetooth = BlessGATTServiceCoreBluetooth(
188+
uuid, primary
189+
)
185190
await service.init(self)
186191
self.services[service.uuid] = service
187192

bless/backends/corebluetooth/service.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from uuid import UUID
2-
from typing import Union, Dict
2+
from typing import Optional, Union, Dict
33

44
from CoreBluetooth import CBMutableService, CBUUID # type: ignore
55

@@ -16,16 +16,19 @@ class BlessGATTServiceCoreBluetooth(BaseBlessGATTService, BleakGATTService):
1616
GATT Characteristic implementation for the CoreBluetooth backend
1717
"""
1818

19-
def __init__(self, uuid: Union[str, UUID]):
19+
def __init__(self, uuid: Union[str, UUID], primary: Optional[bool] = None):
2020
"""
2121
New Bless Service for macOS
2222
2323
Parameters
2424
----------
2525
uuid: Union[str, UUID]
2626
The uuid to assign to the service
27+
primary : Optional[bool]
28+
True if this is a primary service, False otherwise. If None, default
29+
behavior of the backend is used which is that all services are primary.
2730
"""
28-
BaseBlessGATTService.__init__(self, uuid)
31+
BaseBlessGATTService.__init__(self, uuid, primary)
2932
self.__handle = 0
3033
self._characteristics: Dict[int, BlessGATTCharacteristic] = {} # type: ignore
3134
self._cb_service = None
@@ -36,7 +39,7 @@ async def init(self, server: "BaseBlessServer"):
3639
"""
3740
service_uuid: CBUUID = CBUUID.alloc().initWithString_(self._uuid)
3841
cb_service: CBMutableService = CBMutableService.alloc().initWithType_primary_(
39-
service_uuid, True
42+
service_uuid, True if self._primary is None else self._primary
4043
)
4144

4245
# Store the CoreBluetooth service

bless/backends/server.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,17 @@ async def is_advertising(self) -> bool:
105105
raise NotImplementedError()
106106

107107
@abc.abstractmethod
108-
async def add_new_service(self, uuid: str):
108+
async def add_new_service(self, uuid: str, primary: Optional[bool] = None):
109109
"""
110110
Add a new GATT service to be hosted by the server
111111
112112
Parameters
113113
----------
114114
uuid : str
115115
The UUID for the service to add
116+
primary : Optional[bool]
117+
True if this is a primary service, False otherwise. If None,
118+
default behavior of the backend is used.
116119
"""
117120
raise NotImplementedError()
118121

bless/backends/service.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import abc
22

33
from uuid import UUID
4-
from typing import List, Union, cast, TYPE_CHECKING
4+
from typing import Optional, List, Union, cast, TYPE_CHECKING
55
from bleak.backends.service import BleakGATTService # type: ignore
66

77
if TYPE_CHECKING:
@@ -14,7 +14,7 @@ class BlessGATTService(BleakGATTService):
1414
GATT Service object for Bless
1515
"""
1616

17-
def __init__(self, uuid: Union[str, UUID]):
17+
def __init__(self, uuid: Union[str, UUID], primary: Optional[bool] = None):
1818
"""
1919
Instantiates a new GATT Service but is not yet assigned to any
2020
application
@@ -23,11 +23,15 @@ def __init__(self, uuid: Union[str, UUID]):
2323
----------
2424
uuid : Union[str, UUID]
2525
The uuid of the service
26+
primary : Optional[bool]
27+
True if this is a primary service, False otherwise. If None, default
28+
behavior of the backend is used.
2629
"""
2730
if type(uuid) is str:
2831
uuid_str: str = cast(str, uuid)
2932
uuid = UUID(uuid_str)
3033
self._uuid: str = str(uuid)
34+
self._primary = primary
3135
self._characteristics: dict[int, BlessGATTCharacteristic] = {} # type: ignore
3236

3337
@abc.abstractmethod

bless/backends/winrt/server.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,23 @@ def _status_update(
232232
if args is not None and args.status == 2:
233233
self._advertising_started.set()
234234

235-
async def add_new_service(self, uuid: str):
235+
async def add_new_service(self, uuid: str, primary: Optional[bool] = None):
236236
"""
237237
Generate a new service to be associated with the server
238238
239239
Parameters
240240
----------
241241
uuid : str
242242
The string representation of the UUID of the service to be added
243+
primary : Optional[bool]
244+
True if this is a primary service, False otherwise. If None,
245+
default behavior of the backend is used.
246+
For WinRT, it seems to only allow primary services to be added so
247+
this is currently unused.
243248
"""
244249
logger.debug("Creating a new service with uuid: {}".format(uuid))
245250
logger.debug("Adding service to server with uuid {}".format(uuid))
246-
service: BlessGATTServiceWinRT = BlessGATTServiceWinRT(uuid)
251+
service: BlessGATTServiceWinRT = BlessGATTServiceWinRT(uuid, primary)
247252
await service.init(self)
248253
self.services[service.uuid] = service
249254

bless/backends/winrt/service.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,21 @@ class BlessGATTServiceWinRT(BaseBlessGATTService, BleakGATTService):
3232
GATT Characteristic implementation for the WinRT backend
3333
"""
3434

35-
def __init__(self, uuid: Union[str, UUID]):
35+
def __init__(self, uuid: Union[str, UUID], primary: Optional[bool] = None):
3636
"""
3737
Initialize the Bless GATT Service object
3838
3939
Parameters
4040
----------
4141
uuid: Union[str, UUID]
4242
The UUID to assign to the service
43+
primary : Optional[bool]
44+
True if this is a primary service, False otherwise. If None, default
45+
behavior of the backend is used.
46+
For WinRT, it seems to only allow primary services to be added so this
47+
is currently unused.
4348
"""
44-
BaseBlessGATTService.__init__(self, uuid)
49+
BaseBlessGATTService.__init__(self, uuid, primary)
4550
self.service_provider: Optional[GattServiceProvider] = None
4651
self._local_service: Optional[GattLocalService] = None
4752
self._characteristics: Dict[int, BlessGATTCharacteristic] = {} # type: ignore

0 commit comments

Comments
 (0)