Dart wrapper via dart:js
for https://wicg.github.io/webusb/
- canUseUsb
- getDevices/requestDevice
- subscribeConnect/unsubscribeConnect
- subscribeDisconnect/unsubscribeDisconnect
- open/close
- reset
- selectConfiguration
- claimInterface/releaseInterface
- transferIn/transferOut
bool canUse = canUseUsb();
print('canUse $canUse');
List<UsbDevice> getDevices = await usb.getDevices();
_device = getDevices[0];
UsbDevice requestDevice = await usb.requestDevice(RequestOptions(
filters: [ledgerDeviceIds],
));
_device = requestDevice;
https://developer.mozilla.org/en-US/docs/Web/API/usb/onconnect
final EventListener _handleConnect = allowInterop((Event event) {}
...
usb.subscribeConnect(_handleConnect);
...
usb.unsubscribeConnect(_handleConnect);
https://developer.mozilla.org/en-US/docs/Web/API/usb/ondisconnect
final EventListener _handleDisconnect = allowInterop((Event event) {}
...
usb.subscribeDisconnect(_handleDisconnect);
...
usb.unsubscribeDisconnect(_handleDisconnect);
_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');
});
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');
});
https://developer.mozilla.org/en-US/docs/Web/API/USBDevice/selectConfiguration
await _device?.selectConfiguration(configurationValue);
_configuration = _device?.configuration;
_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');
});
var usbInTransferResult = await _device!.transferIn(endpointNumber, packetSize);
print('usbInTransferResult ${usbInTransferResult.data.lengthInBytes}');
var usbOutTransferResult = await _device?.transferOut(endpointNumber, makeBlock(getAppAndVersion));
print('usbOutTransferResult ${usbOutTransferResult?.bytesWritten}');
Status in Chromium: https://chromestatus.com/feature/5651917954875392