Dart Documentationchrome.usbChromeUsb

ChromeUsb class

class ChromeUsb extends ChromeApi {
 static final JsObject _usb = chrome['usb'];

 ChromeUsb._();

 bool get available => _usb != null;

 /**
  * Lists USB devices specified by vendorId/productId/interfaceId tuple.
  * [options]: The properties to search for on target devices.
  * [callback]: Invoked with a list of [Device]s on complete.
  */
 Future<List<Device>> getDevices(EnumerateDevicesOptions options) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter<List<Device>>.oneArg((e) => listify(e, _createDevice));
   _usb.callMethod('getDevices', [jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * This method is ChromeOS specific. Calling this method on other platforms
  * will fail. Requests access from the permission broker to an OS claimed
  * device if the given interface on the device is not claimed.
  * 
  * [device]: The device to request access to.
  * [interfaceId]:
  */
 Future<bool> requestAccess(Device device, int interfaceId) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter<bool>.oneArg();
   _usb.callMethod('requestAccess', [jsify(device), interfaceId, completer.callback]);
   return completer.future;
 }

 /**
  * Opens a USB device returned by [getDevices].
  * [device]: The device to open.
  * [callback]: Invoked with the created ConnectionHandle on complete.
  */
 Future<ConnectionHandle> openDevice(Device device) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter<ConnectionHandle>.oneArg(_createConnectionHandle);
   _usb.callMethod('openDevice', [jsify(device), completer.callback]);
   return completer.future;
 }

 /**
  * Finds USB devices specified by the vendorId/productId/interfaceId tuple
  * and, if permissions allow, opens them for use.
  * 
  * On Chrome OS, you can specify the interfaceId. In that case the method will
  * request access from permission broker in the same way as in
  * [requestUsbAcess].
  * 
  * If the access request is rejected, or the device is failed to be opened,
  * its connection handle will not be created or returned.
  * 
  * Calling this method is equivalent to calling [getDevices] followed by a
  * series of [requestAccess] (if it is on ChromeOs) and [openDevice] calls,
  * and returning all the successfully opened connection handles.
  * 
  * [options]: The properties to search for on target devices.
  * [callback]: Invoked with the opened ConnectionHandle on complete.
  */
 Future<List<ConnectionHandle>> findDevices(EnumerateDevicesAndRequestAccessOptions options) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter<List<ConnectionHandle>>.oneArg((e) => listify(e, _createConnectionHandle));
   _usb.callMethod('findDevices', [jsify(options), completer.callback]);
   return completer.future;
 }

 /**
  * Closes a connection handle. Invoking operations on a device after it has
  * been closed is a safe operation, but causes no action to be taken.
  * [handle]: The connection handle to close.
  * [callback]: The callback to invoke once the device is closed.
  */
 Future closeDevice(ConnectionHandle handle) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _usb.callMethod('closeDevice', [jsify(handle), completer.callback]);
   return completer.future;
 }

 /**
  * Lists all the interfaces on the USB device.
  * [handle]: The device from which the interfaces should be listed.
  * [callback]: The callback to invoke when the interfaces are enumerated.
  */
 Future<List<InterfaceDescriptor>> listInterfaces(ConnectionHandle handle) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter<List<InterfaceDescriptor>>.oneArg((e) => listify(e, _createInterfaceDescriptor));
   _usb.callMethod('listInterfaces', [jsify(handle), completer.callback]);
   return completer.future;
 }

 /**
  * Claims an interface on the specified USB device. Before you can transfer
  * data with endpoints, you must claim their parent interfaces. Only one
  * connection handle on the same host can claim each interface. If the
  * interface is already claimed, this call will fail.
  * 
  * You shall call releaseInterface when the interface is not needed anymore.
  * 
  * [handle]: The device on which the interface is to be claimed.
  * [interface]: The interface number to be claimed.
  * [callback]: The callback to invoke once the interface is claimed.
  */
 Future claimInterface(ConnectionHandle handle, int interfaceNumber) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _usb.callMethod('claimInterface', [jsify(handle), interfaceNumber, completer.callback]);
   return completer.future;
 }

 /**
  * Releases a claim to an interface on the provided device.
  * [handle]: The device on which the interface is to be released.
  * [interface]: The interface number to be released.
  * [callback]: The callback to invoke once the interface is released.
  */
 Future releaseInterface(ConnectionHandle handle, int interfaceNumber) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _usb.callMethod('releaseInterface', [jsify(handle), interfaceNumber, completer.callback]);
   return completer.future;
 }

 /**
  * Selects an alternate setting on a previously claimed interface on a device.
  * [handle]: The device on which the interface settings are to be set.
  * [interface]: The interface number to be set.
  * [alternateSetting]: The alternate setting to set.
  * [callback]: The callback to invoke once the interface setting is set.
  */
 Future setInterfaceAlternateSetting(ConnectionHandle handle, int interfaceNumber, int alternateSetting) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _usb.callMethod('setInterfaceAlternateSetting', [jsify(handle), interfaceNumber, alternateSetting, completer.callback]);
   return completer.future;
 }

 /**
  * Performs a control transfer on the specified device. See the
  * ControlTransferInfo structure for the parameters required to make a
  * transfer.
  * 
  * Conceptually control transfer talks to the device itself. You do not need
  * to claim interface 0 to perform a control transfer.
  * 
  * [handle]: A connection handle to make the transfer on.
  * [transferInfo]: The parameters to the transfer. See ControlTransferInfo.
  * [callback]: Invoked once the transfer has completed.
  */
 Future<TransferResultInfo> controlTransfer(ConnectionHandle handle, ControlTransferInfo transferInfo) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter<TransferResultInfo>.oneArg(_createTransferResultInfo);
   _usb.callMethod('controlTransfer', [jsify(handle), jsify(transferInfo), completer.callback]);
   return completer.future;
 }

 /**
  * Performs a bulk transfer on the specified device.
  * [handle]: A connection handle to make the transfer on.
  * [transferInfo]: The parameters to the transfer. See GenericTransferInfo.
  * [callback]: Invoked once the transfer has completed.
  */
 Future<TransferResultInfo> bulkTransfer(ConnectionHandle handle, GenericTransferInfo transferInfo) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter<TransferResultInfo>.oneArg(_createTransferResultInfo);
   _usb.callMethod('bulkTransfer', [jsify(handle), jsify(transferInfo), completer.callback]);
   return completer.future;
 }

 /**
  * Performs an interrupt transfer on the specified device.
  * [handle]: A connection handle to make the transfer on.
  * [transferInfo]: The parameters to the transfer. See GenericTransferInfo.
  * [callback]: Invoked once the transfer has completed.
  */
 Future<TransferResultInfo> interruptTransfer(ConnectionHandle handle, GenericTransferInfo transferInfo) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter<TransferResultInfo>.oneArg(_createTransferResultInfo);
   _usb.callMethod('interruptTransfer', [jsify(handle), jsify(transferInfo), completer.callback]);
   return completer.future;
 }

 /**
  * Performs an isochronous transfer on the specific device.
  * [handle]: A connection handle to make the transfer on.
  * [transferInfo]: The parameters to the transfer. See
  * IsochronousTransferInfo.
  * [callback]: Invoked once the transfer has been completed.
  */
 Future<TransferResultInfo> isochronousTransfer(ConnectionHandle handle, IsochronousTransferInfo transferInfo) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter<TransferResultInfo>.oneArg(_createTransferResultInfo);
   _usb.callMethod('isochronousTransfer', [jsify(handle), jsify(transferInfo), completer.callback]);
   return completer.future;
 }

 /**
  * Tries to reset the USB device and restores it to the previous status. If
  * the reset fails, the given connection handle will be closed and the USB
  * device will appear to be disconnected then reconnected. In that case you
  * must call [getDevices] or [findDevices] again to acquire the device.
  * 
  * [handle]: A connection handle to reset.
  * [callback]: Invoked once the device is reset with a boolean indicating
  * whether the reset is completed successfully.
  */
 Future<bool> resetDevice(ConnectionHandle handle) {
   if (_usb == null) _throwNotAvailable();

   var completer = new ChromeCompleter<bool>.oneArg();
   _usb.callMethod('resetDevice', [jsify(handle), completer.callback]);
   return completer.future;
 }

 void _throwNotAvailable() {
   throw new UnsupportedError("'chrome.usb' is not available");
 }
}

Extends

ChromeApi > ChromeUsb

Properties

final bool available #

Returns true if the API is available. The common causes of an API not being avilable are:

  • a permission is missing in the application's manifest.json file
  • the API is defined on a newer version of Chrome then the current runtime
docs inherited from ChromeApi
bool get available => _usb != null;

Methods

Future<TransferResultInfo> bulkTransfer(ConnectionHandle handle, GenericTransferInfo transferInfo) #

Performs a bulk transfer on the specified device. handle: A connection handle to make the transfer on. transferInfo: The parameters to the transfer. See GenericTransferInfo. callback: Invoked once the transfer has completed.

Future<TransferResultInfo> bulkTransfer(ConnectionHandle handle, GenericTransferInfo transferInfo) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter<TransferResultInfo>.oneArg(_createTransferResultInfo);
 _usb.callMethod('bulkTransfer', [jsify(handle), jsify(transferInfo), completer.callback]);
 return completer.future;
}

Future claimInterface(ConnectionHandle handle, int interfaceNumber) #

Claims an interface on the specified USB device. Before you can transfer data with endpoints, you must claim their parent interfaces. Only one connection handle on the same host can claim each interface. If the interface is already claimed, this call will fail.

You shall call releaseInterface when the interface is not needed anymore.

handle: The device on which the interface is to be claimed. interface: The interface number to be claimed. callback: The callback to invoke once the interface is claimed.

Future claimInterface(ConnectionHandle handle, int interfaceNumber) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _usb.callMethod('claimInterface', [jsify(handle), interfaceNumber, completer.callback]);
 return completer.future;
}

Future closeDevice(ConnectionHandle handle) #

Closes a connection handle. Invoking operations on a device after it has been closed is a safe operation, but causes no action to be taken. handle: The connection handle to close. callback: The callback to invoke once the device is closed.

Future closeDevice(ConnectionHandle handle) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _usb.callMethod('closeDevice', [jsify(handle), completer.callback]);
 return completer.future;
}

Future<TransferResultInfo> controlTransfer(ConnectionHandle handle, ControlTransferInfo transferInfo) #

Performs a control transfer on the specified device. See the ControlTransferInfo structure for the parameters required to make a transfer.

Conceptually control transfer talks to the device itself. You do not need to claim interface 0 to perform a control transfer.

handle: A connection handle to make the transfer on. transferInfo: The parameters to the transfer. See ControlTransferInfo. callback: Invoked once the transfer has completed.

Future<TransferResultInfo> controlTransfer(ConnectionHandle handle, ControlTransferInfo transferInfo) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter<TransferResultInfo>.oneArg(_createTransferResultInfo);
 _usb.callMethod('controlTransfer', [jsify(handle), jsify(transferInfo), completer.callback]);
 return completer.future;
}

Future<List<ConnectionHandle>> findDevices(EnumerateDevicesAndRequestAccessOptions options) #

Finds USB devices specified by the vendorId/productId/interfaceId tuple and, if permissions allow, opens them for use.

On Chrome OS, you can specify the interfaceId. In that case the method will request access from permission broker in the same way as in requestUsbAcess.

If the access request is rejected, or the device is failed to be opened, its connection handle will not be created or returned.

Calling this method is equivalent to calling getDevices followed by a series of requestAccess (if it is on ChromeOs) and openDevice calls, and returning all the successfully opened connection handles.

options: The properties to search for on target devices. callback: Invoked with the opened ConnectionHandle on complete.

Future<List<ConnectionHandle>> findDevices(EnumerateDevicesAndRequestAccessOptions options) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter<List<ConnectionHandle>>.oneArg((e) => listify(e, _createConnectionHandle));
 _usb.callMethod('findDevices', [jsify(options), completer.callback]);
 return completer.future;
}

Future<List<Device>> getDevices(EnumerateDevicesOptions options) #

Lists USB devices specified by vendorId/productId/interfaceId tuple. options: The properties to search for on target devices. callback: Invoked with a list of Devices on complete.

Future<List<Device>> getDevices(EnumerateDevicesOptions options) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter<List<Device>>.oneArg((e) => listify(e, _createDevice));
 _usb.callMethod('getDevices', [jsify(options), completer.callback]);
 return completer.future;
}

Future<TransferResultInfo> interruptTransfer(ConnectionHandle handle, GenericTransferInfo transferInfo) #

Performs an interrupt transfer on the specified device. handle: A connection handle to make the transfer on. transferInfo: The parameters to the transfer. See GenericTransferInfo. callback: Invoked once the transfer has completed.

Future<TransferResultInfo> interruptTransfer(ConnectionHandle handle, GenericTransferInfo transferInfo) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter<TransferResultInfo>.oneArg(_createTransferResultInfo);
 _usb.callMethod('interruptTransfer', [jsify(handle), jsify(transferInfo), completer.callback]);
 return completer.future;
}

Future<TransferResultInfo> isochronousTransfer(ConnectionHandle handle, IsochronousTransferInfo transferInfo) #

Performs an isochronous transfer on the specific device. handle: A connection handle to make the transfer on. transferInfo: The parameters to the transfer. See IsochronousTransferInfo. callback: Invoked once the transfer has been completed.

Future<TransferResultInfo> isochronousTransfer(ConnectionHandle handle, IsochronousTransferInfo transferInfo) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter<TransferResultInfo>.oneArg(_createTransferResultInfo);
 _usb.callMethod('isochronousTransfer', [jsify(handle), jsify(transferInfo), completer.callback]);
 return completer.future;
}

Future<List<InterfaceDescriptor>> listInterfaces(ConnectionHandle handle) #

Lists all the interfaces on the USB device. handle: The device from which the interfaces should be listed. callback: The callback to invoke when the interfaces are enumerated.

Future<List<InterfaceDescriptor>> listInterfaces(ConnectionHandle handle) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter<List<InterfaceDescriptor>>.oneArg((e) => listify(e, _createInterfaceDescriptor));
 _usb.callMethod('listInterfaces', [jsify(handle), completer.callback]);
 return completer.future;
}

Future<ConnectionHandle> openDevice(Device device) #

Opens a USB device returned by getDevices. device: The device to open. callback: Invoked with the created ConnectionHandle on complete.

Future<ConnectionHandle> openDevice(Device device) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter<ConnectionHandle>.oneArg(_createConnectionHandle);
 _usb.callMethod('openDevice', [jsify(device), completer.callback]);
 return completer.future;
}

Future releaseInterface(ConnectionHandle handle, int interfaceNumber) #

Releases a claim to an interface on the provided device. handle: The device on which the interface is to be released. interface: The interface number to be released. callback: The callback to invoke once the interface is released.

Future releaseInterface(ConnectionHandle handle, int interfaceNumber) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _usb.callMethod('releaseInterface', [jsify(handle), interfaceNumber, completer.callback]);
 return completer.future;
}

Future<bool> requestAccess(Device device, int interfaceId) #

This method is ChromeOS specific. Calling this method on other platforms will fail. Requests access from the permission broker to an OS claimed device if the given interface on the device is not claimed.

device: The device to request access to. interfaceId:

Future<bool> requestAccess(Device device, int interfaceId) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter<bool>.oneArg();
 _usb.callMethod('requestAccess', [jsify(device), interfaceId, completer.callback]);
 return completer.future;
}

Future<bool> resetDevice(ConnectionHandle handle) #

Tries to reset the USB device and restores it to the previous status. If the reset fails, the given connection handle will be closed and the USB device will appear to be disconnected then reconnected. In that case you must call getDevices or findDevices again to acquire the device.

handle: A connection handle to reset. callback: Invoked once the device is reset with a boolean indicating whether the reset is completed successfully.

Future<bool> resetDevice(ConnectionHandle handle) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter<bool>.oneArg();
 _usb.callMethod('resetDevice', [jsify(handle), completer.callback]);
 return completer.future;
}

Future setInterfaceAlternateSetting(ConnectionHandle handle, int interfaceNumber, int alternateSetting) #

Selects an alternate setting on a previously claimed interface on a device. handle: The device on which the interface settings are to be set. interface: The interface number to be set. alternateSetting: The alternate setting to set. callback: The callback to invoke once the interface setting is set.

Future setInterfaceAlternateSetting(ConnectionHandle handle, int interfaceNumber, int alternateSetting) {
 if (_usb == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _usb.callMethod('setInterfaceAlternateSetting', [jsify(handle), interfaceNumber, alternateSetting, completer.callback]);
 return completer.future;
}