Skip to content

Commit 8a51391

Browse files
committed
fix bluetooth support issue for chrome on android.
and usb otg serial support for chrome on android. Update src/js/protocols/bluetooth.js Co-authored-by: Mark Haslinghuis <[email protected]>
1 parent 0ae4af9 commit 8a51391

File tree

5 files changed

+675
-11
lines changed

5 files changed

+675
-11
lines changed

src/js/DarkTheme.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ DarkTheme.apply = function() {
3232
self.applyNormal();
3333
}
3434

35-
if (chrome.app.window !== undefined) {
35+
if (chrome?.app?.window !== undefined) {
3636
windowWatcherUtil.passValue(chrome.app.window.get("receiver_msp"), 'darkTheme', isEnabled);
3737
}
3838
});

src/js/main.js

+9
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,15 @@ function startProcess() {
420420
$(this).data('state', state);
421421
});
422422

423+
424+
$("#menu_btn").on('click', function () {
425+
$("#tab-content-container .tab_container").addClass('reveal');
426+
});
427+
428+
$("#tab-content-container .tab_container").on('click', function () {
429+
$("#tab-content-container .tab_container").removeClass('reveal');
430+
});
431+
423432
let result = getConfig('logopen');
424433
if (result.logopen) {
425434
$("#showlog").trigger('click');

src/js/protocols/bluetooth.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const bluetoothDevices = [
2222
class BT extends EventTarget {
2323
constructor() {
2424
super();
25+
this.lastWrite=null;
2526

2627
if (!this.bluetooth && window && window.navigator && window.navigator.bluetooth) {
2728
this.bluetooth = navigator.bluetooth;
@@ -96,10 +97,14 @@ class BT extends EventTarget {
9697
}
9798

9899
async loadDevices() {
99-
const devices = await this.getDevices();
100+
try{
101+
const devices = await this.bluetooth.getDevices();
100102

101-
this.portCounter = 1;
102-
this.devices = devices.map(device => this.createPort(device));
103+
this.portCounter = 1;
104+
this.devices = devices.map(device => this.createPort(device));
105+
}catch(e){
106+
107+
}
103108
}
104109

105110
async requestPermissionDevice() {
@@ -260,8 +265,12 @@ class BT extends EventTarget {
260265
}
261266

262267
this.readCharacteristic.addEventListener('characteristicvaluechanged', this.handleNotification.bind(this));
263-
264-
return await this.readCharacteristic.readValue();
268+
try{
269+
return await this.readCharacteristic.readValue();
270+
}catch(e){
271+
console.error(e);
272+
return;
273+
}
265274
}
266275

267276
handleNotification(event) {
@@ -270,8 +279,9 @@ class BT extends EventTarget {
270279
for (let i = 0; i < event.target.value.byteLength; i++) {
271280
buffer[i] = event.target.value.getUint8(i);
272281
}
273-
274-
this.dispatchEvent(new CustomEvent("receive", { detail: buffer }));
282+
setTimeout(()=>{
283+
this.dispatchEvent(new CustomEvent("receive", { detail: buffer }));
284+
},0);
275285
}
276286

277287
startNotifications() {
@@ -348,8 +358,14 @@ class BT extends EventTarget {
348358
this.bytesSent += data.byteLength;
349359

350360
const dataBuffer = new Uint8Array(data);
351-
352-
await this.writeCharacteristic.writeValue(dataBuffer);
361+
try {
362+
if (this.lastWrite){
363+
await this.lastWrite;
364+
}
365+
} catch(error) {
366+
console.error(error);
367+
}
368+
this.lastWrite = this.writeCharacteristic.writeValueWithoutResponse(dataBuffer);
353369

354370
return {
355371
bytesSent: data.byteLength,

src/js/utils/checkBrowserCompatibilty.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { serial as serialPolyfill } from './web-serial-polyfill/serial.ts';
2+
13
export function isChromium() {
24
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
35
if (!navigator.userAgentData) {
@@ -13,7 +15,18 @@ export function isChromium() {
1315
}
1416

1517
export function checkBrowserCompatibility() {
16-
const compatible = "serial" in navigator;
18+
let compatible = "serial" in navigator;
19+
if (!compatible) {
20+
if('usb' in navigator &&
21+
!('brave' in navigator)
22+
) {
23+
navigator.serial = serialPolyfill;
24+
}
25+
26+
if(navigator?.serial || navigator?.bluetooth){
27+
compatible = true;
28+
}
29+
}
1730

1831
if (isChromium() && compatible) {
1932
return true;

0 commit comments

Comments
 (0)