Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 12 additions & 10 deletions lib/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
AfterRequestDevice,
BeforeRequestDevice,
BoardVersion,
ConnectOptions,
ConnectionStatus,
ConnectionStatusEvent,
DeviceConnection,
Expand Down Expand Up @@ -62,6 +61,7 @@ export class MicrobitWebBluetoothConnection
this.availability = value;
};
private availability: boolean | undefined;
private nameFilter: string | undefined;

constructor(options: MicrobitWebBluetoothConnectionOptions = {}) {
super();
Expand Down Expand Up @@ -100,8 +100,8 @@ export class MicrobitWebBluetoothConnection
);
}

async connect(options: ConnectOptions = {}): Promise<ConnectionStatus> {
await this.connectInternal(options);
async connect(): Promise<ConnectionStatus> {
await this.connectInternal();
return this.status;
}

Expand Down Expand Up @@ -150,9 +150,9 @@ export class MicrobitWebBluetoothConnection
this.setStatus(ConnectionStatus.NO_AUTHORIZED_DEVICE);
}

private async connectInternal(options: ConnectOptions): Promise<void> {
private async connectInternal(): Promise<void> {
if (!this.connection) {
const device = await this.chooseDevice(options);
const device = await this.chooseDevice();
if (!device) {
return;
}
Expand All @@ -169,9 +169,11 @@ export class MicrobitWebBluetoothConnection
this.setStatus(ConnectionStatus.CONNECTED);
}

private async chooseDevice(
options: ConnectOptions,
): Promise<BluetoothDevice | undefined> {
setNameFilter(name: string) {
this.nameFilter = name;
}

private async chooseDevice(): Promise<BluetoothDevice | undefined> {
if (this.device) {
return this.device;
}
Expand All @@ -183,8 +185,8 @@ export class MicrobitWebBluetoothConnection
navigator.bluetooth.requestDevice({
filters: [
{
namePrefix: options.name
? `BBC micro:bit [${options.name}]`
namePrefix: this.nameFilter
? `BBC micro:bit [${this.nameFilter}]`
: "BBC micro:bit",
},
],
Expand Down
7 changes: 1 addition & 6 deletions lib/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ export type FlashDataSource = (
boardVersion: BoardVersion,
) => Promise<string | Uint8Array>;

export interface ConnectOptions {
// Name filter used for Web Bluetooth
name?: string;
}

export type BoardVersion = "V1" | "V2";

export class ConnectionStatusEvent extends Event {
Expand Down Expand Up @@ -182,7 +177,7 @@ export interface DeviceConnection
*
* @returns the final connection status.
*/
connect(options?: ConnectOptions): Promise<ConnectionStatus>;
connect(): Promise<ConnectionStatus>;

/**
* Get the board version.
Expand Down
2 changes: 0 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
AfterRequestDevice,
BeforeRequestDevice,
BoardVersion,
ConnectOptions,
ConnectionStatus,
ConnectionStatusEvent,
DeviceError,
Expand Down Expand Up @@ -46,7 +45,6 @@ export type {
AccelerometerDataEvent,
DeviceConnection,
BoardVersion,
ConnectOptions,
DeviceErrorCode,
FlashDataSource,
};
25 changes: 16 additions & 9 deletions lib/usb-radio-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@
* SPDX-License-Identifier: MIT
*/

import { MicrobitWebUSBConnection } from "./usb.js";
import * as protocol from "./usb-serial-protocol.js";
import { Logging, NullLogging } from "./logging.js";
import { TypedEventTarget } from "./events.js";
import { AccelerometerDataEvent } from "./accelerometer.js";
import { ButtonEvent, ButtonState } from "./buttons.js";
import {
BoardVersion,
ConnectionStatus,
ConnectionStatusEvent,
ConnectOptions,
DeviceConnection,
DeviceConnectionEventMap,
SerialDataEvent,
} from "./device.js";
import { TypedEventTarget } from "./events.js";
import { Logging, NullLogging } from "./logging.js";
import {
ServiceConnectionEventMap,
TypedServiceEventDispatcher,
} from "./service-events.js";
import { AccelerometerDataEvent } from "./accelerometer.js";
import { ButtonEvent, ButtonState } from "./buttons.js";
import * as protocol from "./usb-serial-protocol.js";
import { MicrobitWebUSBConnection } from "./usb.js";

const connectTimeoutDuration: number = 10000;

Expand All @@ -45,6 +44,7 @@ export class MicrobitRadioBridgeConnection
status: ConnectionStatus;
private logging: Logging;
private serialSession: RadioBridgeSerialSession | undefined;
private remoteDeviceId: number | undefined;

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

constructor(
private delegate: MicrobitWebUSBConnection,
private remoteDeviceId: number,
options?: MicrobitRadioBridgeConnectionOptions,
) {
super();
Expand Down Expand Up @@ -87,10 +86,18 @@ export class MicrobitRadioBridgeConnection
this.delegate.clearDevice();
}

async connect(options: ConnectOptions): Promise<ConnectionStatus> {
setRemoteDeviceId(remoteDeviceId: number) {
this.remoteDeviceId = remoteDeviceId;
}

async connect(): Promise<ConnectionStatus> {
// TODO: previously this skipped overlapping connect attempts but that seems awkward
// can we... just not do that? or wait?

if (!this.remoteDeviceId) {
throw new BridgeError(`Missing remote micro:bit ID`);
}

this.logging.event({
type: "Connect",
message: "Serial connect start",
Expand Down
11 changes: 4 additions & 7 deletions src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ const createConnection = (type: "usb" | "bluetooth" | "radio") => {
case "usb":
return new MicrobitWebUSBConnection();
case "radio":
return new MicrobitRadioBridgeConnection(
new MicrobitWebUSBConnection(),
// This only works with the local-sensor hex.
// To use with a remote micro:bit we need a UI flow that grabs the remote id.
0,
);
// This only works with the local-sensor hex.
// To use with a remote micro:bit we need a UI flow that grabs and sets the remote id.
return new MicrobitRadioBridgeConnection(new MicrobitWebUSBConnection());
}
};

Expand Down Expand Up @@ -121,7 +118,7 @@ const createConnectSection = (type: ConnectionType): Section => {
"button",
{
onclick: () => {
void connection.connect({ name: name || undefined });
void connection.connect();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to come back to this and set the name filter for the Bluetooth connection if a name has been provided.

},
},
"Connect",
Expand Down