npm.io
1.0.3 • Published 1 year ago

capacitor-usb-serial

Licence
MIT
Version
1.0.3
Deps
0
Size
49 kB
Vulns
0
Weekly
0
DeprecatedThis package is deprecated

capacitor-usb-serial

This plugin provides a simple interface for USB serial communication in Capacitor applications. It allows you to connect to USB devices, send and receive data, and manage multiple device connections.

The plugin only supports Android devices and it uses usb-serial-for-android under the hood.

If you want to contribute the apple part feel free to open an issue / PR on the github repo.

Install

npm install capacitor-usb-serial
npx cap sync

API

Defines the interface for USB serial communication plugin

getDeviceHandlers()
getDeviceHandlers() => Promise<DeviceHandler[]>

Returns an array of DeviceHandler objects for all connected USB devices. This helper function provides a simplified interface for interacting with the plugin methods. It is recommended for most use cases unless more granular control is required. Each DeviceHandler object includes methods for connecting, disconnecting, writing to, and reading from a specific device.

Returns: Promise<DeviceHandler[]>


getDeviceConnections()
getDeviceConnections() => Promise<{ devices: DeviceInfo[]; }>

Returns all connected devices

Returns: Promise<{ devices: DeviceInfo[]; }>


openConnection(...)
openConnection(options: FullConnectionParams) => Promise<{ portKey: string; }>

Connect to a device using its deviceId

Param Type Description
options FullConnectionParams - Connection parameters including deviceId

Returns: Promise<{ portKey: string; }>


getActivePorts()
getActivePorts() => Promise<{ ports: string[]; }>

Returns all active ports

Returns: Promise<{ ports: string[]; }>


endConnection(...)
endConnection(options: { key: string; }) => Promise<void>

Disconnect from a device using its assigned portKey

Param Type Description
options { key: string; } - Object containing the portKey

endConnections(...)
endConnections(options?: { keys?: string[] | undefined; } | undefined) => Promise<void>

Disconnect from all devices or specified devices

Param Type Description
options { keys?: string[]; } - Optional object containing an array of portKeys to disconnect

write(...)
write(options: { key: string; message: string; noRead?: boolean }) => Promise<ReadResponse>

Write a message to a device using its assigned portKey and performs a quick read straight afterwards. If noRead is passed as true the read would be skipped.

Param Type Description
options { key: string; message: string; noRead?: boolean } - Object containing the portKey and message to write

read(...)
read(options: { key: string; }) => Promise<ReadResponse>

Read a message from a device using its assigned portKey

Param Type Description
options { key: string; } - Object containing the portKey

Returns: Promise<ReadResponse>


Interfaces
DeviceHandler

Provides a simplified interface for handling a specific device

Method Type Description
connect (options?: ConnectionParams) => Promise Connect to the device
disconnect () => Promise Disconnect from the device
write (message: string) => Promise Write a message to the device
read () => Promise Read a message from the device
DeviceInfo

Represents information about a connected device

Prop Type Description
deviceKey string Unique identifier used internally
deviceId number Numeric identifier for the device
productId number Product ID of the device
vendorId number Vendor ID of the device
deviceName string Human-readable name of the device
FullConnectionParams

Extends ConnectionParams to include deviceId

Prop Type Description
deviceId number Unique identifier for the device
Type Aliases
ReadResponse

Represents the response from a read operation

{ data: string, bytesRead: number }