ant-plus-next / WebUsbStick
WebUsbStick class extends the WebUSBDriver to represent a specific USB stick that uses the WebUSB API. It checks for WebUSB API availability in the environment before initializing.
WebUSBDriver
new WebUsbStick():
WebUsbStick
Creates an instance of WebUsbStick. Throws an error if the WebUSB API is not available in the current environment (e.g., unsupported browsers).
If the WebUSB API is not available in the environment.
try {
const usbStick = new WebUsbStick();
// Use usbStick for further operations
} catch (error) {
console.error(error.message);
}
WebUSBDriver.constructor
_canScan:
boolean
=false
Indicates whether the device can scan.
WebUSBDriver._canScan
core/driver/webUSBDriver.ts:95
maxChannels:
number
=0
The maximum number of channels available for communication.
WebUSBDriver.maxChannels
core/driver/webUSBDriver.ts:89
usedChannels:
number
=0
The number of channels currently used.
WebUSBDriver.usedChannels
core/driver/webUSBDriver.ts:62
attach(
sensor
,forScan
):Promise
<boolean
>
Attaches a sensor to the driver and assigns it a channel.
The sensor to attach.
boolean
Whether the sensor is being attached for scanning.
Promise
<boolean
>
Resolves with true if the sensor was successfully attached, otherwise false.
WebUSBDriver.attach
core/driver/webUSBDriver.ts:326
canAttach():
Promise
<boolean
>
Checks if a new sensor can be attached to the driver. It verifies whether the current number of used channels is less than the maximum available channels.
Promise
<boolean
>
Resolves with true if a new sensor can be attached, otherwise false.
const canAttach = await this.stick.canAttach();
if (canAttach) {
console.log("A new sensor can be attached.");
} else {
console.log("Cannot attach sensor: Maximum number of channels reached.");
}
WebUSBDriver.canAttach
core/driver/webUSBDriver.ts:121
cancelReadLoop():
void
Cancels the current read loop by aborting the signal.
void
WebUSBDriver.cancelReadLoop
core/driver/webUSBDriver.ts:265
canScan():
Promise
<boolean
>
Checks if the device can scan for channels.
Promise
<boolean
>
Resolves with true if the device can scan, otherwise false.
WebUSBDriver.canScan
core/driver/webUSBDriver.ts:130
close():
Promise
<void
>
Closes the connection to the USB device and cleans up.
Promise
<void
>
Resolves when the device is closed.
WebUSBDriver.close
core/driver/webUSBDriver.ts:241
detach(
sensor
):Promise
<boolean
>
Detaches a sensor from the driver.
The sensor to detach.
Promise
<boolean
>
Resolves with true if the sensor was successfully detached, otherwise false.
WebUSBDriver.detach
core/driver/webUSBDriver.ts:350
isPresent():
Promise
<boolean
>
Checks if a device is currently connected.
Promise
<boolean
>
Resolves with true if a device is present, otherwise false.
WebUSBDriver.isPresent
core/driver/webUSBDriver.ts:367
isScanning():
Promise
<boolean
>
Checks if the device is currently scanning.
Promise
<boolean
>
Resolves with true if the device is scanning, otherwise false.
WebUSBDriver.isScanning
core/driver/webUSBDriver.ts:376
open():
Promise
<boolean
>
Opens a connection to the USB device and initializes the endpoints.
Promise
<boolean
>
Resolves with true if the device was successfully opened, otherwise false.
WebUSBDriver.open
core/driver/webUSBDriver.ts:139
read(
data
):Promise
<void
>
Processes the data received from the USB device.
Uint8Array
<ArrayBufferLike
>
The data received from the USB device.
Promise
<void
>
Resolves when the data has been processed.
WebUSBDriver.read
core/driver/webUSBDriver.ts:278
reset():
Promise
<void
>
Resets the device and channels, and sends a reset message to the system.
Promise
<void
>
Resolves when the reset is completed.
WebUSBDriver.reset
core/driver/webUSBDriver.ts:312
write(
data
):Promise
<void
>
Writes data to the USB device.
Uint8Array
<ArrayBufferLike
>
The data to be sent to the USB device.
Promise
<void
>
Resolves when the data has been written.
WebUSBDriver.write