Skip to content

Commit 188a51b

Browse files
committed
Initial cleanup
1 parent 4c0399f commit 188a51b

File tree

3 files changed

+113
-89
lines changed

3 files changed

+113
-89
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"android:release": "vite build && node capacitor.config.generator.mjs && npx cap build android --release",
2323
"format": "prettier --write {src,test}/**/*.{js,vue,css,less}",
2424
"storybook": "start-storybook -p 6006",
25-
"prepare": "husky install"
25+
"prepare": "husky install",
26+
"postinstall": "patch-package"
2627
},
2728
"window": {
2829
"icon": "images/bf_icon_128.png",
@@ -116,7 +117,9 @@
116117
"lint-staged": "^15.4.3",
117118
"nw-builder": "^3.8.6",
118119
"os": "^0.1.2",
120+
"patch-package": "^8.0.1",
119121
"postcss": "^8.5.3",
122+
"postinstall-postinstall": "^2.1.0",
120123
"prettier": "^3.5.2",
121124
"rollup": "^4.34.9",
122125
"rollup-plugin-copy": "^3.5.0",

src/js/protocols/CapacitorSerial.js

Lines changed: 9 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class CapacitorSerialProtocol extends EventTarget {
2222

2323
this.connect = this.connect.bind(this);
2424
this.disconnect = this.disconnect.bind(this);
25-
this.handleReceiveBytes = this.handleReceiveBytes.bind(this);
2625
this.handleDataEvent = this.handleDataEvent.bind(this);
2726
this.handleConnectedEvent = this.handleConnectedEvent.bind(this);
2827
this.handleAttachedEvent = this.handleAttachedEvent.bind(this);
@@ -49,32 +48,26 @@ class CapacitorSerialProtocol extends EventTarget {
4948
}
5049

5150
handleDataEvent(event) {
52-
console.log(`${logHead} Data event received:`, event);
5351
if (event?.data) {
54-
console.log(`${logHead} Raw data from plugin:`, event.data, typeof event.data);
55-
// Convert hex string to Uint8Array (like WebSerial does)
52+
// Convert hex string from plugin to Uint8Array
5653
const uint8Array = this.hexStringToUint8Array(event.data);
57-
console.log(`${logHead} Received ${uint8Array.byteLength} bytes:`, uint8Array);
5854
this.bytesReceived += uint8Array.byteLength;
5955
this.dispatchEvent(new CustomEvent("receive", { detail: uint8Array }));
60-
} else {
61-
console.warn(`${logHead} Data event received but no data property:`, event);
6256
}
6357
}
6458

6559
handleConnectedEvent(event) {
66-
console.log(`${logHead} Device connected:`, event);
6760
this.connected = true;
6861
}
6962

7063
handleAttachedEvent(event) {
71-
console.log(`${logHead} Device attached:`, event);
7264
const added = this.handleNewDevice(event);
73-
this.dispatchEvent(new CustomEvent("addedDevice", { detail: added }));
65+
if (added) {
66+
this.dispatchEvent(new CustomEvent("addedDevice", { detail: added }));
67+
}
7468
}
7569

7670
handleDetachedEvent(event) {
77-
console.log(`${logHead} Device detached:`, event);
7871
this.handleDeviceRemoval(event);
7972
}
8073

@@ -83,46 +76,24 @@ class CapacitorSerialProtocol extends EventTarget {
8376
this.dispatchEvent(new CustomEvent("error", { detail: event.error }));
8477
}
8578

86-
hexStringToArrayBuffer(hexString) {
87-
// Remove any spaces or non-hex characters
88-
const cleanHex = hexString.replace(/[^0-9A-Fa-f]/g, "");
89-
const length = cleanHex.length / 2;
90-
const buffer = new ArrayBuffer(length);
91-
const view = new Uint8Array(buffer);
92-
93-
for (let i = 0; i < length; i++) {
94-
view[i] = parseInt(cleanHex.substr(i * 2, 2), 16);
95-
}
96-
97-
return buffer;
98-
}
99-
10079
hexStringToUint8Array(hexString) {
101-
// Remove any spaces or non-hex characters
102-
const cleanHex = hexString.replace(/[^0-9A-Fa-f]/g, "");
103-
const length = cleanHex.length / 2;
80+
const length = hexString.length / 2;
10481
const uint8Array = new Uint8Array(length);
10582

10683
for (let i = 0; i < length; i++) {
107-
uint8Array[i] = parseInt(cleanHex.substr(i * 2, 2), 16);
84+
uint8Array[i] = parseInt(hexString.substr(i * 2, 2), 16);
10885
}
10986

11087
return uint8Array;
11188
}
11289

11390
arrayBufferToHexString(buffer) {
114-
// Handle both Uint8Array and ArrayBuffer
11591
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
11692
return Array.from(bytes)
11793
.map((b) => b.toString(16).padStart(2, "0"))
11894
.join("");
11995
}
12096

121-
handleReceiveBytes(info) {
122-
console.log(`${logHead} Received ${info.detail.byteLength} bytes`);
123-
this.bytesReceived += info.detail.byteLength;
124-
}
125-
12697
getConnectedPort() {
12798
return this.connectionId;
12899
}
@@ -131,7 +102,6 @@ class CapacitorSerialProtocol extends EventTarget {
131102
const added = this.createPort(device);
132103
if (added) {
133104
this.ports.push(added);
134-
this.dispatchEvent(new CustomEvent("addedDevice", { detail: added }));
135105
}
136106
return added;
137107
}
@@ -159,28 +129,22 @@ class CapacitorSerialProtocol extends EventTarget {
159129
if (index !== -1) {
160130
const removed = this.ports.splice(index, 1)[0];
161131
this.dispatchEvent(new CustomEvent("removedDevice", { detail: removed }));
162-
console.log(`${logHead} Device removed: VID:${device.vendorId} PID:${device.productId}`);
163132
}
164133
}
165134

166135
async loadDevices() {
167136
try {
168137
const result = await UsbSerial.connectedDevices();
169-
console.log(`${logHead} connectedDevices result:`, result);
170138
this.ports = [];
171139

172140
if (result?.devices && Array.isArray(result.devices)) {
173141
for (const device of result.devices) {
174-
const vid = device.device?.vendorId;
175-
const pid = device.device?.productId;
176-
const did = device.device?.deviceId;
177142
const port = this.createPort(device.device);
178143
if (port) {
179144
this.ports.push(port);
180145
}
181146
}
182147
}
183-
console.log(`${logHead} Loaded ${this.ports.length} devices`);
184148
} catch (error) {
185149
console.error(`${logHead} Error loading devices:`, error);
186150
}
@@ -193,21 +157,6 @@ class CapacitorSerialProtocol extends EventTarget {
193157

194158
async connect(path, options = { baudRate: 115200 }) {
195159
try {
196-
/*
197-
if (!this.selectedDevice) {
198-
console.error(`${logHead} No device selected`);
199-
this.dispatchEvent(new CustomEvent("connect", { detail: false }));
200-
return false;
201-
}
202-
203-
const device = this.ports.find(p => p.deviceId === this.selectedDevice.deviceId);
204-
if (!device) {
205-
console.error(`${logHead} Device not found`);
206-
this.dispatchEvent(new CustomEvent("connect", { detail: false }));
207-
return false;
208-
}
209-
*/
210-
211160
const device = this.ports.find((device) => device.path === path);
212161

213162
if (!device) {
@@ -216,8 +165,6 @@ class CapacitorSerialProtocol extends EventTarget {
216165
return false;
217166
}
218167

219-
console.log(`${logHead} Connecting to device:`, device);
220-
// Open serial connection
221168
await UsbSerial.openSerial({
222169
deviceId: device.deviceId,
223170
portNum: 0,
@@ -229,16 +176,12 @@ class CapacitorSerialProtocol extends EventTarget {
229176
rts: false,
230177
});
231178

232-
console.log(`${logHead} Serial connection opened with options:`, options);
233179
this.isOpen = true;
234180
this.connectionId = path;
235181
this.connected = true;
236182
this.port = device;
237183

238-
this.addEventListener("receive", this.handleReceiveBytes);
239-
240184
this.dispatchEvent(new CustomEvent("connect", { detail: true }));
241-
console.log(`${logHead} Connected to ${path}`);
242185

243186
return true;
244187
} catch (error) {
@@ -248,33 +191,25 @@ class CapacitorSerialProtocol extends EventTarget {
248191
}
249192
}
250193

251-
/**
252-
* Request serial permissions for a device.
253-
* This will trigger Android's permission dialog.
254-
*/
255194
async requestPermissionDevice() {
256195
try {
257-
// Reload devices to get the latest list
258196
await this.loadDevices();
259197

260198
// Try to find a matching device from our known serial devices list
261199
for (const { vendorId, productId } of serialDevices) {
262200
const device = this.ports.find((p) => p.vendorId === vendorId && p.productId === productId);
263201
if (device) {
264202
this.selectedDevice = device;
265-
console.log(`${logHead} Selected device:`, device);
266203
return device;
267204
}
268205
}
269206

270207
// If no known device found, select the first available device
271208
if (this.ports.length > 0) {
272209
this.selectedDevice = this.ports[0];
273-
console.log(`${logHead} Selected first available device:`, this.selectedDevice);
274210
return this.selectedDevice;
275211
}
276212

277-
console.warn(`${logHead} No compatible devices found`);
278213
return null;
279214
} catch (error) {
280215
console.error(`${logHead} Error requesting device permission:`, error);
@@ -291,26 +226,17 @@ class CapacitorSerialProtocol extends EventTarget {
291226

292227
try {
293228
// Handle both Uint8Array and ArrayBuffer
294-
let uint8Array;
295-
if (data instanceof Uint8Array) {
296-
uint8Array = data;
297-
} else if (data instanceof ArrayBuffer) {
298-
uint8Array = new Uint8Array(data);
299-
} else {
300-
throw new Error("Data must be Uint8Array or ArrayBuffer");
301-
}
229+
const uint8Array = data instanceof Uint8Array ? data : new Uint8Array(data);
302230

303231
// Convert to hex string for the plugin
304-
const hexString = this.arrayBufferToHexString(uint8Array.buffer);
305-
console.log(`${logHead} Sending data:`, data, "hex:", hexString);
232+
const hexString = this.arrayBufferToHexString(uint8Array);
306233

307234
await UsbSerial.writeSerial({ data: hexString });
308235

309236
const bytesSent = uint8Array.byteLength;
310237
this.bytesSent += bytesSent;
311-
console.log(`${logHead} Sent ${bytesSent} bytes`);
312238

313-
callback?.({ bytesSent: bytesSent });
239+
callback?.({ bytesSent });
314240
return true;
315241
} catch (error) {
316242
console.error(`${logHead} Error sending data:`, error);
@@ -330,7 +256,6 @@ class CapacitorSerialProtocol extends EventTarget {
330256

331257
try {
332258
await UsbSerial.closeSerial();
333-
console.log(`${logHead} Serial connection closed`);
334259
} catch (error) {
335260
console.error(`${logHead} Error closing serial connection:`, error);
336261
closeError = error;
@@ -339,7 +264,6 @@ class CapacitorSerialProtocol extends EventTarget {
339264
this.connected = false;
340265
this.port = null;
341266
this.connectionId = null;
342-
this.removeEventListener("receive", this.handleReceiveBytes);
343267
this.dispatchEvent(new CustomEvent("disconnect", { detail: true }));
344268
}
345269

0 commit comments

Comments
 (0)