@rhinosw/react-native-usb-serial 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +477 -0
- package/android/build.gradle +69 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/usbserial/UsbSerialModule.kt +301 -0
- package/android/src/main/java/com/usbserial/UsbSerialPackage.kt +31 -0
- package/lib/module/NativeUsbSerial.js +8 -0
- package/lib/module/NativeUsbSerial.js.map +1 -0
- package/lib/module/index.js +107 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeUsbSerial.d.ts +93 -0
- package/lib/typescript/src/NativeUsbSerial.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +60 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +176 -0
- package/src/NativeUsbSerial.ts +120 -0
- package/src/index.tsx +121 -0
package/package.json
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rhinosw/react-native-usb-serial",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Production-ready React Native USB serial communication module for Android with Modbus RTU soil sensor support",
|
|
5
|
+
"main": "./lib/module/index.js",
|
|
6
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"source": "./src/index.tsx",
|
|
10
|
+
"types": "./lib/typescript/src/index.d.ts",
|
|
11
|
+
"default": "./lib/module/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"lib",
|
|
18
|
+
"android",
|
|
19
|
+
"ios",
|
|
20
|
+
"cpp",
|
|
21
|
+
"*.podspec",
|
|
22
|
+
"react-native.config.js",
|
|
23
|
+
"!ios/build",
|
|
24
|
+
"!android/build",
|
|
25
|
+
"!android/gradle",
|
|
26
|
+
"!android/gradlew",
|
|
27
|
+
"!android/gradlew.bat",
|
|
28
|
+
"!android/local.properties",
|
|
29
|
+
"!**/__tests__",
|
|
30
|
+
"!**/__fixtures__",
|
|
31
|
+
"!**/__mocks__",
|
|
32
|
+
"!**/.*"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"example": "yarn workspace react-native-usb-serial-example",
|
|
36
|
+
"clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
|
|
37
|
+
"prepare": "bob build",
|
|
38
|
+
"typecheck": "tsc",
|
|
39
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
40
|
+
"test": "jest",
|
|
41
|
+
"release": "release-it --only-version"
|
|
42
|
+
},
|
|
43
|
+
"keywords": [
|
|
44
|
+
"react-native",
|
|
45
|
+
"android",
|
|
46
|
+
"usb",
|
|
47
|
+
"serial",
|
|
48
|
+
"communication",
|
|
49
|
+
"modbus",
|
|
50
|
+
"rtu",
|
|
51
|
+
"soil-sensor",
|
|
52
|
+
"iot",
|
|
53
|
+
"hardware"
|
|
54
|
+
],
|
|
55
|
+
"repository": {
|
|
56
|
+
"type": "git",
|
|
57
|
+
"url": "git+https://github.com/RhinoLance/react-native-usb-serial.git"
|
|
58
|
+
},
|
|
59
|
+
"author": "Rezaul Karim <devrhinolance.official@gmail.com> (https://github.com/RhinoLance)",
|
|
60
|
+
"license": "MIT",
|
|
61
|
+
"bugs": {
|
|
62
|
+
"url": "https://github.com/RhinoLance/react-native-usb-serial/issues"
|
|
63
|
+
},
|
|
64
|
+
"homepage": "https://github.com/RhinoLance/react-native-usb-serial#readme",
|
|
65
|
+
"publishConfig": {
|
|
66
|
+
"registry": "https://registry.npmjs.org/"
|
|
67
|
+
},
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">=16.0.0"
|
|
70
|
+
},
|
|
71
|
+
"peerDependencies": {
|
|
72
|
+
"react": "*",
|
|
73
|
+
"react-native": "*"
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
77
|
+
"@eslint/compat": "^1.3.2",
|
|
78
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
79
|
+
"@eslint/js": "^9.35.0",
|
|
80
|
+
"@react-native/babel-preset": "0.83.0",
|
|
81
|
+
"@react-native/eslint-config": "0.83.0",
|
|
82
|
+
"@release-it/conventional-changelog": "^10.0.1",
|
|
83
|
+
"@types/jest": "^29.5.14",
|
|
84
|
+
"@types/react": "^19.2.0",
|
|
85
|
+
"commitlint": "^19.8.1",
|
|
86
|
+
"del-cli": "^6.0.0",
|
|
87
|
+
"eslint": "^9.35.0",
|
|
88
|
+
"jest": "^29.7.0",
|
|
89
|
+
"lefthook": "^2.0.3",
|
|
90
|
+
"react": "19.2.0",
|
|
91
|
+
"react-native": "0.83.0",
|
|
92
|
+
"react-native-builder-bob": "^0.40.13",
|
|
93
|
+
"release-it": "^19.0.4",
|
|
94
|
+
"turbo": "^2.5.6",
|
|
95
|
+
"typescript": "^5.9.2"
|
|
96
|
+
},
|
|
97
|
+
"workspaces": [
|
|
98
|
+
"example"
|
|
99
|
+
],
|
|
100
|
+
"packageManager": "yarn@4.11.0",
|
|
101
|
+
"react-native-builder-bob": {
|
|
102
|
+
"source": "src",
|
|
103
|
+
"output": "lib",
|
|
104
|
+
"targets": [
|
|
105
|
+
[
|
|
106
|
+
"module",
|
|
107
|
+
{
|
|
108
|
+
"esm": true
|
|
109
|
+
}
|
|
110
|
+
],
|
|
111
|
+
[
|
|
112
|
+
"typescript",
|
|
113
|
+
{
|
|
114
|
+
"project": "tsconfig.build.json"
|
|
115
|
+
}
|
|
116
|
+
]
|
|
117
|
+
]
|
|
118
|
+
},
|
|
119
|
+
"codegenConfig": {
|
|
120
|
+
"name": "UsbSerialSpec",
|
|
121
|
+
"type": "modules",
|
|
122
|
+
"jsSrcsDir": "src",
|
|
123
|
+
"android": {
|
|
124
|
+
"javaPackageName": "com.usbserial"
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
"prettier": {
|
|
128
|
+
"quoteProps": "consistent",
|
|
129
|
+
"singleQuote": true,
|
|
130
|
+
"tabWidth": 2,
|
|
131
|
+
"trailingComma": "es5",
|
|
132
|
+
"useTabs": false
|
|
133
|
+
},
|
|
134
|
+
"jest": {
|
|
135
|
+
"preset": "react-native",
|
|
136
|
+
"modulePathIgnorePatterns": [
|
|
137
|
+
"<rootDir>/example/node_modules",
|
|
138
|
+
"<rootDir>/lib/"
|
|
139
|
+
]
|
|
140
|
+
},
|
|
141
|
+
"commitlint": {
|
|
142
|
+
"extends": [
|
|
143
|
+
"@commitlint/config-conventional"
|
|
144
|
+
]
|
|
145
|
+
},
|
|
146
|
+
"release-it": {
|
|
147
|
+
"git": {
|
|
148
|
+
"commitMessage": "chore: release ${version}",
|
|
149
|
+
"tagName": "v${version}"
|
|
150
|
+
},
|
|
151
|
+
"npm": {
|
|
152
|
+
"publish": true
|
|
153
|
+
},
|
|
154
|
+
"github": {
|
|
155
|
+
"release": true
|
|
156
|
+
},
|
|
157
|
+
"plugins": {
|
|
158
|
+
"@release-it/conventional-changelog": {
|
|
159
|
+
"preset": {
|
|
160
|
+
"name": "angular"
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
"create-react-native-library": {
|
|
166
|
+
"type": "turbo-module",
|
|
167
|
+
"languages": "kotlin-objc",
|
|
168
|
+
"tools": [
|
|
169
|
+
"eslint",
|
|
170
|
+
"jest",
|
|
171
|
+
"lefthook",
|
|
172
|
+
"release-it"
|
|
173
|
+
],
|
|
174
|
+
"version": "0.57.2"
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { TurboModuleRegistry, type TurboModule , Platform} from 'react-native';
|
|
2
|
+
|
|
3
|
+
export type UsbDevice = {
|
|
4
|
+
deviceName: string;
|
|
5
|
+
vendorId: number;
|
|
6
|
+
productId: number;
|
|
7
|
+
manufacturer?: string | null;
|
|
8
|
+
productName?: string | null;
|
|
9
|
+
serialNumber?: string | null;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type SoilData = {
|
|
13
|
+
[key: string]: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type RawReadConfig = {
|
|
17
|
+
bufferSize?: number;
|
|
18
|
+
timeout?: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type SoilSensorConfig = {
|
|
22
|
+
slaveId?: number;
|
|
23
|
+
startAddress?: number;
|
|
24
|
+
registerCount?: number;
|
|
25
|
+
responseDelayMs?: number;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export interface Spec extends TurboModule {
|
|
29
|
+
/**
|
|
30
|
+
* Multiply two numbers
|
|
31
|
+
*/
|
|
32
|
+
multiply(a: number, b: number): number;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Returns all connected USB devices
|
|
36
|
+
*/
|
|
37
|
+
getDeviceList(): UsbDevice[];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Check if app has permission for a specific device
|
|
41
|
+
*/
|
|
42
|
+
hasPermission(device: UsbDevice): Promise<boolean>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Request USB permission for a specific device
|
|
46
|
+
*/
|
|
47
|
+
requestUsbPermission(device: UsbDevice): Promise<boolean>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Connect to a specific USB device with optional baud rate
|
|
51
|
+
*/
|
|
52
|
+
connect(device: UsbDevice, baudRate?: number): Promise<boolean>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Check if serial connection is active
|
|
56
|
+
*/
|
|
57
|
+
isConnected(): Promise<boolean>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Disconnect the current serial device
|
|
61
|
+
*/
|
|
62
|
+
disconnect(): Promise<void>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Write data to the serial port
|
|
66
|
+
*/
|
|
67
|
+
write(data: string): Promise<void>;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Read data from the serial port
|
|
71
|
+
*/
|
|
72
|
+
read(bufferSize?: number, timeout?: number): Promise<string>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Start listening for raw serial data at specified interval
|
|
76
|
+
*/
|
|
77
|
+
onReadInterval(intervalMs: number, config?: RawReadConfig): Promise<void>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Stop listening for raw serial data
|
|
81
|
+
*/
|
|
82
|
+
offReadInterval(): Promise<void>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get currently connected device
|
|
86
|
+
*/
|
|
87
|
+
getConnectedDevice(): Promise<UsbDevice | null>;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Read soil sensor data once
|
|
91
|
+
*/
|
|
92
|
+
readSoilData(config?: SoilSensorConfig): Promise<SoilData | null>;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Start listening for soil sensor data at specified interval
|
|
96
|
+
*/
|
|
97
|
+
onReadSoilDataInterval(
|
|
98
|
+
intervalMs: number,
|
|
99
|
+
config?: SoilSensorConfig
|
|
100
|
+
): Promise<void>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Stop listening for soil sensor data
|
|
104
|
+
*/
|
|
105
|
+
offReadSoilDataInterval(): Promise<void>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export type UsbSerialEvent = {
|
|
109
|
+
data: string;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export type UsbSoilEvent = {
|
|
113
|
+
[key: string]: number;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
function getModule(): Spec {
|
|
117
|
+
return Platform.OS === 'android' ? TurboModuleRegistry.getEnforcing<Spec>('UsbSerial') : {} as Spec;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export default getModule();
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import UsbSerial, {
|
|
2
|
+
type UsbDevice,
|
|
3
|
+
type SoilData,
|
|
4
|
+
type RawReadConfig,
|
|
5
|
+
type SoilSensorConfig,
|
|
6
|
+
} from './NativeUsbSerial';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Get list of all connected USB devices
|
|
10
|
+
*/
|
|
11
|
+
export function getDeviceList(): UsbDevice[] {
|
|
12
|
+
return UsbSerial.getDeviceList();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Check if app has permission for a device
|
|
17
|
+
*/
|
|
18
|
+
export function hasPermission(device: UsbDevice): Promise<boolean> {
|
|
19
|
+
return UsbSerial.hasPermission(device);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Request USB permission for a device
|
|
24
|
+
*/
|
|
25
|
+
export function requestUsbPermission(device: UsbDevice): Promise<boolean> {
|
|
26
|
+
return UsbSerial.requestUsbPermission(device);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Connect to a USB device
|
|
31
|
+
*/
|
|
32
|
+
export function connect(
|
|
33
|
+
device: UsbDevice,
|
|
34
|
+
baudRate?: number
|
|
35
|
+
): Promise<boolean> {
|
|
36
|
+
return UsbSerial.connect(device, baudRate);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Check if currently connected
|
|
41
|
+
*/
|
|
42
|
+
export function isConnected(): Promise<boolean> {
|
|
43
|
+
return UsbSerial.isConnected();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Disconnect from device
|
|
48
|
+
*/
|
|
49
|
+
export function disconnect(): Promise<void> {
|
|
50
|
+
return UsbSerial.disconnect();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Write data to serial port
|
|
55
|
+
*/
|
|
56
|
+
export function write(data: string): Promise<void> {
|
|
57
|
+
return UsbSerial.write(data);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Read data from serial port
|
|
62
|
+
*/
|
|
63
|
+
export function read(bufferSize?: number, timeout?: number): Promise<string> {
|
|
64
|
+
return UsbSerial.read(bufferSize, timeout);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Start listening for raw serial data
|
|
69
|
+
*/
|
|
70
|
+
export function onReadInterval(
|
|
71
|
+
intervalMs: number,
|
|
72
|
+
config?: RawReadConfig
|
|
73
|
+
): Promise<void> {
|
|
74
|
+
return UsbSerial.onReadInterval(intervalMs, config);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Stop listening for raw serial data
|
|
79
|
+
*/
|
|
80
|
+
export function offReadInterval(): Promise<void> {
|
|
81
|
+
return UsbSerial.offReadInterval();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get currently connected device
|
|
86
|
+
*/
|
|
87
|
+
export function getConnectedDevice(): Promise<UsbDevice | null> {
|
|
88
|
+
return UsbSerial.getConnectedDevice();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Read soil sensor data once
|
|
93
|
+
*/
|
|
94
|
+
export function readSoilData(
|
|
95
|
+
config?: SoilSensorConfig
|
|
96
|
+
): Promise<SoilData | null> {
|
|
97
|
+
return UsbSerial.readSoilData(config);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Start listening for soil sensor data
|
|
102
|
+
*/
|
|
103
|
+
export function onReadSoilDataInterval(
|
|
104
|
+
intervalMs: number,
|
|
105
|
+
config?: SoilSensorConfig
|
|
106
|
+
): Promise<void> {
|
|
107
|
+
return UsbSerial.onReadSoilDataInterval(intervalMs, config);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Stop listening for soil sensor data
|
|
112
|
+
*/
|
|
113
|
+
export function offReadSoilDataInterval(): Promise<void> {
|
|
114
|
+
return UsbSerial.offReadSoilDataInterval();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Export types
|
|
118
|
+
export type { UsbDevice, SoilData, RawReadConfig, SoilSensorConfig };
|
|
119
|
+
|
|
120
|
+
// Export native module
|
|
121
|
+
export default UsbSerial;
|