Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/transport-web-bluetooth/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshtastic/transport-web-bluetooth",
"version": "0.1.1",
"version": "0.1.2",
"exports": {
".": "./mod.ts"
},
Expand Down
37 changes: 24 additions & 13 deletions packages/transport-web-bluetooth/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import type { Types } from "@meshtastic/core";
export class TransportWebBluetooth implements Types.Transport {
private _toDevice: WritableStream<Uint8Array>;
private _fromDevice: ReadableStream<Types.DeviceOutput>;
private _fromDeviceController?: ReadableStreamDefaultController<
Types.DeviceOutput
>;
private _isFirstWrite = true;

private toRadioCharacteristic: BluetoothRemoteGATTCharacteristic;
private fromRadioCharacteristic: BluetoothRemoteGATTCharacteristic;
private fromNumCharacteristic: BluetoothRemoteGATTCharacteristic;
Expand All @@ -22,7 +27,6 @@ export class TransportWebBluetooth implements Types.Transport {
public static async createFromDevice(
device: BluetoothDevice,
): Promise<TransportWebBluetooth> {
console.log("creating from device");
return await this.prepareConnection(device);
}

Expand All @@ -36,7 +40,6 @@ export class TransportWebBluetooth implements Types.Transport {
}

const service = await gattServer.getPrimaryService(this.ServiceUuid);
console.log("service", service);

const toRadioCharacteristic = await service.getCharacteristic(
this.ToRadioUuid,
Expand All @@ -55,8 +58,6 @@ export class TransportWebBluetooth implements Types.Transport {
throw new Error("Failed to find required characteristics");
}

await fromNumCharacteristic.startNotifications();

console.log("Connected to device", device.name);

return new TransportWebBluetooth(
Expand All @@ -75,24 +76,35 @@ export class TransportWebBluetooth implements Types.Transport {
this.fromRadioCharacteristic = fromRadioCharacteristic;
this.fromNumCharacteristic = fromNumCharacteristic;

this._toDevice = new WritableStream({
write: async (chunk) => {
await this.toRadioCharacteristic.writeValue(chunk);
this._fromDevice = new ReadableStream({
start: (ctrl) => {
this._fromDeviceController = ctrl;
},
});

let controller: ReadableStreamDefaultController<Types.DeviceOutput>;
this._toDevice = new WritableStream({
write: async (chunk) => {
await this.toRadioCharacteristic.writeValue(chunk);

this._fromDevice = new ReadableStream({
start: (ctrl) => {
controller = ctrl;
if (this._isFirstWrite && this._fromDeviceController) {
this._isFirstWrite = false;
setTimeout(() => {
this.readFromRadio(this._fromDeviceController!);
}, 50);
}
},
});

this.fromNumCharacteristic.addEventListener(
"characteristicvaluechanged",
() => this.readFromRadio(controller),
() => {
if (this._fromDeviceController) {
this.readFromRadio(this._fromDeviceController);
}
},
);

this.fromNumCharacteristic.startNotifications();
}

get toDevice(): WritableStream<Uint8Array> {
Expand All @@ -106,7 +118,6 @@ export class TransportWebBluetooth implements Types.Transport {
protected async readFromRadio(
controller: ReadableStreamDefaultController<Types.DeviceOutput>,
): Promise<void> {
console.log("reading from radio");
let hasMoreData = true;
while (hasMoreData && this.fromRadioCharacteristic) {
const value = await this.fromRadioCharacteristic.readValue();
Expand Down