1-
21"""
32Example for a BLE 4.0 Server using a GATT dictionary of services and
43characteristics
54"""
6-
5+ import sys
76import logging
87import asyncio
98import threading
109
11- from typing import Any , Dict
10+ from typing import Any , Dict , Union
1211
1312from bless import ( # type: ignore
14- BlessServer ,
15- BlessGATTCharacteristic ,
16- GATTCharacteristicProperties ,
17- GATTAttributePermissions
18- )
13+ BlessServer ,
14+ BlessGATTCharacteristic ,
15+ GATTCharacteristicProperties ,
16+ GATTAttributePermissions ,
17+ )
1918
2019logging .basicConfig (level = logging .DEBUG )
2120logger = 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+
9398loop = asyncio .get_event_loop ()
9499loop .run_until_complete (run (loop ))
0 commit comments