webbluetooth-windows 0.0.5

Windows BLE from pure Rust — WinRT reached through hand-written COM, no bindings crate
Documentation
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
//! The Windows engine: WinRT, reached through hand-written COM.
//!
//! Closest in shape to the Linux BlueZ backend, because WinRT is also
//! request/reply: `ReadValueAsync` returns the bytes in its own completion,
//! so there is no read-versus-notification ambiguity and no correlation FIFO.
//!
//! # Connecting is implicit
//!
//! WinRT has no `Connect`. `BluetoothLEDevice.FromBluetoothAddressAsync` hands
//! back a device object whether or not the radio has a link, and the link is
//! established by the first thing that needs it — here, service discovery. So
//! `Inner::connect` resolves the device and discovers its services, and
//! treats *that* as the connection.
//!
//! # One visible platform difference
//!
//! `BluetoothDevice::id` is the peer's Bluetooth address, as on Linux and
//! Android. Apple never exposes one.

use crate::ble;
use crate::com::{ComPtr, Delegate};
use crate::guid::{Guid, Signature};
use crate::iids::{classes, generics, interfaces, slots};
use crate::winrt;
use futures_channel::oneshot;
use std::collections::{BTreeSet, HashMap};
use std::sync::{Arc, Mutex, Weak};
use webbluetooth_core::chooser::DeviceChooser;
use webbluetooth_core::error::{Availability, Error, Result};
use webbluetooth_core::filter::{Advertisement, RequestDeviceOptions};
use webbluetooth_core::gatt::{CharacteristicProperties, WriteType};
use webbluetooth_core::registry::DeviceRegistry;
use webbluetooth_core::scan::{self, ScanHub};
use webbluetooth_core::state::ManagerState;
use webbluetooth_core::uuid::BluetoothUuid;

/// `GattCommunicationStatus.Success`.
const GATT_SUCCESS: i32 = 0;
/// `GattClientCharacteristicConfigurationDescriptorValue`.
const CCCD_NONE: i32 = 0;
const CCCD_NOTIFY: i32 = 1;
const CCCD_INDICATE: i32 = 2;
/// `GattWriteOption`.
const WRITE_WITH_RESPONSE: i32 = 0;
const WRITE_WITHOUT_RESPONSE: i32 = 1;
/// `BluetoothLEScanningMode.Active` — asks for scan responses, which carry the
/// name most peripherals leave out of the advertisement itself.
const SCANNING_MODE_ACTIVE: i32 = 1;

/// The signature of a runtime class, for computing a handler's IID.
/// The signature of `DeviceInformationCollection`.
///
/// Built here rather than taken from the generated table because its default
/// interface is `IVectorView<DeviceInformation>` — parameterised, so it has no
/// fixed IID to extract and the generator skips the class entirely. The IID is
/// computed the same way the runtime computes it: hash the generic's GUID
/// together with its argument's signature.
fn device_information_collection() -> Signature {
    let element = Signature::Parameterized {
        generic: generics::I_VECTOR_VIEW,
        arguments: vec![class_signature(classes::DEVICE_INFORMATION)],
    };
    Signature::Class {
        name: "Windows.Devices.Enumeration.DeviceInformationCollection",
        default_interface: element
            .iid()
            .expect("a parameterised signature always has an IID"),
    }
}

/// The ATT MTU every LE link is guaranteed to carry before negotiation.
const ATT_DEFAULT_MTU: u16 = 23;

fn class_signature(class: classes::Class) -> Signature {
    Signature::Class {
        name: class.0,
        default_interface: class.1,
    }
}

/// A handle into a device's attribute table.
#[derive(Clone, Debug)]
pub struct Handle(Arc<HandleData>);

#[derive(Debug)]
struct HandleData {
    object: ComPtr,
    uuid: BluetoothUuid,
    properties: u32,
    /// The last value read or notified.
    ///
    /// Every other platform hands this back from its own cache —
    /// `CBCharacteristic.value`, BlueZ's `Value` property, Android's
    /// `getValue()`. WinRT keeps nothing, so it is kept here, on the handle
    /// rather than in a map beside it: a map would have to be keyed by the COM
    /// pointer, and a freed characteristic's address can be handed to the next
    /// one, which would serve one characteristic's value for another. Living
    /// on the handle, the entry cannot outlive the object it describes.
    value: Mutex<Option<Vec<u8>>>,
}

impl Handle {
    fn object(&self) -> &ComPtr {
        &self.0.object
    }
}

pub fn attribute_uuid(handle: &Handle) -> Result<BluetoothUuid> {
    Ok(handle.0.uuid)
}

#[allow(dead_code)]
#[derive(Debug, Default, Clone)]
pub struct RestoredScan {
    pub device_ids: Vec<String>,
    pub scan_services: Vec<BluetoothUuid>,
    pub scan_allow_duplicates: bool,
}

/// What this backend needs to reach a device.
struct DeviceData {
    address: u64,
    /// `BluetoothLEDevice`, once resolved.
    device: Option<ComPtr>,
    /// Services discovered on connect; WinRT caches them behind the same call,
    /// but each round trip is an IPC hop worth avoiding.
    services: Vec<(BluetoothUuid, ComPtr)>,
}

/// The last thing an advertisement said about a device.
struct Sighting {
    address: u64,
    name: Option<String>,
    rssi: i32,
    at: std::time::Instant,
}

pub struct Inner {
    /// `false` when WinRT could not be reached at all.
    available: bool,
    /// Everyone watching the scan; the watcher is shared and reference-counted.
    hub: ScanHub,
    /// What each advertisement told us, kept so the chooser's answer is still
    /// resolvable after it returns — and so signal strength can be reported at
    /// all. See [`Inner::read_rssi`].
    sightings: Mutex<HashMap<String, Sighting>>,
    /// The advertisement watcher, and the delegate it calls.
    watcher: Mutex<Option<(ComPtr, ComPtr)>>,
    devices: DeviceRegistry<DeviceData>,
    /// Subscribers per characteristic, and the handler keeping them fed.
    notifications: Mutex<HashMap<usize, Vec<webbluetooth_core::backlog::Sender<Vec<u8>>>>>,
    value_handlers: Mutex<HashMap<usize, ComPtr>>,
    /// The `GattServicesChanged` delegate per device, held so it outlives the
    /// registration. Keyed by device id rather than pointer: one per device.
    services_changed_handlers: Mutex<HashMap<String, ComPtr>>,
    /// Handed to event handlers so they can reach the registry without owning
    /// the engine — a strong reference here would be a cycle that never frees.
    self_ref: Weak<Inner>,
    /// The controller `select_adapter` chose, if one was chosen.
    selected: Mutex<Option<String>>,
    /// `GattSession.MaxPduSize` per device, read once when the link comes up.
    pdu_sizes: Mutex<HashMap<String, u16>>,
}

#[cfg(all(feature = "classic", target_os = "windows"))]
impl Inner {
    /// Enumerate Classic devices through Win32, independent of WinRT LE scans.
    pub async fn classic_devices(&self) -> Result<Vec<webbluetooth_core::ClassicDevice>> {
        crate::classic_discovery::discover().map_err(Error::Network)
    }
}

impl Inner {
    pub fn new(show_power_alert: bool) -> Arc<Self> {
        Self::with_restoration(show_power_alert, None, BTreeSet::new())
    }

    /// Windows preserves nothing across a process restart, so the identifier is
    /// accepted and ignored.
    pub fn with_restoration(
        _show_power_alert: bool,
        _restore_identifier: Option<&str>,
        _restore_allowed: BTreeSet<BluetoothUuid>,
    ) -> Arc<Self> {
        Arc::new_cyclic(|weak: &Weak<Inner>| Inner {
            // Joining the multi-threaded apartment is what makes every later
            // call legal on any thread.
            available: winrt::initialize(),
            hub: ScanHub::new(),
            sightings: Mutex::new(HashMap::new()),
            watcher: Mutex::new(None),
            devices: DeviceRegistry::default(),
            notifications: Mutex::new(HashMap::new()),
            value_handlers: Mutex::new(HashMap::new()),
            services_changed_handlers: Mutex::new(HashMap::new()),
            self_ref: weak.clone(),
            selected: Mutex::new(None),
            pdu_sizes: Mutex::new(HashMap::new()),
        })
    }

    // ── Availability ────────────────────────────────────────────────────────

    pub fn state(&self) -> ManagerState {
        if !self.available {
            return ManagerState::Unsupported;
        }
        // Reaching the statics proves the Bluetooth stack is present; whether a
        // radio is switched on only shows when something is asked of it.
        match ble::statics(
            classes::BLUETOOTH_LE_DEVICE,
            interfaces::I_BLUETOOTH_LE_DEVICE_STATICS,
        ) {
            Some(_) => ManagerState::PoweredOn,
            None => ManagerState::Unsupported,
        }
    }

    pub async fn settled_state(&self) -> ManagerState {
        self.state()
    }

    pub async fn require_powered_on(&self) -> Result<()> {
        self.state().require_powered_on()
    }

    pub fn restored(&self) -> Option<RestoredScan> {
        None
    }

    /// Await a WinRT operation, given the signature of what it returns.
    async fn await_operation(&self, operation: ComPtr, result: Signature) -> Result<ComPtr> {
        let (tx, rx) = oneshot::channel();
        let sender = Mutex::new(Some(tx));
        let attached = ble::on_completed(&operation, result, move |value| {
            if let Ok(mut guard) = sender.lock() {
                if let Some(tx) = guard.take() {
                    let _ = tx.send(value);
                }
            }
        });
        if !attached {
            return Err(Error::Network(
                "could not attach a completion handler".into(),
            ));
        }
        // The operation must outlive its handler, which fires on a pool thread.
        let held = operation;
        let value = rx
            .await
            .map_err(|_| Error::Aborted("the operation was cancelled".into()))?;
        drop(held);
        value.ok_or_else(|| Error::Network("the operation reported no result".into()))
    }

    // ── Scanning ────────────────────────────────────────────────────────────

    pub fn scan_hub(&self) -> &ScanHub {
        &self.hub
    }

    /// Start or stop the advertisement watcher.
    ///
    /// WinRT's watcher takes no service filter, so a watcher that widens the
    /// hub's filter needs nothing done here: starting is a no-op when one is
    /// already running, and only the last watcher leaving stops it.
    pub fn set_radio_scanning(self: &Arc<Self>, on: bool) -> Result<()> {
        if !on {
            if let Some((watcher, _handler)) = self.watcher.lock().unwrap().take() {
                ble::call_void(&watcher, slots::ibluetooth_leadvertisement_watcher::STOP);
            }
            return Ok(());
        }
        if self.watcher.lock().unwrap().is_some() {
            return Ok(());
        }

        let watcher = ble::activate(classes::BLUETOOTH_LE_ADVERTISEMENT_WATCHER)
            .ok_or(Error::NotAvailable(Availability::Unsupported))?;

        // Active scanning asks for a scan response, which is where most
        // peripherals put their name.
        unsafe {
            let f: unsafe extern "system" fn(*mut std::ffi::c_void, i32) -> i32 =
                watcher.method(slots::ibluetooth_leadvertisement_watcher::SET_SCANNING_MODE);
            f(watcher.as_raw(), SCANNING_MODE_ACTIVE);
        }

        // `Received` is a TypedEventHandler, whose IID is computed from both
        // its argument types.
        let handler_iid = Signature::Parameterized {
            generic: generics::TYPED_EVENT_HANDLER,
            arguments: vec![
                class_signature(classes::BLUETOOTH_LE_ADVERTISEMENT_WATCHER),
                class_signature(classes::BLUETOOTH_LE_ADVERTISEMENT_RECEIVED_EVENT_ARGS),
            ],
        }
        .iid()
        .expect("a parameterised signature always has an IID");

        let engine = Arc::downgrade(self);
        let handler = Delegate::new(handler_iid, move |_sender, args| {
            let Some(engine) = engine.upgrade() else {
                return;
            };
            if args.is_null() {
                return;
            }
            // Borrowed for the duration of the callback; not ours to release.
            let args = std::mem::ManuallyDrop::new(unsafe { ComPtr::adopt(args) });
            if let Some(args) = args.as_ref() {
                engine.on_advertisement(args);
            }
        });

        let mut token: i64 = 0;
        unsafe {
            let f: unsafe extern "system" fn(
                *mut std::ffi::c_void,
                *mut std::ffi::c_void,
                *mut i64,
            ) -> i32 = watcher.method(slots::ibluetooth_leadvertisement_watcher::RECEIVED);
            f(watcher.as_raw(), handler.as_raw(), &mut token);
        }

        if !ble::call_void(&watcher, slots::ibluetooth_leadvertisement_watcher::START) {
            return Err(Error::Network(
                "the advertisement watcher would not start".into(),
            ));
        }
        *self.watcher.lock().unwrap() = Some((watcher, handler));
        Ok(())
    }

    /// Scan, let `chooser` pick, and grant access to the result.
    pub async fn request_device(
        self: &Arc<Self>,
        options: RequestDeviceOptions,
        chooser: &dyn DeviceChooser,
    ) -> Result<String> {
        options.validate()?;
        self.require_powered_on().await?;

        let allowed = options.grant();
        let id = scan::choose(
            &self.hub,
            options,
            chooser,
            |on| self.set_radio_scanning(on),
            || {},
        )
        .await?;
        let Some((address, name)) = self
            .sightings
            .lock()
            .unwrap()
            .get(&id)
            .map(|s| (s.address, s.name.clone()))
        else {
            return Err(Error::NotFound(format!(
                "the chooser returned {id:?}, which was not among the devices it was offered"
            )));
        };

        self.devices.insert(
            &id,
            name,
            allowed,
            false,
            DeviceData {
                address,
                device: None,
                services: Vec::new(),
            },
        );
        Ok(id)
    }

    /// One advertisement, from the watcher's callback.
    fn on_advertisement(&self, args: &ComPtr) {
        if !self.hub.is_watching() {
            return;
        }

        use slots::ibluetooth_leadvertisement_received_event_args as ev;
        let Some(address) = ble::get_u64(args, ev::BLUETOOTH_ADDRESS) else {
            return;
        };
        let rssi = ble::get_i16(args, ev::RAW_SIGNAL_STRENGTH_IN_D_BM).unwrap_or(127) as i32;

        let mut name = None;
        let mut service_uuids = Vec::new();
        if let Some(advertisement) = ble::get_object(args, ev::ADVERTISEMENT) {
            use slots::ibluetooth_leadvertisement as ad;
            name = ble::get_string(&advertisement, ad::LOCAL_NAME).filter(|s| !s.is_empty());
            if let Some(uuids) = ble::get_object(&advertisement, ad::SERVICE_UUIDS) {
                // An IVectorView<Guid> of by-value GUIDs rather than objects,
                // so it is read directly instead of through `for_each`.
                service_uuids = read_guid_vector(&uuids);
            }
        }

        #[allow(clippy::needless_update)]
        let advertisement = Advertisement {
            local_name: name.clone(),
            tx_power: None,
            // Would mean walking the raw data sections.
            appearance: None,
            is_connectable: Some(true),
            service_uuids: service_uuids
                .iter()
                .filter_map(|g| BluetoothUuid::parse(&g.to_string()).ok())
                .collect(),
            overflow_service_uuids: Vec::new(),
            solicited_service_uuids: Vec::new(),
            manufacturer_data: HashMap::new(),
            service_data: HashMap::new(),
            rssi,
            // WinRT hands over parsed data sections, not the packet as received.
            raw: None,
            // `class_of_device` exists when webbluetooth-core's `classic` is on,
            // which feature unification can do without this crate's `classic`.
            ..Default::default()
        };

        let id = ble::format_address(address);
        self.sightings.lock().unwrap().insert(
            id.clone(),
            Sighting {
                address,
                name: name.clone(),
                rssi,
                at: std::time::Instant::now(),
            },
        );
        // Each watcher applies its own filter; this one just reports.
        self.hub.publish(&id, name.as_deref(), &advertisement);
    }

    // ── Device access ───────────────────────────────────────────────────────

    // The registry answers these identically for every backend.
    webbluetooth_core::registry::forward_to_registry!();

    // ── PHY ─────────────────────────────────────────────────────────────────

    /// `BluetoothLEDevice.GetConnectionPhy`, on Windows 10 2004 or newer.
    ///
    /// WinRT reports the PHY and offers no way to choose one, so this is the
    /// half of the pair that exists here.
    pub async fn phy(&self, id: &str) -> Result<webbluetooth_core::ConnectionPhy> {
        let device = self.device(id)?;
        let device6 = device
            .cast(interfaces::I_BLUETOOTH_LE_DEVICE6)
            .ok_or_else(|| {
                Error::NotSupported(
                    "IBluetoothLEDevice6 is unavailable; the connection PHY needs \
                     Windows 10 2004 or newer"
                        .into(),
                )
            })?;
        let phy = ble::get_object(&device6, slots::ibluetooth_ledevice6::GET_CONNECTION_PHY)
            .ok_or_else(|| Error::Network("GetConnectionPhy was refused".into()))?;

        // `BluetoothLEConnectionPhy` carries a `TransmitInfo` and a
        // `ReceiveInfo`, each a `BluetoothLEConnectionPhyInfo` with three
        // booleans — the PHY is reported as which one it is, not as a number.
        let read = |slot| -> Result<webbluetooth_core::Phy> {
            let info = ble::get_object(&phy, slot)
                .ok_or_else(|| Error::Network("a PHY direction was unreadable".into()))?;
            if ble::get_bool(
                &info,
                slots::ibluetooth_leconnection_phy_info::IS_UNCODED2_M_PHY,
            )
            .unwrap_or(false)
            {
                return Ok(webbluetooth_core::Phy::Le2M);
            }
            if ble::get_bool(&info, slots::ibluetooth_leconnection_phy_info::IS_CODED_PHY)
                .unwrap_or(false)
            {
                return Ok(webbluetooth_core::Phy::LeCoded);
            }
            // `IsUncoded1MPhy`, or none set at all — which is what an
            // unsupported or not-yet-established link reports.
            Ok(webbluetooth_core::Phy::Le1M)
        };

        Ok(webbluetooth_core::ConnectionPhy {
            tx: read(slots::ibluetooth_leconnection_phy::TRANSMIT_INFO)?,
            rx: read(slots::ibluetooth_leconnection_phy::RECEIVE_INFO)?,
        })
    }

    /// Not possible: WinRT reports the PHY and offers no way to ask for one.
    pub async fn set_preferred_phy(
        &self,
        id: &str,
        _tx: webbluetooth_core::Phy,
        _rx: webbluetooth_core::Phy,
    ) -> Result<webbluetooth_core::ConnectionPhy> {
        let _ = self.device(id)?;
        Err(Error::NotSupported(
            "WinRT reports the connection PHY but has no way to request one".into(),
        ))
    }

    // ── Pairing ─────────────────────────────────────────────────────────────

    /// `DeviceInformationPairing.PairAsync`.
    ///
    /// Three hops to reach it: the device carries a `DeviceInformation` on a
    /// later interface, which carries a `Pairing` on a later interface still,
    /// which is what runs the ceremony. Windows picks the ceremony itself here
    /// and shows its own prompt — the custom form, where an application drives
    /// passkey entry, is a different interface and a different design
    /// decision.
    pub async fn pair(&self, id: &str) -> Result<webbluetooth_core::Pairing> {
        let pairing = self.pairing_of(id)?;
        if ble::get_bool(&pairing, slots::idevice_information_pairing::IS_PAIRED).unwrap_or(false) {
            return Ok(webbluetooth_core::Pairing::AlreadyPaired);
        }

        let operation = invoke_async(&pairing, slots::idevice_information_pairing::PAIR_ASYNC)
            .ok_or_else(|| Error::Network("PairAsync was refused".into()))?;
        let result = self
            .await_operation(operation, class_signature(classes::DEVICE_PAIRING_RESULT))
            .await?;

        // `DevicePairingResultStatus`: 0 is Paired, 1 is NotReadyToPair, and
        // 16 is AlreadyPaired. Everything else is a refusal of some kind, and
        // the number is carried through because the distinctions matter to
        // someone debugging a device that will not bond.
        match ble::get_i32(&result, slots::idevice_pairing_result::STATUS) {
            Some(0) => Ok(webbluetooth_core::Pairing::Paired),
            Some(16) => Ok(webbluetooth_core::Pairing::AlreadyPaired),
            Some(status) => Err(Error::Network(format!(
                "pairing did not complete (DevicePairingResultStatus {status})"
            ))),
            None => Err(Error::Network("the pairing result was unreadable".into())),
        }
    }

    pub async fn is_paired(&self, id: &str) -> Result<bool> {
        let pairing = self.pairing_of(id)?;
        Ok(ble::get_bool(&pairing, slots::idevice_information_pairing::IS_PAIRED).unwrap_or(false))
    }

    /// The `DeviceInformationPairing` for a connected device.
    fn pairing_of(&self, id: &str) -> Result<ComPtr> {
        let device = self.device(id)?;
        let device2 = device
            .cast(interfaces::I_BLUETOOTH_LE_DEVICE2)
            .ok_or_else(|| Error::NotSupported("IBluetoothLEDevice2 is unavailable".into()))?;
        let information =
            ble::get_object(&device2, slots::ibluetooth_ledevice2::DEVICE_INFORMATION)
                .ok_or_else(|| Error::Network("the device has no DeviceInformation".into()))?;
        let information2 = information
            .cast(interfaces::I_DEVICE_INFORMATION2)
            .ok_or_else(|| Error::NotSupported("IDeviceInformation2 is unavailable".into()))?;
        ble::get_object(&information2, slots::idevice_information2::PAIRING)
            .ok_or_else(|| Error::Network("the device exposes no pairing information".into()))
    }

    // ── Controllers ─────────────────────────────────────────────────────
    //
    // Windows genuinely has several, and finds them the way it finds any
    // device class: `BluetoothAdapter.GetDeviceSelector()` builds an AQS
    // query, `DeviceInformation.FindAllAsync` runs it, and each result is
    // opened by id. Three round trips rather than one call, because WinRT has
    // no "list the radios" method.

    pub async fn adapters(&self) -> Result<Vec<webbluetooth_core::AdapterInfo>> {
        let statics = ble::statics(
            classes::BLUETOOTH_ADAPTER,
            interfaces::I_BLUETOOTH_ADAPTER_STATICS,
        )
        .ok_or(Error::NotAvailable(Availability::Unsupported))?;

        let selector = ble::get_string(
            &statics,
            slots::ibluetooth_adapter_statics::GET_DEVICE_SELECTOR,
        )
        .ok_or_else(|| Error::Network("GetDeviceSelector was refused".into()))?;

        let found = self.find_all(&selector).await?;
        let default = self.default_adapter_id().await;

        let mut out = Vec::new();
        ble::for_each(&found, |information| {
            let Some(id) = ble::get_string(&information, slots::idevice_information::ID) else {
                return;
            };
            out.push(webbluetooth_core::AdapterInfo {
                name: ble::get_string(&information, slots::idevice_information::NAME)
                    .filter(|n| !n.is_empty()),
                // `IsEnabled` on the device rather than the radio's own state:
                // reading the radio needs `Radio.GetRadiosAsync`, a separate
                // capability, and a disabled device is an unusable one either
                // way.
                powered: ble::get_bool(&information, slots::idevice_information::IS_ENABLED)
                    .unwrap_or(false),
                address: None,
                is_default: default.as_deref() == Some(id.as_str()),
                id,
            });
        });

        // The address needs the adapter itself opened, which is a round trip
        // each, so it is done after the cheap listing rather than inside it.
        for adapter in &mut out {
            adapter.address = self.adapter_address(&adapter.id).await;
        }
        Ok(out)
    }

    pub async fn adapter(&self) -> Result<webbluetooth_core::AdapterInfo> {
        let adapters = self.adapters().await?;
        adapters
            .iter()
            .find(|a| Some(a.id.as_str()) == self.selected.lock().unwrap().as_deref())
            .or_else(|| adapters.iter().find(|a| a.is_default))
            .or_else(|| adapters.first())
            .cloned()
            .ok_or(Error::NotAvailable(Availability::Unsupported))
    }

    pub async fn select_adapter(&self, id: &str) -> Result<()> {
        let adapters = self.adapters().await?;
        if !adapters.iter().any(|a| a.id == id) {
            return Err(Error::NotFound(format!(
                "no controller with id {id:?}; this system has {}",
                adapters
                    .iter()
                    .map(|a| a.id.as_str())
                    .collect::<Vec<_>>()
                    .join(", ")
            )));
        }
        *self.selected.lock().unwrap() = Some(id.to_owned());
        Ok(())
    }

    /// `DeviceInformation.FindAllAsync(selector)`.
    async fn find_all(&self, selector: &str) -> Result<ComPtr> {
        let statics = ble::statics(
            classes::DEVICE_INFORMATION,
            interfaces::I_DEVICE_INFORMATION_STATICS,
        )
        .ok_or(Error::NotAvailable(Availability::Unsupported))?;
        // The raw out-parameter is confined to this block. A bare
        // `*mut c_void` alive across an await makes the whole future
        // `!Send` — `ComPtr` is `Send`, the pointer it is built from is not.
        // The `HString` goes with it. A `WinRtString` owns a `*mut c_void`,
        // so keeping one alive over the await below would make this future
        // `!Send` just as surely as the out-parameter does — and it is only
        // needed for the call itself.
        let operation = {
            let selector = winrt::WinRtString::new(selector)
                .ok_or_else(|| Error::Network("could not build the device selector".into()))?;
            let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
            let hr = unsafe {
                let f: unsafe extern "system" fn(
                    *mut std::ffi::c_void,
                    winrt::HString,
                    *mut *mut std::ffi::c_void,
                ) -> i32 =
                    statics.method(slots::idevice_information_statics::FIND_ALL_ASYNC_AQS_FILTER);
                f(statics.as_raw(), selector.as_raw(), &mut operation)
            };
            (crate::com::succeeded(hr))
                .then(|| unsafe { ComPtr::adopt(operation) })
                .flatten()
        }
        .ok_or_else(|| Error::Network("FindAllAsync was refused".into()))?;

        self.await_operation(operation, device_information_collection())
            .await
    }

    /// The id of the controller `GetDefaultAsync` returns, if there is one.
    async fn default_adapter_id(&self) -> Option<String> {
        let statics = ble::statics(
            classes::BLUETOOTH_ADAPTER,
            interfaces::I_BLUETOOTH_ADAPTER_STATICS,
        )?;
        let operation = invoke_async(
            &statics,
            slots::ibluetooth_adapter_statics::GET_DEFAULT_ASYNC,
        )?;
        let adapter = self
            .await_operation(operation, class_signature(classes::BLUETOOTH_ADAPTER))
            .await
            .ok()?;
        ble::get_string(&adapter, slots::ibluetooth_adapter::DEVICE_ID)
    }

    /// Open one controller by id and read its own address.
    async fn adapter_address(&self, id: &str) -> Option<String> {
        let statics = ble::statics(
            classes::BLUETOOTH_ADAPTER,
            interfaces::I_BLUETOOTH_ADAPTER_STATICS,
        )?;
        // The raw out-parameter is confined to this block, and so is the
        // `HString`: both are `*mut c_void` under the name, and either one
        // alive across the await below would make this future `!Send`.
        let operation = {
            let id = winrt::WinRtString::new(id)?;
            let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
            let hr = unsafe {
                let f: unsafe extern "system" fn(
                    *mut std::ffi::c_void,
                    winrt::HString,
                    *mut *mut std::ffi::c_void,
                ) -> i32 = statics.method(slots::ibluetooth_adapter_statics::FROM_ID_ASYNC);
                f(statics.as_raw(), id.as_raw(), &mut operation)
            };
            (crate::com::succeeded(hr))
                .then(|| unsafe { ComPtr::adopt(operation) })
                .flatten()
        }?;
        let adapter = self
            .await_operation(operation, class_signature(classes::BLUETOOTH_ADAPTER))
            .await
            .ok()?;
        let address = ble::get_u64(&adapter, slots::ibluetooth_adapter::BLUETOOTH_ADDRESS)?;
        Some(ble::format_address(address))
    }

    /// `BluetoothLEDevice.ConnectionParameters`, on Windows 10 2004 or newer.
    ///
    /// The one platform that hands an application the negotiated values
    /// directly. It lives on `IBluetoothLEDevice6`, so an older Windows
    /// answers `NotSupported` rather than failing.
    pub async fn connection_parameters(
        &self,
        id: &str,
    ) -> Result<webbluetooth_core::ConnectionParameters> {
        let device = self.device(id)?;
        let device6 = device
            .cast(interfaces::I_BLUETOOTH_LE_DEVICE6)
            .ok_or_else(|| {
                Error::NotSupported(
                    "IBluetoothLEDevice6 is unavailable; connection parameters need \
                     Windows 10 2004 or newer"
                        .into(),
                )
            })?;
        let parameters = ble::get_object(
            &device6,
            slots::ibluetooth_ledevice6::GET_CONNECTION_PARAMETERS,
        )
        .ok_or_else(|| Error::Network("GetConnectionParameters was refused".into()))?;

        let read = |slot| {
            ble::get_u16(&parameters, slot)
                .ok_or_else(|| Error::Network("a connection parameter was unreadable".into()))
        };
        Ok(webbluetooth_core::ConnectionParameters {
            interval: read(slots::ibluetooth_leconnection_parameters::CONNECTION_INTERVAL)?,
            latency: read(slots::ibluetooth_leconnection_parameters::CONNECTION_LATENCY)?,
            timeout: read(slots::ibluetooth_leconnection_parameters::LINK_TIMEOUT)?,
        })
    }

    /// Adopt a device by address.
    ///
    /// `connect` resolves the `BluetoothLEDevice` from the address anyway —
    /// WinRT has `FromBluetoothAddressAsync` and no notion of a device object
    /// that predates it — so adopting is recording the address and the grant.
    pub async fn adopt_device(
        &self,
        id: &str,
        allowed: webbluetooth_core::registry::Grant,
    ) -> Result<String> {
        let address = ble::parse_address(id)
            .ok_or_else(|| Error::NotFound(format!("{id:?} is not a Bluetooth address")))?;
        let name = self
            .sightings
            .lock()
            .unwrap()
            .get(id)
            .and_then(|s| s.name.clone());
        self.devices.insert(
            id,
            name,
            allowed,
            false,
            DeviceData {
                address,
                device: None,
                services: Vec::new(),
            },
        );
        Ok(id.to_owned())
    }

    async fn gatt_lock(&self, id: &str) -> Result<futures_util::lock::OwnedMutexGuard<()>> {
        self.devices.gatt_lock(id).await
    }

    /// Revoke a grant. Releasing the device object is what drops the link —
    /// Windows keeps it while any reference survives.
    pub fn forget(&self, id: &str) {
        let _ = self.devices.remove(id);
    }

    fn device(&self, id: &str) -> Result<ComPtr> {
        self.devices
            .get(id, |d| d.inner.device.clone())?
            .ok_or_else(|| Error::InvalidState("the device is not connected".into()))
    }

    // ── Attribute accessors ─────────────────────────────────────────────────

    pub fn service_is_primary(&self, _handle: &Handle) -> bool {
        // GetGattServicesAsync returns primary services only.
        true
    }

    pub fn characteristic_properties(&self, handle: &Handle) -> CharacteristicProperties {
        // GattCharacteristicProperties are the Bluetooth bit values.
        CharacteristicProperties(handle.0.properties)
    }

    pub fn cached_value(&self, handle: &Handle) -> Option<Vec<u8>> {
        handle.0.value.lock().unwrap().clone()
    }

    pub fn is_notifying(&self, handle: &Handle) -> bool {
        self.notifications
            .lock()
            .unwrap()
            .contains_key(&handle.object().key())
    }

    // ── GATT operations ─────────────────────────────────────────────────────

    pub async fn connect(self: &Arc<Self>, id: &str) -> Result<()> {
        self.require_powered_on().await?;
        if self.is_connected(id) {
            return Ok(());
        }
        let address = self.devices.get(id, |d| d.inner.address)?;

        let statics = ble::statics(
            classes::BLUETOOTH_LE_DEVICE,
            interfaces::I_BLUETOOTH_LE_DEVICE_STATICS,
        )
        .ok_or(Error::NotAvailable(Availability::Unsupported))?;

        // The raw out-parameter is confined to this block. A bare
        // `*mut c_void` alive across an await makes the whole future
        // `!Send` — `ComPtr` is `Send`, the pointer it is built from is not.
        let operation = {
            let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
            let hr = unsafe {
                let f: unsafe extern "system" fn(
                    *mut std::ffi::c_void,
                    u64,
                    *mut *mut std::ffi::c_void,
                ) -> i32 = statics
                    .method(slots::ibluetooth_ledevice_statics::FROM_BLUETOOTH_ADDRESS_ASYNC);
                f(statics.as_raw(), address, &mut operation)
            };
            (crate::com::succeeded(hr))
                .then(|| unsafe { ComPtr::adopt(operation) })
                .flatten()
        }
        .ok_or_else(|| Error::Network("FromBluetoothAddressAsync was refused".into()))?;

        let device = self
            .await_operation(operation, class_signature(classes::BLUETOOTH_LE_DEVICE))
            .await
            .map_err(|_| Error::Network(format!("no device at {id}")))?;

        // WinRT has no Connect; discovering services is what opens the link.
        let services = self.discover(&device).await?;

        self.attach_services_changed_handler(id, &device);

        self.devices.update(id, |d| {
            d.connected = true;
            d.inner.device = Some(device);
            d.inner.services = services;
        });
        // After the device is stored, because reading it goes back through the
        // registry. A failure here is not a failed connection: it costs a
        // larger MTU, not the link.
        self.read_max_pdu_size(id).await;
        Ok(())
    }

    /// Subscribe to `GattServicesChanged`, WinRT's account of the Service
    /// Changed indication.
    ///
    /// Windows subscribes to that characteristic itself and re-discovers, so
    /// what arrives is the result rather than the indication — the same shape
    /// as CoreBluetooth's `didModifyServices:` and BlueZ's GATT objects coming
    /// and going. Without it every handle stays valid on paper after the peer
    /// has rearranged its attribute table underneath them.
    fn attach_services_changed_handler(&self, id: &str, device: &ComPtr) {
        let handler_iid = Signature::Parameterized {
            generic: generics::TYPED_EVENT_HANDLER,
            arguments: vec![
                class_signature(classes::BLUETOOTH_LE_DEVICE),
                Signature::OBJECT,
            ],
        }
        .iid()
        .expect("a parameterised signature always has an IID");

        // Weak, so the handler the device owns does not own the engine back.
        // Once the engine is gone there is nobody left to tell.
        let engine = self.self_ref.clone();
        let id = id.to_owned();
        let watched = id.clone();
        let handler = Delegate::new(handler_iid, move |_sender, _args| {
            if let Some(engine) = engine.upgrade() {
                engine.devices.mark_services_changed(&watched);
            }
        });

        let mut token: i64 = 0;
        unsafe {
            let f: unsafe extern "system" fn(
                *mut std::ffi::c_void,
                *mut std::ffi::c_void,
                *mut i64,
            ) -> i32 = device.method(slots::ibluetooth_ledevice::GATT_SERVICES_CHANGED);
            f(device.as_raw(), handler.as_raw(), &mut token);
        }
        self.services_changed_handlers
            .lock()
            .unwrap()
            .insert(id, handler);
    }

    /// `GetGattServicesAsync`, unwrapped into `(uuid, service)` pairs.
    async fn discover(&self, device: &ComPtr) -> Result<Vec<(BluetoothUuid, ComPtr)>> {
        // The raw out-parameter is confined to this block. A bare
        // `*mut c_void` alive across an await makes the whole future
        // `!Send` — `ComPtr` is `Send`, the pointer it is built from is not.
        let operation = {
            let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
            let hr = unsafe {
                let f: unsafe extern "system" fn(
                    *mut std::ffi::c_void,
                    *mut *mut std::ffi::c_void,
                ) -> i32 = device.method(slots::ibluetooth_ledevice3::GET_GATT_SERVICES_ASYNC);
                f(device.as_raw(), &mut operation)
            };
            (crate::com::succeeded(hr))
                .then(|| unsafe { ComPtr::adopt(operation) })
                .flatten()
        }
        .ok_or_else(|| Error::Network("GetGattServicesAsync was refused".into()))?;

        let result = self
            .await_operation(
                operation,
                class_signature(classes::GATT_DEVICE_SERVICES_RESULT),
            )
            .await?;

        let status =
            ble::get_i32(&result, slots::igatt_device_services_result::STATUS).unwrap_or(i32::MIN);
        if status != GATT_SUCCESS {
            return Err(gatt_error("service discovery", status));
        }

        let mut services = Vec::new();
        if let Some(vector) =
            ble::get_object(&result, slots::igatt_device_services_result::SERVICES)
        {
            ble::for_each(&vector, |service| {
                if let Some(uuid) = ble::get_guid(&service, slots::igatt_device_service::UUID) {
                    if let Ok(uuid) = BluetoothUuid::parse(&uuid.to_string()) {
                        services.push((uuid, service));
                    }
                }
            });
        }
        Ok(services)
    }

    pub fn disconnect(&self, id: &str) {
        // Releasing every reference is how Windows is told to drop the link.
        self.services_changed_handlers.lock().unwrap().remove(id);
        // Negotiated afresh on the next connection.
        self.pdu_sizes.lock().unwrap().remove(id);
        self.devices.update(id, |d| {
            d.inner.services.clear();
            d.inner.device = None;
        });
        self.devices.mark_disconnected(id);
    }

    pub async fn discover_services(
        &self,
        id: &str,
        uuid: Option<&BluetoothUuid>,
    ) -> Result<Vec<Handle>> {
        let _guard = self.gatt_lock(id).await?;
        if !self.is_connected(id) {
            return Err(Error::InvalidState("the device is not connected".into()));
        }
        let services = self.devices.get(id, |d| d.inner.services.clone())?;
        Ok(services
            .into_iter()
            .filter(|(found, _)| uuid.is_none_or(|want| want == found))
            .map(|(found, object)| {
                Handle(Arc::new(HandleData {
                    object,
                    uuid: found,
                    properties: 0,
                    value: Mutex::new(None),
                }))
            })
            .collect())
    }

    pub async fn discover_included_services(
        &self,
        id: &str,
        service: &Handle,
        uuid: Option<&BluetoothUuid>,
    ) -> Result<Vec<Handle>> {
        let _guard = self.gatt_lock(id).await?;

        // `IGattDeviceService3`, like the characteristic call — the original
        // `IGattDeviceService.GetIncludedServices` is synchronous and
        // deprecated.
        // The raw out-parameter is confined to this block. A bare
        // `*mut c_void` alive across an await makes the whole future
        // `!Send` — `ComPtr` is `Send`, the pointer it is built from is not.
        let operation = {
            let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
            let hr = unsafe {
                let f: unsafe extern "system" fn(
                    *mut std::ffi::c_void,
                    *mut *mut std::ffi::c_void,
                ) -> i32 = service
                    .object()
                    .method(slots::igatt_device_service3::GET_INCLUDED_SERVICES_ASYNC);
                f(service.object().as_raw(), &mut operation)
            };
            (crate::com::succeeded(hr))
                .then(|| unsafe { ComPtr::adopt(operation) })
                .flatten()
        }
        .ok_or_else(|| Error::Network("GetIncludedServicesAsync was refused".into()))?;

        let result = self
            .await_operation(
                operation,
                class_signature(classes::GATT_DEVICE_SERVICES_RESULT),
            )
            .await?;
        let status =
            ble::get_i32(&result, slots::igatt_device_services_result::STATUS).unwrap_or(i32::MIN);
        if status != GATT_SUCCESS {
            return Err(gatt_error("included service discovery", status));
        }

        let mut out = Vec::new();
        if let Some(vector) =
            ble::get_object(&result, slots::igatt_device_services_result::SERVICES)
        {
            ble::for_each(&vector, |included| {
                let Some(found) = ble::get_guid(&included, slots::igatt_device_service::UUID)
                else {
                    return;
                };
                let Ok(found) = BluetoothUuid::parse(&found.to_string()) else {
                    return;
                };
                if uuid.is_some_and(|want| *want != found) {
                    return;
                }
                out.push(Handle(Arc::new(HandleData {
                    object: included,
                    uuid: found,
                    // A service declaration has no properties.
                    properties: 0,
                    value: Mutex::new(None),
                })));
            });
        }
        Ok(out)
    }

    pub async fn discover_characteristics(
        &self,
        id: &str,
        service: &Handle,
        uuid: Option<&BluetoothUuid>,
    ) -> Result<Vec<Handle>> {
        let _guard = self.gatt_lock(id).await?;

        // The raw out-parameter is confined to this block. A bare
        // `*mut c_void` alive across an await makes the whole future
        // `!Send` — `ComPtr` is `Send`, the pointer it is built from is not.
        let operation = {
            let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
            let hr = unsafe {
                let f: unsafe extern "system" fn(
                    *mut std::ffi::c_void,
                    *mut *mut std::ffi::c_void,
                ) -> i32 = service
                    .object()
                    .method(slots::igatt_device_service3::GET_CHARACTERISTICS_ASYNC);
                f(service.object().as_raw(), &mut operation)
            };
            (crate::com::succeeded(hr))
                .then(|| unsafe { ComPtr::adopt(operation) })
                .flatten()
        }
        .ok_or_else(|| Error::Network("GetCharacteristicsAsync was refused".into()))?;

        let result = self
            .await_operation(
                operation,
                class_signature(classes::GATT_CHARACTERISTICS_RESULT),
            )
            .await?;
        let status =
            ble::get_i32(&result, slots::igatt_characteristics_result::STATUS).unwrap_or(i32::MIN);
        if status != GATT_SUCCESS {
            return Err(gatt_error("characteristic discovery", status));
        }

        let mut out = Vec::new();
        if let Some(vector) = ble::get_object(
            &result,
            slots::igatt_characteristics_result::CHARACTERISTICS,
        ) {
            ble::for_each(&vector, |characteristic| {
                let Some(found) = ble::get_guid(&characteristic, slots::igatt_characteristic::UUID)
                else {
                    return;
                };
                let Ok(found) = BluetoothUuid::parse(&found.to_string()) else {
                    return;
                };
                if uuid.is_some_and(|want| *want != found) {
                    return;
                }
                let properties = ble::get_i32(
                    &characteristic,
                    slots::igatt_characteristic::CHARACTERISTIC_PROPERTIES,
                )
                .unwrap_or(0) as u32;
                out.push(Handle(Arc::new(HandleData {
                    object: characteristic,
                    uuid: found,
                    properties,
                    value: Mutex::new(None),
                })));
            });
        }
        Ok(out)
    }

    pub async fn discover_descriptors(
        &self,
        id: &str,
        characteristic: &Handle,
        uuid: Option<&BluetoothUuid>,
    ) -> Result<Vec<Handle>> {
        let _guard = self.gatt_lock(id).await?;

        // The raw out-parameter is confined to this block. A bare
        // `*mut c_void` alive across an await makes the whole future
        // `!Send` — `ComPtr` is `Send`, the pointer it is built from is not.
        let operation = {
            let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
            let hr = unsafe {
                let f: unsafe extern "system" fn(
                    *mut std::ffi::c_void,
                    *mut *mut std::ffi::c_void,
                ) -> i32 = characteristic
                    .object()
                    .method(slots::igatt_characteristic3::GET_DESCRIPTORS_ASYNC);
                f(characteristic.object().as_raw(), &mut operation)
            };
            (crate::com::succeeded(hr))
                .then(|| unsafe { ComPtr::adopt(operation) })
                .flatten()
        }
        .ok_or_else(|| Error::Network("GetDescriptorsAsync was refused".into()))?;

        let result = self
            .await_operation(operation, class_signature(classes::GATT_DESCRIPTORS_RESULT))
            .await?;

        let mut out = Vec::new();
        if let Some(vector) = ble::get_object(&result, slots::igatt_descriptors_result::DESCRIPTORS)
        {
            ble::for_each(&vector, |descriptor| {
                let Some(found) = ble::get_guid(&descriptor, slots::igatt_descriptor::UUID) else {
                    return;
                };
                let Ok(found) = BluetoothUuid::parse(&found.to_string()) else {
                    return;
                };
                if uuid.is_some_and(|want| *want != found) {
                    return;
                }
                out.push(Handle(Arc::new(HandleData {
                    object: descriptor,
                    uuid: found,
                    properties: 0,
                    value: Mutex::new(None),
                })));
            });
        }
        Ok(out)
    }

    pub async fn read_characteristic(&self, id: &str, handle: &Handle) -> Result<Vec<u8>> {
        let _guard = self.gatt_lock(id).await?;
        let operation = invoke_async(
            handle.object(),
            slots::igatt_characteristic::READ_VALUE_ASYNC,
        )
        .ok_or_else(|| Error::Network("ReadValueAsync was refused".into()))?;
        let result = self
            .await_operation(operation, class_signature(classes::GATT_READ_RESULT))
            .await?;

        let status = ble::get_i32(&result, slots::igatt_read_result::STATUS).unwrap_or(i32::MIN);
        if status != GATT_SUCCESS {
            return Err(gatt_error("read", status));
        }
        let buffer = ble::get_object(&result, slots::igatt_read_result::VALUE)
            .ok_or_else(|| Error::Network("the read returned no buffer".into()))?;
        let bytes = ble::buffer_to_bytes(&buffer)
            .ok_or_else(|| Error::Network("the read buffer could not be copied".into()))?;
        *handle.0.value.lock().unwrap() = Some(bytes.clone());
        Ok(bytes)
    }

    pub async fn write_characteristic(
        &self,
        id: &str,
        handle: &Handle,
        value: &[u8],
        write_type: WriteType,
    ) -> Result<()> {
        // Only the acknowledged form takes the lock; see the note in the Apple
        // backend. WinRT queues writes internally and reports the outcome in
        // the operation's `GattCommunicationStatus`, so an unacknowledged one
        // needs no help from us to stay ordered.
        let _guard = match write_type {
            WriteType::WithResponse => Some(self.gatt_lock(id).await?),
            WriteType::WithoutResponse => None,
        };
        let buffer = ble::bytes_to_buffer(value)
            .ok_or_else(|| Error::Network("could not build a write buffer".into()))?;
        let option = match write_type {
            WriteType::WithResponse => WRITE_WITH_RESPONSE,
            WriteType::WithoutResponse => WRITE_WITHOUT_RESPONSE,
        };

        // The raw out-parameter is confined to this block. A bare
        // `*mut c_void` alive across an await makes the whole future
        // `!Send` — `ComPtr` is `Send`, the pointer it is built from is not.
        let operation = {
            let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
            let hr = unsafe {
                let f: unsafe extern "system" fn(
                    *mut std::ffi::c_void,
                    *mut std::ffi::c_void,
                    i32,
                    *mut *mut std::ffi::c_void,
                ) -> i32 = handle
                    .object()
                    .method(slots::igatt_characteristic::WRITE_VALUE_WITH_OPTION_ASYNC);
                f(
                    handle.object().as_raw(),
                    buffer.as_raw(),
                    option,
                    &mut operation,
                )
            };
            (crate::com::succeeded(hr))
                .then(|| unsafe { ComPtr::adopt(operation) })
                .flatten()
        }
        .ok_or_else(|| Error::Network("WriteValueWithOptionAsync was refused".into()))?;

        // The operation yields a GattCommunicationStatus, an enum rather than a
        // class, so its signature is different in kind from the others here.
        let status = self
            .await_operation(
                operation,
                Signature::Enum {
                    name:
                        "Windows.Devices.Bluetooth.GenericAttributeProfile.GattCommunicationStatus",
                    signed: true,
                },
            )
            .await;
        match status {
            Ok(_) => Ok(()),
            Err(e) => Err(e),
        }
    }

    pub async fn set_notify(&self, id: &str, handle: &Handle, enabled: bool) -> Result<bool> {
        let _guard = self.gatt_lock(id).await?;
        let properties = CharacteristicProperties(handle.0.properties);
        let value = match (enabled, properties.notify()) {
            (false, _) => CCCD_NONE,
            (true, true) => CCCD_NOTIFY,
            (true, false) => CCCD_INDICATE,
        };

        // Subscribing on Windows is a descriptor write like everywhere else,
        // but WinRT wraps it so the descriptor never has to be found.
        // The raw out-parameter is confined to this block. A bare
        // `*mut c_void` alive across an await makes the whole future
        // `!Send` — `ComPtr` is `Send`, the pointer it is built from is not.
        let operation = {
            let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
            let hr = unsafe {
                let f: unsafe extern "system" fn(
                    *mut std::ffi::c_void,
                    i32,
                    *mut *mut std::ffi::c_void,
                ) -> i32 = handle.object().method(
                    slots::igatt_characteristic::WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION_DESCRIPTOR_ASYNC,
                );
                f(handle.object().as_raw(), value, &mut operation)
            };
            (crate::com::succeeded(hr))
                .then(|| unsafe { ComPtr::adopt(operation) })
                .flatten()
        }
            .ok_or_else(|| Error::Network("the subscription write was refused".into()))?;

        self.await_operation(
            operation,
            Signature::Enum {
                name: "Windows.Devices.Bluetooth.GenericAttributeProfile.GattCommunicationStatus",
                signed: true,
            },
        )
        .await?;

        if enabled {
            self.attach_value_handler(handle);
        } else {
            self.value_handlers
                .lock()
                .unwrap()
                .remove(&handle.object().key());
            self.notifications
                .lock()
                .unwrap()
                .remove(&handle.object().key());
        }
        Ok(enabled)
    }

    /// Route `ValueChanged` into the notification stream.
    fn attach_value_handler(&self, handle: &Handle) {
        let key = handle.object().key();
        if self.value_handlers.lock().unwrap().contains_key(&key) {
            return;
        }

        let handler_iid = Signature::Parameterized {
            generic: generics::TYPED_EVENT_HANDLER,
            arguments: vec![
                class_signature(classes::GATT_CHARACTERISTIC),
                class_signature(classes::GATT_VALUE_CHANGED_EVENT_ARGS),
            ],
        }
        .iid()
        .expect("a parameterised signature always has an IID");

        // The map is shared rather than the engine, so a live subscription does
        // not keep the whole backend alive.
        let subscribers = Arc::new(());
        let _ = subscribers;
        let notifications: *const Mutex<
            HashMap<usize, Vec<webbluetooth_core::backlog::Sender<Vec<u8>>>>,
        > = &self.notifications;
        let notifications = notifications as usize;
        // Weak, so the delegate the characteristic owns does not own the
        // characteristic back. A dead handle simply stops being updated.
        let value_slot = Arc::downgrade(&handle.0);

        let handler = Delegate::new(handler_iid, move |_sender, args| {
            if args.is_null() {
                return;
            }
            let args = std::mem::ManuallyDrop::new(unsafe { ComPtr::adopt(args) });
            let Some(args) = args.as_ref() else { return };
            let Some(buffer) = ble::get_object(
                args,
                slots::igatt_value_changed_event_args::CHARACTERISTIC_VALUE,
            ) else {
                return;
            };
            let Some(bytes) = ble::buffer_to_bytes(&buffer) else {
                return;
            };

            // SAFETY: the engine outlives every handler it attached — the
            // handler is dropped when `value_handlers` is, which the engine
            // owns.
            if let Some(slot) = value_slot.upgrade() {
                *slot.value.lock().unwrap() = Some(bytes.clone());
            }

            let map = unsafe {
                &*(notifications
                    as *const Mutex<
                        HashMap<usize, Vec<webbluetooth_core::backlog::Sender<Vec<u8>>>>,
                    >)
            };
            let mut guard = map.lock().unwrap();
            if let Some(list) = guard.get_mut(&key) {
                list.retain(|tx| tx.send(bytes.clone()).is_ok());
                if list.is_empty() {
                    guard.remove(&key);
                }
            }
        });

        let mut token: i64 = 0;
        unsafe {
            let f: unsafe extern "system" fn(
                *mut std::ffi::c_void,
                *mut std::ffi::c_void,
                *mut i64,
            ) -> i32 = handle
                .object()
                .method(slots::igatt_characteristic::VALUE_CHANGED);
            f(handle.object().as_raw(), handler.as_raw(), &mut token);
        }
        self.value_handlers.lock().unwrap().insert(key, handler);
    }

    pub fn subscribe(&self, handle: &Handle) -> webbluetooth_core::backlog::Receiver<Vec<u8>> {
        // Bounded: a notification arrives on a platform callback
        // thread that must return promptly, so there is nobody to
        // apply backpressure to. See `webbluetooth_core::backlog`.
        let (tx, rx) = webbluetooth_core::backlog::channel();
        self.notifications
            .lock()
            .unwrap()
            .entry(handle.object().key())
            .or_default()
            .push(tx);
        rx
    }

    pub async fn read_descriptor(&self, id: &str, handle: &Handle) -> Result<Vec<u8>> {
        let _guard = self.gatt_lock(id).await?;
        let operation = invoke_async(handle.object(), slots::igatt_descriptor::READ_VALUE_ASYNC)
            .ok_or_else(|| Error::Network("ReadValueAsync was refused".into()))?;
        let result = self
            .await_operation(operation, class_signature(classes::GATT_READ_RESULT))
            .await?;
        let buffer = ble::get_object(&result, slots::igatt_read_result::VALUE)
            .ok_or_else(|| Error::Network("the read returned no buffer".into()))?;
        let bytes = ble::buffer_to_bytes(&buffer)
            .ok_or_else(|| Error::Network("the read buffer could not be copied".into()))?;
        *handle.0.value.lock().unwrap() = Some(bytes.clone());
        Ok(bytes)
    }

    pub async fn write_descriptor(&self, id: &str, handle: &Handle, value: &[u8]) -> Result<()> {
        // A descriptor write is always an ATT Write Request, so it always
        // takes the lock — there is no unacknowledged form of it.
        let _guard = self.gatt_lock(id).await?;
        let buffer = ble::bytes_to_buffer(value)
            .ok_or_else(|| Error::Network("could not build a write buffer".into()))?;

        // The raw out-parameter is confined to this block. A bare
        // `*mut c_void` alive across an await makes the whole future
        // `!Send` — `ComPtr` is `Send`, the pointer it is built from is not.
        let operation = {
            let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
            let hr = unsafe {
                let f: unsafe extern "system" fn(
                    *mut std::ffi::c_void,
                    *mut std::ffi::c_void,
                    *mut *mut std::ffi::c_void,
                ) -> i32 = handle
                    .object()
                    .method(slots::igatt_descriptor::WRITE_VALUE_ASYNC);
                f(handle.object().as_raw(), buffer.as_raw(), &mut operation)
            };
            (crate::com::succeeded(hr))
                .then(|| unsafe { ComPtr::adopt(operation) })
                .flatten()
        }
        .ok_or_else(|| Error::Network("WriteValueAsync was refused".into()))?;

        self.await_operation(
            operation,
            Signature::Enum {
                name: "Windows.Devices.Bluetooth.GenericAttributeProfile.GattCommunicationStatus",
                signed: true,
            },
        )
        .await?;
        Ok(())
    }

    pub async fn read_rssi(&self, id: &str) -> Result<i32> {
        // WinRT has no equivalent of `readRSSI`: signal strength arrives only
        // on an advertisement. So this reports the last advertisement's
        // reading, which is what BlueZ's cached `RSSI` property amounts to as
        // well — but only while it is recent enough to mean anything, rather
        // than handing back a measurement from some earlier minute as though
        // it described the link now.
        const MAX_AGE: std::time::Duration = std::time::Duration::from_secs(30);

        let recent = self.sightings.lock().unwrap().get(id).and_then(|s| {
            (s.at.elapsed() <= MAX_AGE && s.rssi != webbluetooth_core::filter::UNAVAILABLE_RSSI)
                .then_some(s.rssi)
        });
        if let Some(rssi) = recent {
            return Ok(rssi);
        }
        Err(Error::NotSupported(
            "Windows reports RSSI only on an advertisement, and this device has \
             not been seen recently — scan, or use requestLEScan to track it"
                .into(),
        ))
    }

    /// `BluetoothLEDevice.RequestPreferredConnectionParameters`.
    ///
    /// The three presets WinRT offers line up with our three priorities, so
    /// nothing has to be expressed in connection intervals. `IBluetoothLEDevice6`
    /// arrived in Windows 10 2004, so an older build fails the cast — which is
    /// reported as unsupported rather than as an error.
    pub async fn request_connection_priority(
        &self,
        id: &str,
        priority: webbluetooth_core::ConnectionPriority,
    ) -> Result<()> {
        use webbluetooth_core::ConnectionPriority as P;
        let _guard = self.gatt_lock(id).await?;

        let device = self
            .devices
            .get(id, |d| d.inner.device.clone())?
            .ok_or_else(|| Error::InvalidState("the device is not connected".into()))?;
        let device6 = device
            .cast(interfaces::I_BLUETOOTH_LE_DEVICE6)
            .ok_or_else(|| {
                Error::NotSupported(
                    "IBluetoothLEDevice6 is unavailable; connection parameters need \
                     Windows 10 2004 or newer"
                        .into(),
                )
            })?;

        // The presets are static properties on the parameters class.
        let statics = ble::statics(
            classes::BLUETOOTH_LE_PREFERRED_CONNECTION_PARAMETERS,
            interfaces::I_BLUETOOTH_LE_PREFERRED_CONNECTION_PARAMETERS_STATICS,
        )
        .ok_or_else(|| {
            Error::NotSupported("BluetoothLEPreferredConnectionParameters is unavailable".into())
        })?;
        let slot = match priority {
            P::Balanced => slots::ibluetooth_lepreferred_connection_parameters_statics::BALANCED,
            P::High => {
                slots::ibluetooth_lepreferred_connection_parameters_statics::THROUGHPUT_OPTIMIZED
            }
            P::LowPower => {
                slots::ibluetooth_lepreferred_connection_parameters_statics::POWER_OPTIMIZED
            }
        };
        let preset = ble::get_object(&statics, slot)
            .ok_or_else(|| Error::NotSupported("the connection preset was refused".into()))?;

        // Synchronous, unlike most of WinRT: it returns the request object
        // rather than an operation to await.
        let mut request: *mut std::ffi::c_void = std::ptr::null_mut();
        let hr = unsafe {
            let f: unsafe extern "system" fn(
                *mut std::ffi::c_void,
                *mut std::ffi::c_void,
                *mut *mut std::ffi::c_void,
            ) -> i32 = device6
                .method(slots::ibluetooth_ledevice6::REQUEST_PREFERRED_CONNECTION_PARAMETERS);
            f(device6.as_raw(), preset.as_raw(), &mut request)
        };
        let request = (crate::com::succeeded(hr))
            .then(|| unsafe { ComPtr::adopt(request) })
            .flatten()
            .ok_or_else(|| {
                Error::Network("RequestPreferredConnectionParameters was refused".into())
            })?;

        // Status 0 is `Unspecified`; anything else means Windows took a view.
        // The peripheral still decides, so a successful request says only that
        // the ask was made.
        let _ = ble::get_i32(
            &request,
            slots::ibluetooth_lepreferred_connection_parameters_request::STATUS,
        );
        Ok(())
    }

    pub async fn request_mtu(&self, _id: &str, _mtu: u16) -> Result<()> {
        Err(Error::NotSupported(
            "WinRT exposes the negotiated PDU size but no MTU request API".into(),
        ))
    }

    pub fn max_write_len(&self, id: &str, _write_type: WriteType) -> Result<usize> {
        // Three bytes of ATT header come off the negotiated PDU size. Falling
        // back to the guaranteed minimum rather than failing: a write that is
        // shorter than it could be still works, whereas refusing to write at
        // all because the session could not be read does not.
        Ok(self.max_pdu_size(id).unwrap_or(ATT_DEFAULT_MTU) as usize - 3)
    }

    /// `GattSession.MaxPduSize` — the negotiated ATT MTU.
    ///
    /// WinRT does not put this on the device: a `GattSession` has to be opened
    /// for it, which needs the `BluetoothDeviceId` off a later interface than
    /// the one carrying the rest of the device. Three hops for one number,
    /// which is why the result is cached by the caller rather than read per
    /// write.
    fn max_pdu_size(&self, id: &str) -> Option<u16> {
        self.pdu_sizes.lock().unwrap().get(id).copied()
    }

    /// Open a `GattSession` and remember what it says, once per connection.
    async fn read_max_pdu_size(&self, id: &str) -> Option<u16> {
        let device = self.device(id).ok()?;
        let device4 = device.cast(interfaces::I_BLUETOOTH_LE_DEVICE4)?;
        let device_id =
            ble::get_object(&device4, slots::ibluetooth_ledevice4::BLUETOOTH_DEVICE_ID)?;

        let statics = ble::statics(classes::GATT_SESSION, interfaces::I_GATT_SESSION_STATICS)?;
        // The raw out-parameter is confined to this block. A bare
        // `*mut c_void` alive across an await makes the whole future
        // `!Send` — `ComPtr` is `Send`, the pointer it is built from is not.
        let operation = {
            let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
            let hr = unsafe {
                let f: unsafe extern "system" fn(
                    *mut std::ffi::c_void,
                    *mut std::ffi::c_void,
                    *mut *mut std::ffi::c_void,
                ) -> i32 = statics.method(slots::igatt_session_statics::FROM_DEVICE_ID_ASYNC);
                f(statics.as_raw(), device_id.as_raw(), &mut operation)
            };
            (crate::com::succeeded(hr))
                .then(|| unsafe { ComPtr::adopt(operation) })
                .flatten()
        }?;
        let session = self
            .await_operation(operation, class_signature(classes::GATT_SESSION))
            .await
            .ok()?;
        let size = ble::get_u16(&session, slots::igatt_session::MAX_PDU_SIZE)?;
        // The session is dropped here. It was opened to read one property, and
        // holding it would ask Windows to maintain the connection.
        self.pdu_sizes.lock().unwrap().insert(id.to_owned(), size);
        Some(size)
    }
}

/// Call a no-argument method that returns an `IAsyncOperation`.
fn invoke_async(object: &ComPtr, slot: usize) -> Option<ComPtr> {
    let mut operation: *mut std::ffi::c_void = std::ptr::null_mut();
    let hr = unsafe {
        let f: unsafe extern "system" fn(*mut std::ffi::c_void, *mut *mut std::ffi::c_void) -> i32 =
            object.method(slot);
        f(object.as_raw(), &mut operation)
    };
    crate::com::succeeded(hr)
        .then(|| unsafe { ComPtr::adopt(operation) })
        .flatten()
}

/// Read an `IVectorView<Guid>`, whose elements are values rather than objects.
fn read_guid_vector(vector: &ComPtr) -> Vec<Guid> {
    let mut size: u32 = 0;
    let ok = unsafe {
        let f: unsafe extern "system" fn(*mut std::ffi::c_void, *mut u32) -> i32 =
            vector.method(slots::ivector_view::SIZE);
        crate::com::succeeded(f(vector.as_raw(), &mut size))
    };
    if !ok {
        return Vec::new();
    }
    (0..size)
        .filter_map(|index| {
            let mut guid = Guid::default();
            let hr = unsafe {
                let f: unsafe extern "system" fn(*mut std::ffi::c_void, u32, *mut Guid) -> i32 =
                    vector.method(slots::ivector_view::GET_AT);
                f(vector.as_raw(), index, &mut guid)
            };
            crate::com::succeeded(hr).then_some(guid)
        })
        .collect()
}

/// Turn a `GattCommunicationStatus` into the spec's vocabulary.
fn gatt_error(what: &str, status: i32) -> Error {
    match status {
        1 => Error::Network(format!("{what}: the device is unreachable")),
        2 => Error::Security(format!("{what}: the protocol refused it")),
        3 => Error::Security(format!("{what}: access is denied — is the device paired?")),
        _ => Error::Network(format!("{what} failed (status {status})")),
    }
}

/// Whether this process may use Bluetooth.
///
/// Windows has no per-application Bluetooth permission for a desktop process —
/// a packaged app declares a capability, and a plain one does not. Reaching the
/// stack at all is the honest answer.
pub fn authorization() -> webbluetooth_core::Authorization {
    match ble::statics(
        classes::BLUETOOTH_LE_DEVICE,
        interfaces::I_BLUETOOTH_LE_DEVICE_STATICS,
    ) {
        Some(_) => webbluetooth_core::Authorization::Allowed,
        None => webbluetooth_core::Authorization::Denied,
    }
}