Skip to content

Commit

Permalink
temp: mocks for pairing flow
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-sanderson committed Nov 28, 2024
1 parent 74db921 commit 37d99b0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/suite-desktop-api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export interface RendererChannels {
'bluetooth/event': any;
'bluetooth/adapter-event': boolean;
'bluetooth/select-device-event': ElectronBluetoothDevice[];
'bluetooth/pair-device-event': { paired: boolean; pin: string };
'bluetooth/pair-device-event': { uuid: string, paired: boolean; pin: string };
'bluetooth/connect-device-event': any;
}

Expand Down
80 changes: 67 additions & 13 deletions packages/suite-desktop-core/src/modules/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,60 @@ export const init: ModuleInit = ({ mainWindowProxy }) => {
mainWindowProxy.getInstance()?.webContents.send('bluetooth/pair-device-event', event);
});

const result = await api.sendMessage('connect_device', uuid);
console.warn('Connect result', result);
// Todo: enter pairing mode
const linuxPin = { uuid: 'hci0/dev_E1_43_47_BA_6A_69', paired: false, pin: '' };
const windowsPin = { uuid: 'hci0/dev_E1_43_47_BA_6A_69', paired: false, 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));
mainWindowProxy.getInstance()?.webContents.send('bluetooth/pair-device-event', windowsPin);

// 2. [Win, Lin] Simulates that user confirmed PIN on the device
await new Promise(resolve => setTimeout(resolve, 4000));
mainWindowProxy.getInstance()?.webContents.send('bluetooth/pair-device-event', {
uuid: 'hci0/dev_E1_43_47_BA_6A_69',
paired: true,
pin: '',
});

api.disconnect();
// 3. [Win, Lin, Mac] Simulates that device is starting to connect
await new Promise(resolve => setTimeout(resolve, 2000));
mainWindowProxy.getInstance()?.webContents.send('bluetooth/connect-device-event', {
uuid: 'hci0/dev_E1_43_47_BA_6A_69',
status: 'connecting',
});

if (result !== true) {
console.warn('ERROR!', result);
// mainWindowProxy
// .getInstance()
// ?.webContents.send('bluetooth/connect-device-event', { phase: 'error' });
// 4. [Win, Lin, Mac] Simulates that device is starting to connect
await new Promise(resolve => setTimeout(resolve, 2000));
mainWindowProxy.getInstance()?.webContents.send('bluetooth/connect-device-event', {
uuid: 'hci0/dev_E1_43_47_BA_6A_69',
status: 'done',
});

return { success: false, error: result };
} else {
return { success: true };
}
return { success: true };

// const result = await api.sendMessage('connect_device', uuid);
// console.warn('Connect result', result);
//
// api.disconnect();
//
// if (result !== true) {
// console.warn('ERROR!', result);
// // mainWindowProxy
// // .getInstance()
// // ?.webContents.send('bluetooth/connect-device-event', { phase: 'error' });
//
// return { success: false, error: result };
// } else {
// return { success: true };
// }
});

ipcMain.handle('bluetooth/forget-device', async (_, uuid) => {
Expand Down Expand Up @@ -143,7 +182,22 @@ export const init: ModuleInit = ({ mainWindowProxy }) => {

const devices = await api.sendMessage('start_scan');

emitSelect({ devices });
emitSelect({
devices: [
{
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,
},
...devices,
],
});

// const clear = () => {
// ipcMain.removeAllListeners('bluetooth/stop-scan');
Expand Down

0 comments on commit 37d99b0

Please sign in to comment.