Skip to content

Commit

Permalink
temp: add mock Bluetooth Calls
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-sanderson committed Dec 4, 2024
1 parent 5f47cfe commit 1e2561b
Showing 1 changed file with 87 additions and 28 deletions.
115 changes: 87 additions & 28 deletions packages/transport-bluetooth/src/client/bluetooth-ipc-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,85 @@ export class BluetoothApiImpl extends TypedEmitter<BluetoothApiEvents> implement
}

async connectDevice(uuid: string) {
try {
await this.api.connect();
} catch (error) {
return { success: false, error: error.message };
}
// try {
// await this.api.connect();
// } catch (error) {
// return { success: false, error: error.message };
// }

const emitStatus = (event: DeviceConnectionStatus) => {
this.emit('device-connection-status', event);
};

this.api.on('device_connection_status', event =>
emitStatus({ uuid: event.uuid, type: event.phase }),
);
this.api.on('device_pairing', event => {
if (!event.paired) {
emitStatus({
uuid,
type: 'pairing',
pin: event.pin,
});
} else {
emitStatus({
uuid: event.uuid,
type: 'paired',
});
}
// Todo: enter pairing mode
const UUID = 'hci0/dev_E1_43_47_BA_6A_69';
const linuxPin = { uuid: UUID, type: 'pairing' as const, pin: '' };

Check failure on line 55 in packages/transport-bluetooth/src/client/bluetooth-ipc-proxy.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

'linuxPin' is assigned a value but never used. Allowed unused vars must match /^_/u
const windowsPin = {
uuid: UUID,
type: 'pairing' as const,
pin: '123456',
};

// UI:
// - click button -> "Connecting"
// - "pair-device-event" -> pairing -> show PIN Modal
// - (Win/Linux) "pair-device-event" -> paired -> hide PIN Modal
// - "connect-device-event":start -> connecting -> hide PIN Modal (for Mac)
// - "connect-device-event" :done -> connected, but ...connect-connecting again

// 1. [Win, Lin, Mac] In case of windows, this is where we get PIN, on Mac we may not get this
await new Promise(resolve => setTimeout(resolve, 2000));
emitStatus(windowsPin);

// 2. [Win, Lin] Simulates that user confirmed PIN on the device
await new Promise(resolve => setTimeout(resolve, 4000));
emitStatus({
uuid: UUID,
type: 'paired',
});

try {
const result = await this.api.sendMessage('connect_device', uuid);
console.warn('Connect result', result);
} catch (error) {
return { success: false, error: error.message };
}
// 3. [Win, Lin, Mac] Simulates that device is starting to connect
await new Promise(resolve => setTimeout(resolve, 2000));
emitStatus({
uuid: UUID,
type: 'connecting',
});

// 4. [Win, Lin, Mac] Simulates that device is starting to connect
await new Promise(resolve => setTimeout(resolve, 2000));
emitStatus({
uuid: UUID,
type: 'connected',
});

return { success: true } as const;

// this.api.on('device_connection_status', event =>
// emitStatus({ uuid: event.uuid, type: event.phase }),
// );
// this.api.on('device_pairing', event => {
// if (!event.paired) {
// emitStatus({
// uuid,
// type: 'pairing',
// pin: event.pin,
// });
// } else {
// emitStatus({
// uuid: event.uuid,
// type: 'paired',
// });
// }
// });
//
// try {
// const result = await this.api.sendMessage('connect_device', uuid);
// console.warn('Connect result', result);
// } catch (error) {
// return { success: false, error: error.message };
// }
//
// return { success: true } as const;
}

async forgetDevice(id: string): Promise<any> {
Expand Down Expand Up @@ -103,7 +146,23 @@ export class BluetoothApiImpl extends TypedEmitter<BluetoothApiEvents> implement
return devs.filter(d => d.paired || (d.data && d.data[0] === 1));
};
const emitSelect = ({ devices }: { devices: BluetoothDevice[] }) => {
this.emit('device-list-update', connectableDevices(devices));
this.emit('device-list-update', [
{
name: 'TrezorZephyr',
internal_model: 1,
model_variant: 3,
uuid: 'hci0/dev_E1_43_47_BA_6A_69',
connected: false,
timestamp: 1732787043,
rssi: 0,
pairing_mode: false,
paired: true,
data: [],
},
...devices,
]);

// this.emit('device-list-update', connectableDevices(devices));
};
const emitAdapterState = ({ powered }: { powered: boolean }) => {
this.emit('adapter-event', powered);
Expand Down

0 comments on commit 1e2561b

Please sign in to comment.