@eugabrielsilva/capacitor-usb-serial
v0.0.4
Published
Capacitor-USB-Serial is a native plugin for Capacitor that enables communication with USB serial devices on Android and iOS platforms. Seamlessly connect to IoT devices, microcontrollers, and embedded systems to receive real-time data streams directly int
Downloads
581
Maintainers
Readme
capacitor-usb-serial
Capacitor-USB-Serial is a native plugin for Capacitor that enables communication with USB serial devices on Android and iOS platforms. Seamlessly connect to IoT devices, microcontrollers, and embedded systems to receive real-time data streams directly into your cross-platform mobile apps for telemetry monitoring, drone tracking, or hardware integration applications.
Install
npm install capacitor-usb-serial
npx cap syncAPI
requestPermission(...)listDevices()connect(...)disconnect()write(...)read()startListening()stopListening()addListener('dataReceived', ...)addListener('connectionStateChanged', ...)addListener('deviceAttached', ...)addListener('deviceDetached', ...)addListener('error', ...)- Interfaces
requestPermission(...)
requestPermission(options?: { deviceId?: number | undefined; } | undefined) => Promise<{ granted: boolean; }>Request permission to access USB devices
| Param | Type |
| ------------- | ----------------------------------- |
| options | { deviceId?: number; } |
Returns: Promise<{ granted: boolean; }>
listDevices()
listDevices() => Promise<{ devices: UsbDevice[]; }>List all connected USB devices
Returns: Promise<{ devices: UsbDevice[]; }>
connect(...)
connect(options: { deviceId?: number; serialOptions?: UsbSerialOptions; }) => Promise<{ connected: boolean; }>Connect to a USB device
| Param | Type |
| ------------- | ----------------------------------------------------------------------------------------------------- |
| options | { deviceId?: number; serialOptions?: UsbSerialOptions; } |
Returns: Promise<{ connected: boolean; }>
disconnect()
disconnect() => Promise<{ disconnected: boolean; }>Disconnect from the current device
Returns: Promise<{ disconnected: boolean; }>
write(...)
write(options: { data: string; }) => Promise<{ bytesWritten: number; }>Write data to the serial port
| Param | Type |
| ------------- | ------------------------------ |
| options | { data: string; } |
Returns: Promise<{ bytesWritten: number; }>
read()
read() => Promise<{ data: string; }>Read data from the serial port
Returns: Promise<{ data: string; }>
startListening()
startListening() => Promise<void>Start listening for data
stopListening()
stopListening() => Promise<void>Stop listening for data
addListener('dataReceived', ...)
addListener(eventName: 'dataReceived', listenerFunc: (event: DataReceivedEvent) => void) => Promise<{ remove: () => void; }>Add listener for data received events
| Param | Type |
| ------------------ | ----------------------------------------------------------------------------------- |
| eventName | 'dataReceived' |
| listenerFunc | (event: DataReceivedEvent) => void |
Returns: Promise<{ remove: () => void; }>
addListener('connectionStateChanged', ...)
addListener(eventName: 'connectionStateChanged', listenerFunc: (event: { connected: boolean; deviceId?: number; }) => void) => Promise<{ remove: () => void; }>Add listener for connection state changes
| Param | Type |
| ------------------ | --------------------------------------------------------------------------- |
| eventName | 'connectionStateChanged' |
| listenerFunc | (event: { connected: boolean; deviceId?: number; }) => void |
Returns: Promise<{ remove: () => void; }>
addListener('deviceAttached', ...)
addListener(eventName: 'deviceAttached', listenerFunc: (event: DeviceAttachedEvent) => void) => Promise<{ remove: () => void; }>Add listener for device attached events
| Param | Type |
| ------------------ | --------------------------------------------------------------------------------------- |
| eventName | 'deviceAttached' |
| listenerFunc | (event: DeviceAttachedEvent) => void |
Returns: Promise<{ remove: () => void; }>
addListener('deviceDetached', ...)
addListener(eventName: 'deviceDetached', listenerFunc: (event: DeviceDetachedEvent) => void) => Promise<{ remove: () => void; }>Add listener for device detached events
| Param | Type |
| ------------------ | --------------------------------------------------------------------------------------- |
| eventName | 'deviceDetached' |
| listenerFunc | (event: DeviceDetachedEvent) => void |
Returns: Promise<{ remove: () => void; }>
addListener('error', ...)
addListener(eventName: 'error', listenerFunc: (event: ErrorEvent) => void) => Promise<{ remove: () => void; }>Add listener for error events
| Param | Type |
| ------------------ | --------------------------------------------------------------------- |
| eventName | 'error' |
| listenerFunc | (event: ErrorEvent) => void |
Returns: Promise<{ remove: () => void; }>
Interfaces
UsbDevice
| Prop | Type |
| ---------------------- | ------------------- |
| deviceId | number |
| vendorId | number |
| productId | number |
| deviceName | string |
| manufacturerName | string |
| serialNumber | string |
UsbSerialOptions
| Prop | Type |
| -------------- | ----------------------------------------------------------- |
| baudRate | number |
| dataBits | number |
| stopBits | number |
| parity | 'none' | 'odd' | 'even' | 'mark' | 'space' |
| dtr | boolean |
| rts | boolean |
| protocol | string |
| host | string |
| port | number |
DataReceivedEvent
| Prop | Type |
| --------------- | ------------------- |
| data | string |
| hexData | string |
| timestamp | number |
| deviceId | number |
DeviceAttachedEvent
| Prop | Type |
| ---------------------- | ------------------- |
| deviceId | number |
| vendorId | number |
| productId | number |
| deviceName | string |
| manufacturerName | string |
| serialNumber | string |
DeviceDetachedEvent
| Prop | Type |
| -------------- | ------------------- |
| deviceId | number |
ErrorEvent
| Prop | Type |
| ------------- | ------------------- |
| message | string |
