Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit bd57430

Browse files
authored
Merge pull request #135 from philon-/fix-BLE-connection
Fix Bluetooth connection initialization
2 parents 36c3779 + 24d42a6 commit bd57430

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

packages/transport-web-bluetooth/deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@meshtastic/transport-web-bluetooth",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"exports": {
55
".": "./mod.ts"
66
},

packages/transport-web-bluetooth/src/transport.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import type { Types } from "@meshtastic/core";
33
export class TransportWebBluetooth implements Types.Transport {
44
private _toDevice: WritableStream<Uint8Array>;
55
private _fromDevice: ReadableStream<Types.DeviceOutput>;
6+
private _fromDeviceController?: ReadableStreamDefaultController<
7+
Types.DeviceOutput
8+
>;
9+
private _isFirstWrite = true;
10+
611
private toRadioCharacteristic: BluetoothRemoteGATTCharacteristic;
712
private fromRadioCharacteristic: BluetoothRemoteGATTCharacteristic;
813
private fromNumCharacteristic: BluetoothRemoteGATTCharacteristic;
@@ -22,7 +27,6 @@ export class TransportWebBluetooth implements Types.Transport {
2227
public static async createFromDevice(
2328
device: BluetoothDevice,
2429
): Promise<TransportWebBluetooth> {
25-
console.log("creating from device");
2630
return await this.prepareConnection(device);
2731
}
2832

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

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

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

58-
await fromNumCharacteristic.startNotifications();
59-
6061
console.log("Connected to device", device.name);
6162

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

78-
this._toDevice = new WritableStream({
79-
write: async (chunk) => {
80-
await this.toRadioCharacteristic.writeValue(chunk);
79+
this._fromDevice = new ReadableStream({
80+
start: (ctrl) => {
81+
this._fromDeviceController = ctrl;
8182
},
8283
});
8384

84-
let controller: ReadableStreamDefaultController<Types.DeviceOutput>;
85+
this._toDevice = new WritableStream({
86+
write: async (chunk) => {
87+
await this.toRadioCharacteristic.writeValue(chunk);
8588

86-
this._fromDevice = new ReadableStream({
87-
start: (ctrl) => {
88-
controller = ctrl;
89+
if (this._isFirstWrite && this._fromDeviceController) {
90+
this._isFirstWrite = false;
91+
setTimeout(() => {
92+
this.readFromRadio(this._fromDeviceController!);
93+
}, 50);
94+
}
8995
},
9096
});
9197

9298
this.fromNumCharacteristic.addEventListener(
9399
"characteristicvaluechanged",
94-
() => this.readFromRadio(controller),
100+
() => {
101+
if (this._fromDeviceController) {
102+
this.readFromRadio(this._fromDeviceController);
103+
}
104+
},
95105
);
106+
107+
this.fromNumCharacteristic.startNotifications();
96108
}
97109

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

0 commit comments

Comments
 (0)