1
-
2
1
"""
3
2
Example for a BLE 4.0 Server using a GATT dictionary of services and
4
3
characteristics
5
4
"""
6
-
5
+ import sys
7
6
import logging
8
7
import asyncio
9
8
import threading
10
9
11
- from typing import Any , Dict
10
+ from typing import Any , Dict , Union
12
11
13
12
from bless import ( # type: ignore
14
- BlessServer ,
15
- BlessGATTCharacteristic ,
16
- GATTCharacteristicProperties ,
17
- GATTAttributePermissions
18
- )
13
+ BlessServer ,
14
+ BlessGATTCharacteristic ,
15
+ GATTCharacteristicProperties ,
16
+ GATTAttributePermissions ,
17
+ )
19
18
20
19
logging .basicConfig (level = logging .DEBUG )
21
20
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 ()
23
27
24
28
25
- def read_request (
26
- characteristic : BlessGATTCharacteristic ,
27
- ** kwargs
28
- ) -> bytearray :
29
+ def read_request (characteristic : BlessGATTCharacteristic , ** kwargs ) -> bytearray :
29
30
logger .debug (f"Reading { characteristic .value } " )
30
31
return characteristic .value
31
32
32
33
33
- def write_request (
34
- characteristic : BlessGATTCharacteristic ,
35
- value : Any ,
36
- ** kwargs
37
- ):
34
+ def write_request (characteristic : BlessGATTCharacteristic , value : Any , ** kwargs ):
38
35
characteristic .value = value
39
36
logger .debug (f"Char value set to { characteristic .value } " )
40
- if characteristic .value == b' \x0f ' :
37
+ if characteristic .value == b" \x0f " :
41
38
logger .debug ("Nice" )
42
39
trigger .set ()
43
40
@@ -47,48 +44,56 @@ async def run(loop):
47
44
48
45
# Instantiate the server
49
46
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 ,
66
59
}
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
+ }
68
69
my_service_name = "Test Service"
69
70
server = BlessServer (name = my_service_name , loop = loop )
70
71
server .read_request_func = read_request
71
72
server .write_request_func = write_request
72
73
73
74
await server .add_gatt (gatt )
74
75
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" ))
77
77
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 ()
81
86
await asyncio .sleep (2 )
82
87
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
+ )
86
91
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
+ )
90
94
await asyncio .sleep (5 )
91
95
await server .stop ()
92
96
97
+
93
98
loop = asyncio .get_event_loop ()
94
99
loop .run_until_complete (run (loop ))
0 commit comments