Skip to content

Commit

Permalink
fix bluetooth support issue for chrome on android.
Browse files Browse the repository at this point in the history
and usb otg serial support for chrome on android.

Update src/js/protocols/bluetooth.js
Co-authored-by: Mark Haslinghuis <[email protected]>
  • Loading branch information
kikoqiu committed Oct 28, 2024
1 parent 0ae4af9 commit 8a51391
Show file tree
Hide file tree
Showing 5 changed files with 675 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/js/DarkTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ DarkTheme.apply = function() {
self.applyNormal();
}

if (chrome.app.window !== undefined) {
if (chrome?.app?.window !== undefined) {
windowWatcherUtil.passValue(chrome.app.window.get("receiver_msp"), 'darkTheme', isEnabled);
}
});
Expand Down
9 changes: 9 additions & 0 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,15 @@ function startProcess() {
$(this).data('state', state);
});


$("#menu_btn").on('click', function () {
$("#tab-content-container .tab_container").addClass('reveal');
});

$("#tab-content-container .tab_container").on('click', function () {
$("#tab-content-container .tab_container").removeClass('reveal');
});

let result = getConfig('logopen');
if (result.logopen) {
$("#showlog").trigger('click');
Expand Down
34 changes: 25 additions & 9 deletions src/js/protocols/bluetooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const bluetoothDevices = [
class BT extends EventTarget {
constructor() {
super();
this.lastWrite=null;

if (!this.bluetooth && window && window.navigator && window.navigator.bluetooth) {
this.bluetooth = navigator.bluetooth;
Expand Down Expand Up @@ -96,10 +97,14 @@ class BT extends EventTarget {
}

async loadDevices() {
const devices = await this.getDevices();
try{
const devices = await this.bluetooth.getDevices();

this.portCounter = 1;
this.devices = devices.map(device => this.createPort(device));
this.portCounter = 1;
this.devices = devices.map(device => this.createPort(device));
}catch(e){

}
}

async requestPermissionDevice() {
Expand Down Expand Up @@ -260,8 +265,12 @@ class BT extends EventTarget {
}

this.readCharacteristic.addEventListener('characteristicvaluechanged', this.handleNotification.bind(this));

return await this.readCharacteristic.readValue();
try{
return await this.readCharacteristic.readValue();
}catch(e){
console.error(e);
return;
}
}

handleNotification(event) {
Expand All @@ -270,8 +279,9 @@ class BT extends EventTarget {
for (let i = 0; i < event.target.value.byteLength; i++) {
buffer[i] = event.target.value.getUint8(i);
}

this.dispatchEvent(new CustomEvent("receive", { detail: buffer }));
setTimeout(()=>{
this.dispatchEvent(new CustomEvent("receive", { detail: buffer }));
},0);
}

startNotifications() {
Expand Down Expand Up @@ -348,8 +358,14 @@ class BT extends EventTarget {
this.bytesSent += data.byteLength;

const dataBuffer = new Uint8Array(data);

await this.writeCharacteristic.writeValue(dataBuffer);
try {
if (this.lastWrite){
await this.lastWrite;
}
} catch(error) {
console.error(error);
}
this.lastWrite = this.writeCharacteristic.writeValueWithoutResponse(dataBuffer);

return {
bytesSent: data.byteLength,
Expand Down
15 changes: 14 additions & 1 deletion src/js/utils/checkBrowserCompatibilty.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { serial as serialPolyfill } from './web-serial-polyfill/serial.ts';

export function isChromium() {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
if (!navigator.userAgentData) {
Expand All @@ -13,7 +15,18 @@ export function isChromium() {
}

export function checkBrowserCompatibility() {
const compatible = "serial" in navigator;
let compatible = "serial" in navigator;
if (!compatible) {
if('usb' in navigator &&
!('brave' in navigator)
) {
navigator.serial = serialPolyfill;
}

if(navigator?.serial || navigator?.bluetooth){
compatible = true;
}
}

if (isChromium() && compatible) {
return true;
Expand Down
Loading

0 comments on commit 8a51391

Please sign in to comment.