-
Notifications
You must be signed in to change notification settings - Fork 277
/
client.py
967 lines (781 loc) · 34.7 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
# -*- coding: utf-8 -*-
"""
BLE Client for Windows 10 systems, implemented with WinRT.
Created on 2020-08-19 by hbldh <henrik.blidh@nedomkull.com>
"""
import asyncio
import functools
import logging
import sys
import uuid
import warnings
from ctypes import pythonapi
from typing import Any, Dict, List, Optional, Sequence, Union, cast
import async_timeout
if sys.version_info[:2] < (3, 8):
from typing_extensions import Literal, TypedDict
else:
from typing import Literal, TypedDict
from bleak_winrt.windows.devices.bluetooth import (
BluetoothAddressType,
BluetoothCacheMode,
BluetoothError,
BluetoothLEDevice,
)
from bleak_winrt.windows.devices.bluetooth.genericattributeprofile import (
GattCharacteristic,
GattCharacteristicProperties,
GattClientCharacteristicConfigurationDescriptorValue,
GattCommunicationStatus,
GattDescriptor,
GattDeviceService,
GattSession,
GattSessionStatus,
GattSessionStatusChangedEventArgs,
GattValueChangedEventArgs,
GattWriteOption,
)
from bleak_winrt.windows.devices.enumeration import (
DeviceInformation,
DevicePairingKinds,
DevicePairingResultStatus,
DeviceUnpairingResultStatus,
)
from bleak_winrt.windows.foundation import (
AsyncStatus,
EventRegistrationToken,
IAsyncOperation,
)
from bleak_winrt.windows.storage.streams import Buffer
from ... import BleakScanner
from ...exc import PROTOCOL_ERROR_CODES, BleakDeviceNotFoundError, BleakError
from ..characteristic import BleakGATTCharacteristic
from ..client import BaseBleakClient, NotifyCallback
from ..device import BLEDevice
from ..service import BleakGATTServiceCollection
from .characteristic import BleakGATTCharacteristicWinRT
from .descriptor import BleakGATTDescriptorWinRT
from .scanner import BleakScannerWinRT
from .service import BleakGATTServiceWinRT
logger = logging.getLogger(__name__)
_ACCESS_DENIED_SERVICES = list(
uuid.UUID(u)
for u in ("00001812-0000-1000-8000-00805f9b34fb",) # Human Interface Device Service
)
# TODO: we can use this when minimum Python is 3.8
# class _Result(typing.Protocol):
# status: GattCommunicationStatus
# protocol_error: typing.Optional[int]
def _address_to_int(address: str) -> int:
"""Converts the Bluetooth device address string to its representing integer
Args:
address (str): Bluetooth device address to convert
Returns:
int: integer representation of the given Bluetooth device address
"""
_address_separators = [":", "-"]
for char in _address_separators:
address = address.replace(char, "")
return int(address, base=16)
def _ensure_success(result: Any, attr: Optional[str], fail_msg: str) -> Any:
"""
Ensures that *status* is ``GattCommunicationStatus.SUCCESS``, otherwise
raises ``BleakError``.
Args:
result: The result returned by a WinRT API method.
attr: The name of the attribute containing the result.
fail_msg: A message to include in the exception.
"""
status = result.status if hasattr(result, "status") else result
if status == GattCommunicationStatus.SUCCESS:
return None if attr is None else getattr(result, attr)
if status == GattCommunicationStatus.PROTOCOL_ERROR:
err = PROTOCOL_ERROR_CODES.get(result.protocol_error, "Unknown")
raise BleakError(
f"{fail_msg}: Protocol Error 0x{result.protocol_error:02X}: {err}"
)
if status == GattCommunicationStatus.ACCESS_DENIED:
raise BleakError(f"{fail_msg}: Access Denied")
if status == GattCommunicationStatus.UNREACHABLE:
raise BleakError(f"{fail_msg}: Unreachable")
raise BleakError(f"{fail_msg}: Unexpected status code 0x{result.status:02X}")
class WinRTClientArgs(TypedDict, total=False):
"""
Windows-specific arguments for :class:`BleakClient`.
"""
address_type: Literal["public", "random"]
"""
Can either be ``"public"`` or ``"random"``, depending on the required address
type needed to connect to your device.
"""
use_cached_services: bool
"""
``True`` allows Windows to fetch the services, characteristics and descriptors
from the Windows cache instead of reading them from the device. Can be very
much faster for known, unchanging devices, but not recommended for DIY peripherals
where the GATT layout can change between connections.
``False`` will force the attribute database to be read from the remote device
instead of using the OS cache.
If omitted, the OS Bluetooth stack will do what it thinks is best.
"""
class BleakClientWinRT(BaseBleakClient):
"""Native Windows Bleak Client.
Implemented using `winrt <https://github.com/Microsoft/xlang/tree/master/src/package/pywinrt/projection>`_,
a package that enables Python developers to access Windows Runtime APIs directly from Python.
Args:
address_or_ble_device (str or BLEDevice): The Bluetooth address of the BLE peripheral
to connect to or the ``BLEDevice`` object representing it.
winrt (dict): A dictionary of Windows-specific configuration values.
**timeout (float): Timeout for required ``BleakScanner.find_device_by_address`` call. Defaults to 10.0.
**disconnected_callback (callable): Callback that will be scheduled in the
event loop when the client is disconnected. The callable must take one
argument, which will be this client object.
"""
def __init__(
self,
address_or_ble_device: Union[BLEDevice, str],
*,
winrt: WinRTClientArgs,
**kwargs,
):
super(BleakClientWinRT, self).__init__(address_or_ble_device, **kwargs)
# Backend specific. WinRT objects.
if isinstance(address_or_ble_device, BLEDevice):
self._device_info = address_or_ble_device.details.adv.bluetooth_address
else:
self._device_info = None
self._requester: Optional[BluetoothLEDevice] = None
self._services_changed_events: List[asyncio.Event] = []
self._session_active_events: List[asyncio.Event] = []
self._session_closed_events: List[asyncio.Event] = []
self._session: GattSession = None
self._notification_callbacks: Dict[int, NotifyCallback] = {}
if "address_type" in kwargs:
warnings.warn(
"The address_type keyword arg will in a future version be moved into the win dict input instead.",
PendingDeprecationWarning,
stacklevel=2,
)
# os-specific options
self._use_cached_services = winrt.get("use_cached_services")
self._address_type = winrt.get("address_type", kwargs.get("address_type"))
self._session_services_changed_token: Optional[EventRegistrationToken] = None
self._session_status_changed_token: Optional[EventRegistrationToken] = None
self._max_pdu_size_changed_token: Optional[EventRegistrationToken] = None
def __str__(self):
return f"{type(self).__name__} ({self.address})"
# Connectivity methods
async def _create_requester(self, bluetooth_address: int) -> BluetoothLEDevice:
args = [
bluetooth_address,
]
if self._address_type is not None:
args.append(
BluetoothAddressType.PUBLIC
if self._address_type == "public"
else BluetoothAddressType.RANDOM
)
requester = await BluetoothLEDevice.from_bluetooth_address_async(*args)
# https://github.com/microsoft/Windows-universal-samples/issues/1089#issuecomment-487586755
if requester is None:
raise BleakDeviceNotFoundError(
self.address, f"Device with address {self.address} was not found."
)
return requester
async def connect(self, **kwargs) -> bool:
"""Connect to the specified GATT server.
Keyword Args:
timeout (float): Timeout for required ``BleakScanner.find_device_by_address`` call. Defaults to 10.0.
Returns:
Boolean representing connection status.
"""
# Try to find the desired device.
timeout = kwargs.get("timeout", self._timeout)
if self._device_info is None:
device = await BleakScanner.find_device_by_address(
self.address, timeout=timeout, backend=BleakScannerWinRT
)
if device is None:
raise BleakDeviceNotFoundError(
self.address, f"Device with address {self.address} was not found."
)
self._device_info = device.details.adv.bluetooth_address
logger.debug("Connecting to BLE device @ %s", self.address)
loop = asyncio.get_running_loop()
self._requester = await self._create_requester(self._device_info)
def handle_services_changed():
if not self._services_changed_events:
logger.warning("%s: unhandled services changed event", self.address)
else:
for event in self._services_changed_events:
event.set()
def services_changed_handler(sender, args):
logger.debug("%s: services changed", self.address)
loop.call_soon_threadsafe(handle_services_changed)
self._services_changed_token = self._requester.add_gatt_services_changed(
services_changed_handler
)
# Called on disconnect event or on failure to connect.
def handle_disconnect():
if self._requester:
if self._services_changed_token:
self._requester.remove_gatt_services_changed(
self._services_changed_token
)
self._services_changed_token = None
self._requester.close()
self._requester = None
if self._session:
if self._session_status_changed_token:
self._session.remove_session_status_changed(
self._session_status_changed_token
)
self._session_status_changed_token = None
if self._max_pdu_size_changed_token:
self._session.remove_max_pdu_size_changed(
self._max_pdu_size_changed_token
)
self._max_pdu_size_changed_token = None
self._session.close()
self._session = None
def handle_session_status_changed(
args: GattSessionStatusChangedEventArgs,
):
if args.error != BluetoothError.SUCCESS:
logger.error(f"Unhandled GATT error {args.error}")
if args.status == GattSessionStatus.ACTIVE:
for e in self._session_active_events:
e.set()
elif args.status == GattSessionStatus.CLOSED:
if self._disconnected_callback:
self._disconnected_callback(self)
for e in self._session_closed_events:
e.set()
handle_disconnect()
# this is the WinRT event handler will be called on another thread
def session_status_changed_event_handler(
sender: GattSession, args: GattSessionStatusChangedEventArgs
):
logger.debug(
"session_status_changed_event_handler: id: %s, error: %s, status: %s",
sender.device_id,
args.error,
args.status,
)
loop.call_soon_threadsafe(handle_session_status_changed, args)
def max_pdu_size_changed_handler(sender: GattSession, args):
try:
max_pdu_size = sender.max_pdu_size
except OSError:
# There is a race condition where this event was already
# queued when the GattSession object was closed. In that
# case, we get a Windows error which we can just ignore.
return
logger.debug("max_pdu_size_changed_handler: %d", max_pdu_size)
# Start a GATT Session to connect
event = asyncio.Event()
self._session_active_events.append(event)
try:
self._session = await GattSession.from_device_id_async(
self._requester.bluetooth_device_id
)
if not self._session.can_maintain_connection:
raise BleakError("device does not support GATT sessions")
self._session_status_changed_token = (
self._session.add_session_status_changed(
session_status_changed_event_handler
)
)
self._max_pdu_size_changed_token = self._session.add_max_pdu_size_changed(
max_pdu_size_changed_handler
)
# Windows does not support explicitly connecting to a device.
# Instead it has the concept of a GATT session that is owned
# by the calling program.
self._session.maintain_connection = True
# This keeps the device connected until we set maintain_connection = False.
wait_connect_task = asyncio.create_task(event.wait())
try:
# a connection may not be made until we request info from the
# device, so we have to get services before the GATT session
# is set to active
wait_get_services_task = asyncio.create_task(self.get_services())
try:
# wait for the session to become active
async with async_timeout(timeout):
await asyncio.gather(wait_connect_task, wait_get_services_task)
finally:
wait_get_services_task.cancel()
finally:
wait_connect_task.cancel()
except BaseException:
handle_disconnect()
raise
finally:
self._session_active_events.remove(event)
return True
async def disconnect(self) -> bool:
"""Disconnect from the specified GATT server.
Returns:
Boolean representing if device is disconnected.
"""
logger.debug("Disconnecting from BLE device...")
# Remove notifications.
for handle, event_handler_token in list(self._notification_callbacks.items()):
char = self.services.get_characteristic(handle)
char.obj.remove_value_changed(event_handler_token)
self._notification_callbacks.clear()
# Dispose all service components that we have requested and created.
for service in self.services:
service.obj.close()
self.services = BleakGATTServiceCollection()
self._services_resolved = False
# Without this, disposing the BluetoothLEDevice won't disconnect it
if self._session:
self._session.maintain_connection = False
# calling self._session.close() here prevents any further GATT
# session status events, so we defer that until after the session
# is no longer active
# Dispose of the BluetoothLEDevice and see that the session
# status is now closed.
if self._requester:
event = asyncio.Event()
self._session_closed_events.append(event)
try:
self._requester.close()
# sometimes it can take over one minute before Windows decides
# to end the GATT session/disconnect the device
async with async_timeout.timeout(120):
await event.wait()
finally:
self._session_closed_events.remove(event)
return True
@property
def is_connected(self) -> bool:
"""Check connection status between this client and the server.
Returns:
Boolean representing connection status.
"""
return self._DeprecatedIsConnectedReturn(
False
if self._session is None
else self._session.session_status == GattSessionStatus.ACTIVE
)
@property
def mtu_size(self) -> int:
"""Get ATT MTU size for active connection"""
return self._session.max_pdu_size
async def pair(self, protection_level: int = None, **kwargs) -> bool:
"""Attempts to pair with the device.
Keyword Args:
protection_level (int): A ``DevicePairingProtectionLevel`` enum value:
1. None - Pair the device using no levels of protection.
2. Encryption - Pair the device using encryption.
3. EncryptionAndAuthentication - Pair the device using
encryption and authentication. (This will not work in Bleak...)
Returns:
Boolean regarding success of pairing.
"""
# New local device information object created since the object from the requester isn't updated
device_information = await DeviceInformation.create_from_id_async(
self._requester.device_information.id
)
if (
device_information.pairing.can_pair
and not device_information.pairing.is_paired
):
# Currently only supporting Just Works solutions...
ceremony = DevicePairingKinds.CONFIRM_ONLY
custom_pairing = device_information.pairing.custom
def handler(sender, args):
args.accept()
pairing_requested_token = custom_pairing.add_pairing_requested(handler)
try:
if protection_level:
pairing_result = await custom_pairing.pair_async(
ceremony, protection_level
)
else:
pairing_result = await custom_pairing.pair_async(ceremony)
except Exception as e:
raise BleakError("Failure trying to pair with device!") from e
finally:
custom_pairing.remove_pairing_requested(pairing_requested_token)
if pairing_result.status not in (
DevicePairingResultStatus.PAIRED,
DevicePairingResultStatus.ALREADY_PAIRED,
):
raise BleakError(f"Could not pair with device: {pairing_result.status}")
else:
logger.info(
"Paired to device with protection level %d.",
pairing_result.protection_level_used,
)
return True
else:
return device_information.pairing.is_paired
async def unpair(self) -> bool:
"""Attempts to unpair from the device.
N.B. unpairing also leads to disconnection in the Windows backend.
Returns:
Boolean on whether the unparing was successful.
"""
device = await self._create_requester(
self._device_info
if self._device_info is not None
else _address_to_int(self.address)
)
try:
unpairing_result = await device.device_information.pairing.unpair_async()
if unpairing_result.status not in (
DeviceUnpairingResultStatus.UNPAIRED,
DeviceUnpairingResultStatus.ALREADY_UNPAIRED,
):
raise BleakError(
f"Could not unpair with device: {unpairing_result.status}"
)
logger.info("Unpaired with device.")
finally:
device.close()
return True
# GATT services methods
async def get_services(self, **kwargs) -> BleakGATTServiceCollection:
"""Get all services registered for this GATT server.
Returns:
A :py:class:`bleak.backends.service.BleakGATTServiceCollection` with this device's services tree.
"""
# Return the Service Collection.
if self._services_resolved:
return self.services
logger.debug("Get Services...")
# Each of the get_serv/char/desc_async() methods has two forms, one
# with no args and one with a cache_mode argument
args = []
# If the os-specific use_cached_services arg was given when BleakClient
# was created, the we use the second form with explicit cache mode.
# Otherwise we use the first form with no explicit cache mode which
# allows the OS Bluetooth stack to decide what is best.
if self._use_cached_services is not None:
args.append(
BluetoothCacheMode.CACHED
if self._use_cached_services
else BluetoothCacheMode.UNCACHED
)
# if we receive a services changed event before get_gatt_services_async()
# finishes, we need to call it again with BluetoothCacheMode.UNCACHED
# to ensure we have the correct services as described in
# https://learn.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothledevice.gattserviceschanged
while True:
services_changed_event = asyncio.Event()
services_changed_event_task = asyncio.create_task(
services_changed_event.wait()
)
self._services_changed_events.append(services_changed_event)
get_services_task = FutureLike(
self._requester.get_gatt_services_async(*args)
)
try:
await asyncio.wait(
[services_changed_event_task, get_services_task],
return_when=asyncio.FIRST_COMPLETED,
)
finally:
services_changed_event_task.cancel()
self._services_changed_events.remove(services_changed_event)
get_services_task.cancel()
if not services_changed_event.is_set():
break
logger.debug(
"%s: restarting get services due to services changed event",
self.address,
)
args = [BluetoothCacheMode.CACHED]
services: Sequence[GattDeviceService] = _ensure_success(
get_services_task.result(),
"services",
"Could not get GATT services",
)
for service in services:
# Windows returns an ACCESS_DENIED error when trying to enumerate
# characteristics of services used by the OS, like the HID service
# so we have to exclude those services.
if service.uuid in _ACCESS_DENIED_SERVICES:
continue
self.services.add_service(BleakGATTServiceWinRT(service))
characteristics: Sequence[GattCharacteristic] = _ensure_success(
await service.get_characteristics_async(*args),
"characteristics",
f"Could not get GATT characteristics for {service}",
)
for characteristic in characteristics:
self.services.add_characteristic(
BleakGATTCharacteristicWinRT(
characteristic, self._session.max_pdu_size - 3
)
)
descriptors: Sequence[GattDescriptor] = _ensure_success(
await characteristic.get_descriptors_async(*args),
"descriptors",
f"Could not get GATT descriptors for {service}",
)
for descriptor in descriptors:
self.services.add_descriptor(
BleakGATTDescriptorWinRT(
descriptor,
str(characteristic.uuid),
characteristic.attribute_handle,
)
)
self._services_resolved = True
return self.services
# I/O methods
async def read_gatt_char(
self,
char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID],
**kwargs,
) -> bytearray:
"""Perform read operation on the specified GATT characteristic.
Args:
char_specifier (BleakGATTCharacteristic, int, str or UUID): The characteristic to read from,
specified by either integer handle, UUID or directly by the
BleakGATTCharacteristic object representing it.
Keyword Args:
use_cached (bool): ``False`` forces Windows to read the value from the
device again and not use its own cached value. Defaults to ``False``.
Returns:
(bytearray) The read data.
"""
if not self.is_connected:
raise BleakError("Not connected")
use_cached = kwargs.get("use_cached", False)
if not isinstance(char_specifier, BleakGATTCharacteristic):
characteristic = self.services.get_characteristic(char_specifier)
else:
characteristic = char_specifier
if not characteristic:
raise BleakError(f"Characteristic {char_specifier} was not found!")
value = bytearray(
_ensure_success(
await characteristic.obj.read_value_async(
BluetoothCacheMode.CACHED
if use_cached
else BluetoothCacheMode.UNCACHED
),
"value",
f"Could not read characteristic handle {characteristic.handle}",
)
)
logger.debug("Read Characteristic %04X : %s", characteristic.handle, value)
return value
async def read_gatt_descriptor(self, handle: int, **kwargs) -> bytearray:
"""Perform read operation on the specified GATT descriptor.
Args:
handle (int): The handle of the descriptor to read from.
Keyword Args:
use_cached (bool): `False` forces Windows to read the value from the
device again and not use its own cached value. Defaults to `False`.
Returns:
(bytearray) The read data.
"""
if not self.is_connected:
raise BleakError("Not connected")
use_cached = kwargs.get("use_cached", False)
descriptor = self.services.get_descriptor(handle)
if not descriptor:
raise BleakError(f"Descriptor with handle {handle} was not found!")
value = bytearray(
_ensure_success(
await descriptor.obj.read_value_async(
BluetoothCacheMode.CACHED
if use_cached
else BluetoothCacheMode.UNCACHED
),
"value",
f"Could not read Descriptor value for {handle:04X}",
)
)
logger.debug("Read Descriptor %04X : %s", handle, value)
return value
async def write_gatt_char(
self,
char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID],
data: Union[bytes, bytearray, memoryview],
response: bool = False,
) -> None:
"""Perform a write operation of the specified GATT characteristic.
Args:
char_specifier (BleakGATTCharacteristic, int, str or UUID): The characteristic to write
to, specified by either integer handle, UUID or directly by the
BleakGATTCharacteristic object representing it.
data (bytes or bytearray): The data to send.
response (bool): If write-with-response operation should be done. Defaults to `False`.
"""
if not self.is_connected:
raise BleakError("Not connected")
if not isinstance(char_specifier, BleakGATTCharacteristic):
characteristic = self.services.get_characteristic(char_specifier)
else:
characteristic = char_specifier
if not characteristic:
raise BleakError(f"Characteristic {char_specifier} was not found!")
response = (
GattWriteOption.WRITE_WITH_RESPONSE
if response
else GattWriteOption.WRITE_WITHOUT_RESPONSE
)
buf = Buffer(len(data))
buf.length = buf.capacity
with memoryview(buf) as mv:
mv[:] = data
_ensure_success(
await characteristic.obj.write_value_with_result_async(buf, response),
None,
f"Could not write value {data} to characteristic {characteristic.handle:04X}",
)
async def write_gatt_descriptor(
self, handle: int, data: Union[bytes, bytearray, memoryview]
) -> None:
"""Perform a write operation on the specified GATT descriptor.
Args:
handle (int): The handle of the descriptor to read from.
data (bytes or bytearray): The data to send.
"""
if not self.is_connected:
raise BleakError("Not connected")
descriptor = self.services.get_descriptor(handle)
if not descriptor:
raise BleakError(f"Descriptor with handle {handle} was not found!")
buf = Buffer(len(data))
buf.length = buf.capacity
with memoryview(buf) as mv:
mv[:] = data
_ensure_success(
await descriptor.obj.write_value_with_result_async(buf),
None,
f"Could not write value {data} to descriptor {handle:04X}",
)
logger.debug("Write Descriptor %04X : %s", handle, data)
async def start_notify(
self,
characteristic: BleakGATTCharacteristic,
callback: NotifyCallback,
**kwargs,
) -> None:
"""
Activate notifications/indications on a characteristic.
Keyword Args:
force_indicate (bool): If this is set to True, then Bleak will set up a indication request instead of a
notification request, given that the characteristic supports notifications as well as indications.
"""
winrt_char = cast(GattCharacteristic, characteristic.obj)
# If we want to force indicate even when notify is available, also check if the device
# actually supports indicate as well.
if not kwargs.get("force_indicate", False) and (
winrt_char.characteristic_properties & GattCharacteristicProperties.NOTIFY
):
cccd = GattClientCharacteristicConfigurationDescriptorValue.NOTIFY
elif (
winrt_char.characteristic_properties & GattCharacteristicProperties.INDICATE
):
cccd = GattClientCharacteristicConfigurationDescriptorValue.INDICATE
else:
raise BleakError(
"characteristic does not support notifications or indications"
)
loop = asyncio.get_running_loop()
def handle_value_changed(
sender: GattCharacteristic, args: GattValueChangedEventArgs
):
value = bytearray(args.characteristic_value)
return loop.call_soon_threadsafe(callback, value)
event_handler_token = winrt_char.add_value_changed(handle_value_changed)
self._notification_callbacks[characteristic.handle] = event_handler_token
try:
_ensure_success(
await winrt_char.write_client_characteristic_configuration_descriptor_async(
cccd
),
None,
f"Could not start notify on {characteristic.handle:04X}",
)
except BaseException:
# This usually happens when a device reports that it supports indicate,
# but it actually doesn't.
if characteristic.handle in self._notification_callbacks:
event_handler_token = self._notification_callbacks.pop(
characteristic.handle
)
winrt_char.remove_value_changed(event_handler_token)
raise
async def stop_notify(
self, char_specifier: Union[BleakGATTCharacteristic, int, str, uuid.UUID]
) -> None:
"""Deactivate notification/indication on a specified characteristic.
Args:
char_specifier (BleakGATTCharacteristic, int, str or UUID): The characteristic to deactivate
notification/indication on, specified by either integer handle, UUID or
directly by the BleakGATTCharacteristic object representing it.
"""
if not self.is_connected:
raise BleakError("Not connected")
if not isinstance(char_specifier, BleakGATTCharacteristic):
characteristic = self.services.get_characteristic(char_specifier)
else:
characteristic = char_specifier
if not characteristic:
raise BleakError(f"Characteristic {char_specifier} not found!")
_ensure_success(
await characteristic.obj.write_client_characteristic_configuration_descriptor_async(
GattClientCharacteristicConfigurationDescriptorValue.NONE
),
None,
f"Could not stop notify on {characteristic.handle:04X}",
)
event_handler_token = self._notification_callbacks.pop(characteristic.handle)
characteristic.obj.remove_value_changed(event_handler_token)
class FutureLike:
"""
Wraps a WinRT IAsyncOperation in a "future-like" object so that it can
be passed to Python APIs.
Needed until https://github.com/pywinrt/pywinrt/issues/14
"""
_asyncio_future_blocking = True
def __init__(self, async_result: IAsyncOperation) -> None:
self._async_result = async_result
self._callbacks = []
self._loop = asyncio.get_running_loop()
def call_callbacks(op: IAsyncOperation, status: AsyncStatus):
for c in self._callbacks:
c(self)
async_result.completed = functools.partial(
self._loop.call_soon_threadsafe, call_callbacks
)
def result(self) -> Any:
return self._async_result.get_results()
def done(self) -> bool:
return self._async_result.status != AsyncStatus.STARTED
def cancelled(self) -> bool:
return self._async_result.status == AsyncStatus.CANCELED
def add_done_callback(self, callback, *, context=None) -> None:
self._callbacks.append(callback)
def remove_done_callback(self, callback) -> None:
self._callbacks.remove(callback)
def cancel(self, msg=None) -> bool:
if self._async_result.status != AsyncStatus.STARTED:
return False
self._async_result.cancel()
return True
def exception(self) -> Optional[Exception]:
if self._async_result.status == AsyncStatus.STARTED:
raise asyncio.InvalidStateError
if self._async_result.status == AsyncStatus.COMPLETED:
return None
if self._async_result.status == AsyncStatus.CANCELED:
raise asyncio.CancelledError
if self._async_result.status == AsyncStatus.ERROR:
try:
pythonapi.PyErr_SetFromWindowsErr(self._async_result.error_code)
except OSError as e:
return e
def get_loop(self) -> asyncio.AbstractEventLoop:
return self._loop