Skip to content

Commit 10e318d

Browse files
authored
Merge pull request #108 from kevincar/87-write_request-get-executed-only-after-i-kill-the-server
87 write request get executed only after i kill the server
2 parents 623a56d + 3594d25 commit 10e318d

File tree

3 files changed

+88
-91
lines changed

3 files changed

+88
-91
lines changed

examples/gattserver.py

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
1-
21
"""
32
Example for a BLE 4.0 Server using a GATT dictionary of services and
43
characteristics
54
"""
6-
5+
import sys
76
import logging
87
import asyncio
98
import threading
109

11-
from typing import Any, Dict
10+
from typing import Any, Dict, Union
1211

1312
from bless import ( # type: ignore
14-
BlessServer,
15-
BlessGATTCharacteristic,
16-
GATTCharacteristicProperties,
17-
GATTAttributePermissions
18-
)
13+
BlessServer,
14+
BlessGATTCharacteristic,
15+
GATTCharacteristicProperties,
16+
GATTAttributePermissions,
17+
)
1918

2019
logging.basicConfig(level=logging.DEBUG)
2120
logger = logging.getLogger(name=__name__)
22-
trigger: threading.Event = threading.Event()
21+
22+
trigger: Union[asyncio.Event, threading.Event]
23+
if sys.platform in ["darwin", "win32"]:
24+
trigger = threading.Event()
25+
else:
26+
trigger = asyncio.Event()
2327

2428

25-
def read_request(
26-
characteristic: BlessGATTCharacteristic,
27-
**kwargs
28-
) -> bytearray:
29+
def read_request(characteristic: BlessGATTCharacteristic, **kwargs) -> bytearray:
2930
logger.debug(f"Reading {characteristic.value}")
3031
return characteristic.value
3132

3233

33-
def write_request(
34-
characteristic: BlessGATTCharacteristic,
35-
value: Any,
36-
**kwargs
37-
):
34+
def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs):
3835
characteristic.value = value
3936
logger.debug(f"Char value set to {characteristic.value}")
40-
if characteristic.value == b'\x0f':
37+
if characteristic.value == b"\x0f":
4138
logger.debug("Nice")
4239
trigger.set()
4340

@@ -47,48 +44,56 @@ async def run(loop):
4744

4845
# Instantiate the server
4946
gatt: Dict = {
50-
"A07498CA-AD5B-474E-940D-16F1FBE7E8CD": {
51-
"51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B": {
52-
"Properties": (GATTCharacteristicProperties.read |
53-
GATTCharacteristicProperties.write |
54-
GATTCharacteristicProperties.indicate),
55-
"Permissions": (GATTAttributePermissions.readable |
56-
GATTAttributePermissions.writeable),
57-
"Value": None
58-
}
59-
},
60-
"5c339364-c7be-4f23-b666-a8ff73a6a86a": {
61-
"bfc0c92f-317d-4ba9-976b-cc11ce77b4ca": {
62-
"Properties": GATTCharacteristicProperties.read,
63-
"Permissions": GATTAttributePermissions.readable,
64-
"Value": bytearray(b'\x69')
65-
}
47+
"A07498CA-AD5B-474E-940D-16F1FBE7E8CD": {
48+
"51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B": {
49+
"Properties": (
50+
GATTCharacteristicProperties.read
51+
| GATTCharacteristicProperties.write
52+
| GATTCharacteristicProperties.indicate
53+
),
54+
"Permissions": (
55+
GATTAttributePermissions.readable
56+
| GATTAttributePermissions.writeable
57+
),
58+
"Value": None,
6659
}
67-
}
60+
},
61+
"5c339364-c7be-4f23-b666-a8ff73a6a86a": {
62+
"bfc0c92f-317d-4ba9-976b-cc11ce77b4ca": {
63+
"Properties": GATTCharacteristicProperties.read,
64+
"Permissions": GATTAttributePermissions.readable,
65+
"Value": bytearray(b"\x69"),
66+
}
67+
},
68+
}
6869
my_service_name = "Test Service"
6970
server = BlessServer(name=my_service_name, loop=loop)
7071
server.read_request_func = read_request
7172
server.write_request_func = write_request
7273

7374
await server.add_gatt(gatt)
7475
await server.start()
75-
logger.debug(server.get_characteristic(
76-
"51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B"))
76+
logger.debug(server.get_characteristic("51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B"))
7777
logger.debug("Advertising")
78-
logger.info("Write '0xF' to the advertised characteristic: " +
79-
"51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B")
80-
trigger.wait()
78+
logger.info(
79+
"Write '0xF' to the advertised characteristic: "
80+
+ "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B"
81+
)
82+
if trigger.__module__ == "threading":
83+
trigger.wait()
84+
else:
85+
await trigger.wait()
8186
await asyncio.sleep(2)
8287
logger.debug("Updating")
83-
server.get_characteristic("51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B").value = (
84-
bytearray(b"i")
85-
)
88+
server.get_characteristic("51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B").value = bytearray(
89+
b"i"
90+
)
8691
server.update_value(
87-
"A07498CA-AD5B-474E-940D-16F1FBE7E8CD",
88-
"51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B"
89-
)
92+
"A07498CA-AD5B-474E-940D-16F1FBE7E8CD", "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B"
93+
)
9094
await asyncio.sleep(5)
9195
await server.stop()
9296

97+
9398
loop = asyncio.get_event_loop()
9499
loop.run_until_complete(run(loop))

examples/server.py

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
1-
21
"""
32
Example for a BLE 4.0 Server
43
"""
5-
4+
import sys
65
import logging
76
import asyncio
87
import threading
98

10-
from typing import Any
9+
from typing import Any, Union
1110

1211
from bless import ( # type: ignore
13-
BlessServer,
14-
BlessGATTCharacteristic,
15-
GATTCharacteristicProperties,
16-
GATTAttributePermissions
17-
)
12+
BlessServer,
13+
BlessGATTCharacteristic,
14+
GATTCharacteristicProperties,
15+
GATTAttributePermissions,
16+
)
1817

1918
logging.basicConfig(level=logging.DEBUG)
2019
logger = logging.getLogger(name=__name__)
21-
trigger: threading.Event = threading.Event()
20+
21+
# NOTE: Some systems require different synchronization methods.
22+
trigger: Union[asyncio.Event, threading.Event]
23+
if sys.platform in ["darwin", "win32"]:
24+
trigger = threading.Event()
25+
else:
26+
trigger = asyncio.Event()
2227

2328

24-
def read_request(
25-
characteristic: BlessGATTCharacteristic,
26-
**kwargs
27-
) -> bytearray:
29+
def read_request(characteristic: BlessGATTCharacteristic, **kwargs) -> bytearray:
2830
logger.debug(f"Reading {characteristic.value}")
2931
return characteristic.value
3032

3133

32-
def write_request(
33-
characteristic: BlessGATTCharacteristic,
34-
value: Any,
35-
**kwargs
36-
):
34+
def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs):
3735
characteristic.value = value
3836
logger.debug(f"Char value set to {characteristic.value}")
39-
if characteristic.value == b'\x0f':
37+
if characteristic.value == b"\x0f":
4038
logger.debug("NICE")
4139
trigger.set()
4240

@@ -56,38 +54,31 @@ async def run(loop):
5654
# Add a Characteristic to the service
5755
my_char_uuid = "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B"
5856
char_flags = (
59-
GATTCharacteristicProperties.read |
60-
GATTCharacteristicProperties.write |
61-
GATTCharacteristicProperties.indicate
62-
)
63-
permissions = (
64-
GATTAttributePermissions.readable |
65-
GATTAttributePermissions.writeable
66-
)
57+
GATTCharacteristicProperties.read
58+
| GATTCharacteristicProperties.write
59+
| GATTCharacteristicProperties.indicate
60+
)
61+
permissions = GATTAttributePermissions.readable | GATTAttributePermissions.writeable
6762
await server.add_new_characteristic(
68-
my_service_uuid,
69-
my_char_uuid,
70-
char_flags,
71-
None,
72-
permissions)
73-
74-
logger.debug(
75-
server.get_characteristic(
76-
my_char_uuid
77-
)
78-
)
63+
my_service_uuid, my_char_uuid, char_flags, None, permissions
64+
)
65+
66+
logger.debug(server.get_characteristic(my_char_uuid))
7967
await server.start()
8068
logger.debug("Advertising")
8169
logger.info(f"Write '0xF' to the advertised characteristic: {my_char_uuid}")
82-
trigger.wait()
70+
if trigger.__module__ == "threading":
71+
trigger.wait()
72+
else:
73+
await trigger.wait()
74+
8375
await asyncio.sleep(2)
8476
logger.debug("Updating")
8577
server.get_characteristic(my_char_uuid)
86-
server.update_value(
87-
my_service_uuid, "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B"
88-
)
78+
server.update_value(my_service_uuid, "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B")
8979
await asyncio.sleep(5)
9080
await server.stop()
9181

82+
9283
loop = asyncio.get_event_loop()
9384
loop.run_until_complete(run(loop))

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
install_requires=[
1919
"bleak",
2020
"pywin32;platform_system=='Windows'",
21-
"pysetupdi @ git+https://github.com/gwangyi/pysetupdi#egg=pysetupdi;platform_system=='Windows'" # noqa: E501
21+
"dbus_next;platform_system=='Linux'",
22+
"pysetupdi @ git+https://github.com/gwangyi/pysetupdi#egg=pysetupdi;platform_system=='Windows'", # noqa: E501
2223
],
2324
classifiers=[
2425
"Programming Language :: Python :: 3",
2526
"License :: OSI Approved :: MIT License",
2627
"Operating System :: OS Independent",
2728
],
28-
python_requires='>=3.7',
29+
python_requires=">=3.7",
2930
)

0 commit comments

Comments
 (0)