1.0.2 • Published 10 months ago

capacitor-usb-serial v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

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

ParamTypeDescription
optionsFullConnectionParams- 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

ParamTypeDescription
options{ key: string; }- Object containing the portKey

endConnections(...)

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

Disconnect from all devices or specified devices

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

write(...)

write(options: { key: string; message: string; }) => Promise<void>

Write a message to a device using its assigned portKey

ParamTypeDescription
options{ key: string; message: string; }- 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

ParamTypeDescription
options{ key: string; }- Object containing the portKey

Returns: Promise<ReadResponse>


Interfaces

DeviceHandler

Provides a simplified interface for handling a specific device

MethodTypeDescription
connect(options?: ConnectionParams) => PromiseConnect to the device
disconnect() => PromiseDisconnect from the device
write(message: string) => PromiseWrite a message to the device
read() => PromiseRead a message from the device

DeviceInfo

Represents information about a connected device

PropTypeDescription
deviceKeystringUnique identifier used internally
deviceIdnumberNumeric identifier for the device
productIdnumberProduct ID of the device
vendorIdnumberVendor ID of the device
deviceNamestringHuman-readable name of the device

FullConnectionParams

Extends ConnectionParams to include deviceId

PropTypeDescription
deviceIdnumberUnique identifier for the device

Type Aliases

ReadResponse

Represents the response from a read operation

{ data: string, bytesRead: number }

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago

0.0.2

11 months ago

0.0.1

11 months ago