Skip to content

Commit 3febb2c

Browse files
committed
Add onDisconnect back to webusb interop
Signed-off-by: Shengqi Chen <harry-chen@outlook.com>
1 parent adcf850 commit 3febb2c

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lib/webusb_interop.dart

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const int USB_CLASS_CODE_VENDOR_SPECIFIC = 0xFF;
2323
@JS('navigator.usb')
2424
extension type _USB._(JSObject _) implements JSObject {
2525
external static JSObject requestDevice(_USBDeviceRequestOptions options);
26+
external static set ondisconnect(JSFunction value);
2627
}
2728

2829
@JS()
@@ -60,6 +61,11 @@ class WebUSB {
6061
return _device != null && getProperty(_device, 'opened');
6162
}
6263

64+
static void _onDisconnect() {
65+
_device = null;
66+
log.info('device is disconnected from WebUSB API');
67+
}
68+
6369
static const USB_PROBE_MAGIC = '_NFC_IM_';
6470

6571
/// Try to poll a WebUSB device according to our protocol.
@@ -76,6 +82,7 @@ class WebUSB {
7682
promiseToFuture(callMethod(device, 'claimInterface', [1])))
7783
.timeout(Duration(milliseconds: timeout));
7884
_device = device;
85+
_USB.ondisconnect = _onDisconnect.toJS;
7986
log.info("WebUSB device opened", _device);
8087
} on TimeoutException catch (_) {
8188
log.severe("Polling tag timeout");

0 commit comments

Comments
 (0)