Skip to content

Commit a3c23c4

Browse files
Remove connect options in favour of setters (#12)
This will allow us to create a radio bridge connection instance upfront. This API should probably change but let's get our internal clients using it and evolve it with usage in mind.
1 parent 5e246a6 commit a3c23c4

File tree

5 files changed

+35
-32
lines changed

5 files changed

+35
-32
lines changed

lib/bluetooth.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
AfterRequestDevice,
1414
BeforeRequestDevice,
1515
BoardVersion,
16-
ConnectOptions,
1716
ConnectionStatus,
1817
ConnectionStatusEvent,
1918
DeviceConnection,
@@ -62,6 +61,7 @@ export class MicrobitWebBluetoothConnection
6261
this.availability = value;
6362
};
6463
private availability: boolean | undefined;
64+
private nameFilter: string | undefined;
6565

6666
constructor(options: MicrobitWebBluetoothConnectionOptions = {}) {
6767
super();
@@ -100,8 +100,8 @@ export class MicrobitWebBluetoothConnection
100100
);
101101
}
102102

103-
async connect(options: ConnectOptions = {}): Promise<ConnectionStatus> {
104-
await this.connectInternal(options);
103+
async connect(): Promise<ConnectionStatus> {
104+
await this.connectInternal();
105105
return this.status;
106106
}
107107

@@ -150,9 +150,9 @@ export class MicrobitWebBluetoothConnection
150150
this.setStatus(ConnectionStatus.NO_AUTHORIZED_DEVICE);
151151
}
152152

153-
private async connectInternal(options: ConnectOptions): Promise<void> {
153+
private async connectInternal(): Promise<void> {
154154
if (!this.connection) {
155-
const device = await this.chooseDevice(options);
155+
const device = await this.chooseDevice();
156156
if (!device) {
157157
return;
158158
}
@@ -169,9 +169,11 @@ export class MicrobitWebBluetoothConnection
169169
this.setStatus(ConnectionStatus.CONNECTED);
170170
}
171171

172-
private async chooseDevice(
173-
options: ConnectOptions,
174-
): Promise<BluetoothDevice | undefined> {
172+
setNameFilter(name: string) {
173+
this.nameFilter = name;
174+
}
175+
176+
private async chooseDevice(): Promise<BluetoothDevice | undefined> {
175177
if (this.device) {
176178
return this.device;
177179
}
@@ -183,8 +185,8 @@ export class MicrobitWebBluetoothConnection
183185
navigator.bluetooth.requestDevice({
184186
filters: [
185187
{
186-
namePrefix: options.name
187-
? `BBC micro:bit [${options.name}]`
188+
namePrefix: this.nameFilter
189+
? `BBC micro:bit [${this.nameFilter}]`
188190
: "BBC micro:bit",
189191
},
190192
],

lib/device.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ export type FlashDataSource = (
9797
boardVersion: BoardVersion,
9898
) => Promise<string | Uint8Array>;
9999

100-
export interface ConnectOptions {
101-
// Name filter used for Web Bluetooth
102-
name?: string;
103-
}
104-
105100
export type BoardVersion = "V1" | "V2";
106101

107102
export class ConnectionStatusEvent extends Event {
@@ -182,7 +177,7 @@ export interface DeviceConnection
182177
*
183178
* @returns the final connection status.
184179
*/
185-
connect(options?: ConnectOptions): Promise<ConnectionStatus>;
180+
connect(): Promise<ConnectionStatus>;
186181

187182
/**
188183
* Get the board version.

lib/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
AfterRequestDevice,
88
BeforeRequestDevice,
99
BoardVersion,
10-
ConnectOptions,
1110
ConnectionStatus,
1211
ConnectionStatusEvent,
1312
DeviceError,
@@ -47,7 +46,6 @@ export type {
4746
AccelerometerDataEvent,
4847
BoardVersion,
4948
ButtonEvent,
50-
ConnectOptions,
5149
DeviceConnection,
5250
DeviceErrorCode,
5351
FlashDataSource,

lib/usb-radio-bridge.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7-
import { MicrobitWebUSBConnection } from "./usb.js";
8-
import * as protocol from "./usb-serial-protocol.js";
9-
import { Logging, NullLogging } from "./logging.js";
10-
import { TypedEventTarget } from "./events.js";
7+
import { AccelerometerDataEvent } from "./accelerometer.js";
8+
import { ButtonEvent, ButtonState } from "./buttons.js";
119
import {
1210
BoardVersion,
1311
ConnectionStatus,
1412
ConnectionStatusEvent,
15-
ConnectOptions,
1613
DeviceConnection,
1714
DeviceConnectionEventMap,
1815
SerialDataEvent,
1916
} from "./device.js";
17+
import { TypedEventTarget } from "./events.js";
18+
import { Logging, NullLogging } from "./logging.js";
2019
import {
2120
ServiceConnectionEventMap,
2221
TypedServiceEventDispatcher,
2322
} from "./service-events.js";
24-
import { AccelerometerDataEvent } from "./accelerometer.js";
25-
import { ButtonEvent, ButtonState } from "./buttons.js";
23+
import * as protocol from "./usb-serial-protocol.js";
24+
import { MicrobitWebUSBConnection } from "./usb.js";
2625

2726
const connectTimeoutDuration: number = 10000;
2827

@@ -45,6 +44,7 @@ export class MicrobitRadioBridgeConnection
4544
status: ConnectionStatus;
4645
private logging: Logging;
4746
private serialSession: RadioBridgeSerialSession | undefined;
47+
private remoteDeviceId: number | undefined;
4848

4949
private delegateStatusListner = (e: ConnectionStatusEvent) => {
5050
if (e.status !== ConnectionStatus.CONNECTED) {
@@ -56,7 +56,6 @@ export class MicrobitRadioBridgeConnection
5656

5757
constructor(
5858
private delegate: MicrobitWebUSBConnection,
59-
private remoteDeviceId: number,
6059
options?: MicrobitRadioBridgeConnectionOptions,
6160
) {
6261
super();
@@ -87,10 +86,18 @@ export class MicrobitRadioBridgeConnection
8786
this.delegate.clearDevice();
8887
}
8988

90-
async connect(options: ConnectOptions): Promise<ConnectionStatus> {
89+
setRemoteDeviceId(remoteDeviceId: number) {
90+
this.remoteDeviceId = remoteDeviceId;
91+
}
92+
93+
async connect(): Promise<ConnectionStatus> {
9194
// TODO: previously this skipped overlapping connect attempts but that seems awkward
9295
// can we... just not do that? or wait?
9396

97+
if (this.remoteDeviceId === undefined) {
98+
throw new BridgeError(`Missing remote micro:bit ID`);
99+
}
100+
94101
this.logging.event({
95102
type: "Connect",
96103
message: "Serial connect start",

src/demo.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ const createConnection = (type: "usb" | "bluetooth" | "radio") => {
3131
case "usb":
3232
return new MicrobitWebUSBConnection();
3333
case "radio":
34-
return new MicrobitRadioBridgeConnection(
34+
// This only works with the local-sensor hex.
35+
// To use with a remote micro:bit we need a UI flow that grabs and sets the remote id.
36+
const connection = new MicrobitRadioBridgeConnection(
3537
new MicrobitWebUSBConnection(),
36-
// This only works with the local-sensor hex.
37-
// To use with a remote micro:bit we need a UI flow that grabs the remote id.
38-
0,
3938
);
39+
connection.setRemoteDeviceId(0);
40+
return connection;
4041
}
4142
};
4243

@@ -121,7 +122,7 @@ const createConnectSection = (type: ConnectionType): Section => {
121122
"button",
122123
{
123124
onclick: () => {
124-
void connection.connect({ name: name || undefined });
125+
void connection.connect();
125126
},
126127
},
127128
"Connect",

0 commit comments

Comments
 (0)