Awful hacks to use BLE PTT device via webbluetooth

This commit is contained in:
David Baker
2022-06-01 08:55:20 +01:00
parent e9b963080c
commit 6779abe64e
4 changed files with 73 additions and 0 deletions

View File

@@ -55,6 +55,7 @@
"@matrix-org/olm": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.8.tgz",
"@storybook/react": "^6.5.0-alpha.5",
"@types/request": "^2.48.8",
"@types/web-bluetooth": "^0.0.14",
"@typescript-eslint/eslint-plugin": "^5.22.0",
"@typescript-eslint/parser": "^5.22.0",
"babel-loader": "^8.2.3",

View File

@@ -60,6 +60,10 @@ export interface PTTState {
connected: boolean;
}
let btsetupdone = false;
let characteristics;
let globalButtonHeld;
export const usePTT = (
client: MatrixClient,
groupCall: GroupCall,
@@ -228,6 +232,64 @@ export const usePTT = (
[setConnected]
);
const onCharatceristicChange = useCallback(async () => {
const val = await characteristics[0].value;
const strval = new TextDecoder("utf-8").decode(val);
if (globalButtonHeld && strval.startsWith("+PTT=R")) {
stopTalking();
} else if (!globalButtonHeld && strval.startsWith("+PTT=P")) {
startTalking();
}
//setTimeout(async () => {
//console.log(e);
//return;
//onCharatceristicChange();
//}, 200);
}, [startTalking, stopTalking]);
useEffect(() => {
if (btsetupdone) return;
btsetupdone = true;
async function doBluetooth() {
try {
const device = await navigator.bluetooth.requestDevice({
//filters: [{ services: ["00006666-0000-1000-8000-00805f9b34fb"] }],
filters: [{ services: [0x6666] }],
//optionalServices: [0x6666],
//acceptAllDevices: true,
});
console.log(device);
console.log("connecting ble");
const connectedServer = await device.gatt.connect();
console.log("connected", connectedServer);
const service = await connectedServer.getPrimaryService(
//"00006666-0000-1000-8000-00805f9b34fb"
0x6666
);
console.log(service);
characteristics = await service.getCharacteristics();
console.log("characteristics: ", characteristics);
//let readProm = Promise.resolve();
console.log("starting notifs");
await characteristics[0].startNotifications();
characteristics[0].oncharacteristicvaluechanged =
onCharatceristicChange;
console.log("listener added");
//setInterval(onCharatceristicChange, 5000);
//onCharatceristicChange();
} catch (e) {
console.log("error setting up ble ptt", e);
}
}
doBluetooth();
}, [pttButtonHeld, startTalking, stopTalking, onCharatceristicChange]);
useEffect(() => {
function onKeyDown(event: KeyboardEvent): void {
if (event.code === "Space") {
@@ -292,6 +354,8 @@ export const usePTT = (
}));
}, []);
globalButtonHeld = pttButtonHeld;
return {
pttButtonHeld,
isAdmin,

View File

@@ -39,6 +39,9 @@ export default defineConfig(({ mode }) => {
proxy: {
"/_matrix": env.VITE_DEFAULT_HOMESERVER || "http://localhost:8008",
},
hmr: {
clientPort: 443,
},
},
resolve: {
dedupe: [

View File

@@ -3046,6 +3046,11 @@
resolved "https://registry.yarnpkg.com/@types/unist/-/unist-2.0.6.tgz#250a7b16c3b91f672a24552ec64678eeb1d3a08d"
integrity sha512-PBjIUxZHOuj0R15/xuwJYjFi+KZdNFrehocChv4g5hu6aFroHue8m0lBP0POdK2nKzbw0cgV1mws8+V/JAcEkQ==
"@types/web-bluetooth@^0.0.14":
version "0.0.14"
resolved "https://registry.yarnpkg.com/@types/web-bluetooth/-/web-bluetooth-0.0.14.tgz#94e175b53623384bff1f354cdb3197a8d63cdbe5"
integrity sha512-5d2RhCard1nQUC3aHcq/gHzWYO6K0WJmAbjO7mQJgCQKtZpgXxv1rOM6O/dBDhDYYVutk1sciOgNSe+5YyfM8A==
"@types/webpack-env@^1.16.0":
version "1.16.3"
resolved "https://registry.yarnpkg.com/@types/webpack-env/-/webpack-env-1.16.3.tgz#b776327a73e561b71e7881d0cd6d34a1424db86a"