Skip to content

Commit

Permalink
fix webusb interop by dropping ondisconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
dangfan committed Dec 21, 2024
1 parent 601f303 commit adcf850
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/flutter_nfc_kit_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'package:flutter_nfc_kit/webusb_interop.dart';
class FlutterNfcKitWeb {
static void registerWith(Registrar registrar) {
final MethodChannel channel = MethodChannel(
'flutter_nfc_kit',
'flutter_nfc_kit/method',
const StandardMethodCodec(),
registrar,
);
Expand Down
27 changes: 10 additions & 17 deletions lib/webusb_interop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,26 @@ final log = Logger('FlutterNFCKit:WebUSB');
const int USB_CLASS_CODE_VENDOR_SPECIFIC = 0xFF;

@JS('navigator.usb')
class _USB {
external static dynamic requestDevice(_USBDeviceRequestOptions options);
// ignore: unused_field
external static Function ondisconnect;
extension type _USB._(JSObject _) implements JSObject {
external static JSObject requestDevice(_USBDeviceRequestOptions options);
}

@JS()
@anonymous
class _USBDeviceRequestOptions {
external factory _USBDeviceRequestOptions({List<_USBDeviceFilter> filters});
extension type _USBDeviceRequestOptions._(JSObject _) implements JSObject {
external factory _USBDeviceRequestOptions(
{JSArray<_USBDeviceFilter> filters});
}

@JS()
@anonymous
class _USBDeviceFilter {
extension type _USBDeviceFilter._(JSObject _) implements JSObject {
external factory _USBDeviceFilter({int classCode});
}

@JS()
@anonymous
class _USBControlTransferParameters {
extension type _USBControlTransferParameters._(JSObject _) implements JSObject {
external factory _USBControlTransferParameters(
{String requestType,
String recipient,
Expand All @@ -61,28 +60,22 @@ class WebUSB {
return _device != null && getProperty(_device, 'opened');
}

static void _onDisconnect(event) {
_device = null;
log.info('device is disconnected from WebUSB API');
}

static const USB_PROBE_MAGIC = '_NFC_IM_';

/// Try to poll a WebUSB device according to our protocol.
static Future<String> poll(int timeout, bool probeMagic) async {
// request WebUSB device with custom classcode
if (!_deviceAvailable()) {
var devicePromise = _USB.requestDevice(_USBDeviceRequestOptions(filters: [
_USBDeviceFilter(classCode: USB_CLASS_CODE_VENDOR_SPECIFIC)
]));
var devicePromise = _USB.requestDevice(_USBDeviceRequestOptions(
filters: [_USBDeviceFilter(classCode: USB_CLASS_CODE_VENDOR_SPECIFIC)]
.toJS));
dynamic device = await promiseToFuture(devicePromise);
try {
await promiseToFuture(callMethod(device, 'open', List.empty()))
.then((_) =>
promiseToFuture(callMethod(device, 'claimInterface', [1])))
.timeout(Duration(milliseconds: timeout));
_device = device;
_USB.ondisconnect = allowInterop(_onDisconnect);
log.info("WebUSB device opened", _device);
} on TimeoutException catch (_) {
log.severe("Polling tag timeout");
Expand Down

0 comments on commit adcf850

Please sign in to comment.