Dart wrapper via dart:js for https://wicg.github.io/webusb/

Features

  • canUseUsb
  • getDevices/requestDevice
  • subscribeConnect/unsubscribeConnect
  • subscribeDisconnect/unsubscribeDisconnect
  • open/close
  • reset
  • selectConfiguration
  • claimInterface/releaseInterface
  • transferIn/transferOut

Usage

canUseUsb

bool canUse = canUseUsb();
print('canUse $canUse');

getDevices/requestDevice

List<UsbDevice> getDevices = await usb.getDevices();
_device = getDevices[0];
UsbDevice requestDevice = await usb.requestDevice(RequestOptions(
  filters: [ledgerDeviceIds],
));
_device = requestDevice;

subscribeConnect/unsubscribeConnect

https://developer.mozilla.org/en-US/docs/Web/API/usb/onconnect

final EventListener _handleConnect = allowInterop((Event event) {}
...
usb.subscribeConnect(_handleConnect);
...
usb.unsubscribeConnect(_handleConnect);

subscribeDisconnect/unsubscribeDisconnect

https://developer.mozilla.org/en-US/docs/Web/API/usb/ondisconnect

final EventListener _handleDisconnect = allowInterop((Event event) {}
...
usb.subscribeDisconnect(_handleDisconnect);
...
usb.unsubscribeDisconnect(_handleDisconnect);

open/close

_device?.open().then((value) {
  print('device.open success');
}).catchError((error) {
  print('device.open $error');
});
_device?.close().then((value) {
  print('device.close success');
}).catchError((error) {
  print('device.close $error');
});

reset

https://developer.mozilla.org/en-US/docs/Web/API/USBDevice/reset

_device?.reset().then((value) {
  print('device.reset success');
}).catchError((error) {
  print('device.reset $error');
});

selectConfiguration

https://developer.mozilla.org/en-US/docs/Web/API/USBDevice/selectConfiguration

await _device?.selectConfiguration(configurationValue);
_configuration = _device?.configuration;

claimInterface/releaseInterface

_device?.claimInterface(_interface!.interfaceNumber).then((value) {
  print('device.claimInterface success');
}).catchError((error) {
  print('device.claimInterface $error');
});
_device?.releaseInterface(_interface!.interfaceNumber).then((value) {
  print('device.releaseInterface success');
}).catchError((error) {
  print('device.releaseInterface $error');
});

transferIn/transferOut

var usbInTransferResult = await _device!.transferIn(endpointNumber, packetSize);
print('usbInTransferResult ${usbInTransferResult.data.lengthInBytes}');
var usbOutTransferResult = await _device?.transferOut(endpointNumber, makeBlock(getAppAndVersion));
print('usbOutTransferResult ${usbOutTransferResult?.bytesWritten}');

Additional information

Status in Chromium: https://chromestatus.com/feature/5651917954875392

Libraries

web_usb