Skip to content

Commit 60ee184

Browse files
Send actual path within EventData (#49)
* Send actual path within EventData Also fixed some type declarations * Remove path from listener signature Co-authored-by: Basten Gao <[email protected]> * Call listener without path Co-authored-by: Basten Gao <[email protected]> * Fix `send` return value Co-authored-by: Basten Gao <[email protected]> * Add contributors --------- Co-authored-by: Basten Gao <[email protected]>
1 parent 8699e8c commit 60ee184

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

android/src/main/java/com/bastengao/serialport/SerialPortWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public void run() {
4848
byte[] data = Arrays.copyOf(buffer, size);
4949
String hex = SerialPortApiModule.bytesToHex(data);
5050
event.putString("data", hex);
51+
event.putString("path", path);
5152
sender.sendEvent(DataReceivedEvent, event);
5253
}
5354
} catch (IOException e) {

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
"name": "bastengao",
3636
"email": "[email protected]"
3737
},
38+
"contributors": [
39+
{
40+
"name": "Alessandro Raffa @ Infobiotech",
41+
"email": "[email protected]",
42+
"url": "https://github.com/infobiotech"
43+
}
44+
],
3845
"license": "MIT",
3946
"licenseFilename": "LICENSE",
4047
"readmeFilename": "README.md",

src/API.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default class API {
7777
* @param {openOptions} options
7878
* @returns {Promise<SerialPort>} connected serial port
7979
*/
80-
static open(devicePath: string, {baudRate, parity = 0, dataBits = 8, stopBits = 1}: { baudRate: number, parity?: number, dataBits?: number, stopBits?: number }) {
80+
static open(devicePath: string, {baudRate, parity = 0, dataBits = 8, stopBits = 1}: { baudRate: number, parity?: number, dataBits?: number, stopBits?: number }): Promise<SerialPort> {
8181
if (Platform.OS !== 'android') throw new Error(`Not support ${Platform.OS}`)
8282
return SerialPortAPI.open(devicePath, baudRate, parity, dataBits, stopBits)
8383
.then((serialPort: SerialPortWrapper) => {

src/SerialPort.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface SerialPortWrapper {
1010

1111
export interface EventData {
1212
data: string;
13+
path: string;
1314
}
1415

1516
export interface ListenerProxy {
@@ -64,7 +65,7 @@ class SerialPort {
6465
* @param {string} hex the hex of data
6566
* @returns {Promise} success promise
6667
*/
67-
send(hex: string) {
68+
send(hex: string): Promise<boolean> {
6869
return SerialPortAPI.send(this.path, hex)
6970
}
7071

@@ -73,8 +74,11 @@ class SerialPort {
7374
* @param {listener} listener
7475
* @returns {Subscription} subscription
7576
*/
76-
onReceived(listener: (data: Buffer) => void): EventSubscription {
77+
onReceived(listener: (buffer: Buffer) => void): EventSubscription {
7778
const listenerProxy = (event: EventData) => {
79+
if (!event.path || this.path !== event.path) {
80+
return;
81+
}
7882
if (!event.data) {
7983
return;
8084
}

0 commit comments

Comments
 (0)