@@ -21,27 +21,26 @@ final log = Logger('FlutterNFCKit:WebUSB');
21
21
const int USB_CLASS_CODE_VENDOR_SPECIFIC = 0xFF ;
22
22
23
23
@JS ('navigator.usb' )
24
- class _USB {
25
- external static dynamic requestDevice (_USBDeviceRequestOptions options);
26
- // ignore: unused_field
27
- external static Function ondisconnect;
24
+ extension type _USB ._(JSObject _) implements JSObject {
25
+ external static JSObject requestDevice (_USBDeviceRequestOptions options);
28
26
}
29
27
30
28
@JS ()
31
29
@anonymous
32
- class _USBDeviceRequestOptions {
33
- external factory _USBDeviceRequestOptions ({List <_USBDeviceFilter > filters});
30
+ extension type _USBDeviceRequestOptions ._(JSObject _) implements JSObject {
31
+ external factory _USBDeviceRequestOptions (
32
+ {JSArray <_USBDeviceFilter > filters});
34
33
}
35
34
36
35
@JS ()
37
36
@anonymous
38
- class _USBDeviceFilter {
37
+ extension type _USBDeviceFilter ._( JSObject _) implements JSObject {
39
38
external factory _USBDeviceFilter ({int classCode});
40
39
}
41
40
42
41
@JS ()
43
42
@anonymous
44
- class _USBControlTransferParameters {
43
+ extension type _USBControlTransferParameters ._( JSObject _) implements JSObject {
45
44
external factory _USBControlTransferParameters (
46
45
{String requestType,
47
46
String recipient,
@@ -61,28 +60,22 @@ class WebUSB {
61
60
return _device != null && getProperty (_device, 'opened' );
62
61
}
63
62
64
- static void _onDisconnect (event) {
65
- _device = null ;
66
- log.info ('device is disconnected from WebUSB API' );
67
- }
68
-
69
63
static const USB_PROBE_MAGIC = '_NFC_IM_' ;
70
64
71
65
/// Try to poll a WebUSB device according to our protocol.
72
66
static Future <String > poll (int timeout, bool probeMagic) async {
73
67
// request WebUSB device with custom classcode
74
68
if (! _deviceAvailable ()) {
75
- var devicePromise = _USB .requestDevice (_USBDeviceRequestOptions (filters : [
76
- _USBDeviceFilter (classCode: USB_CLASS_CODE_VENDOR_SPECIFIC )
77
- ] ));
69
+ var devicePromise = _USB .requestDevice (_USBDeviceRequestOptions (
70
+ filters : [ _USBDeviceFilter (classCode: USB_CLASS_CODE_VENDOR_SPECIFIC )]
71
+ .toJS ));
78
72
dynamic device = await promiseToFuture (devicePromise);
79
73
try {
80
74
await promiseToFuture (callMethod (device, 'open' , List .empty ()))
81
75
.then ((_) =>
82
76
promiseToFuture (callMethod (device, 'claimInterface' , [1 ])))
83
77
.timeout (Duration (milliseconds: timeout));
84
78
_device = device;
85
- _USB .ondisconnect = allowInterop (_onDisconnect);
86
79
log.info ("WebUSB device opened" , _device);
87
80
} on TimeoutException catch (_) {
88
81
log.severe ("Polling tag timeout" );
0 commit comments