Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Bluetooth handle connection status for knownDevices #17221

Draft
wants to merge 5 commits into
base: feat/rust-bluetooth
Choose a base branch
from

Conversation

peter-sanderson
Copy link
Contributor

@peter-sanderson peter-sanderson commented Feb 25, 2025

Partially addresses: #15390

Allows for disconnect:

image

Add cancel for paring mode:
image

@peter-sanderson peter-sanderson mentioned this pull request Feb 25, 2025
10 tasks
Copy link

github-actions bot commented Feb 25, 2025

🚀 Expo preview is ready!

  • Project → trezor-suite-preview
  • Platforms → android, ios
  • Scheme → trezorsuitelite
  • Runtime Version → 26
  • More info

Learn more about 𝝠 Expo Github Action

@peter-sanderson peter-sanderson added the no-project This label is used to specify that PR doesn't need to be added to a project label Feb 25, 2025
@peter-sanderson peter-sanderson force-pushed the bluetooth-suite4 branch 3 times, most recently from 78738fc to d94c687 Compare February 25, 2025 16:34
@peter-sanderson peter-sanderson force-pushed the bluetooth-suite4 branch 3 times, most recently from 89455d9 to c227f7d Compare February 26, 2025 13:32
@peter-sanderson peter-sanderson force-pushed the bluetooth-suite4 branch 2 times, most recently from d2f7759 to 633b19d Compare February 26, 2025 14:37
return true;
});
const devices = allDevices;
// .filter(it => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will be uncommented, I need a fix for this from Szymon


if (!result.success) {
dispatch(
bluetoothActions.connectDeviceEventAction({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

change of the connection state is now responsibility of the bluetoothIpc and the connection status update will come from it as an update


return Array.from(map.values()).sort(
(a, b) => b.device.lastUpdatedTimestamp - a.device.lastUpdatedTimestamp,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorting is now responsibility of the bluetoothIpc code

Copy link
Contributor

@Lemonexe Lemonexe left a comment

Choose a reason for hiding this comment

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

Looks ok, though I cannot test (no BT device), which means I also did not check id design matches figma.

Codewise LGTM with some comments to be addressed.

Overall, I don't understand why the refactoring from deviceState to device with connectionStatus is necessary, though it certainly looks good

@@ -15,27 +15,15 @@ export const bluetoothConnectDeviceThunk = createThunk<
`${BLUETOOTH_PREFIX}/bluetoothConnectDeviceThunk`,
async ({ id }, { fulfillWithValue, dispatch }) => {
const result = await bluetoothIpc.connectDevice(id);
console.log('_____calling: bluetoothIpc.connectDevice(id)', id);
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 reminder to remove console log.

Also, why remove connectDeviceEventAction? Is it guaranteed that when bluetoothIpc.connectDevice finishes, it will trigger deviceActions.connectDevice (so it'd be useless to call it twice), or how does it work?

// Devices with 'pairing-error' status should NOT be displayed in the list, as it
// won't be possible to connect to them ever again. User has to start pairing again,
// which would produce a device with new id.
.filter(nearbyDevice => nearbyDevice.connectionStatus?.type !== 'pairing-error')
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we persist them in redux if they cannot be connected ever again, and have to be paired again to (creating new device object)?

If any device's state becomes 'pairing-error', does it make any sense to keep it in redux for the rest of Suite session?

If not, let's filter them right at the source, in bluetoothReducer: case for bluetoothActions.nearbyDevicesUpdateAction.

Else, how about filtering them in storageActions: saveKnownDevices, so that at least we don't remember them forever?

};

describe('bluetoothSelectors', () => {
it('selects knownDevices and nearbyDevices in one list fot the UI', () => {
it('selects knownDevices and nearbyDevices in one list fot the UI, all known devices are', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

unfinished sentence?

}
});
const nearbyDevicesCopy = [...nearbyDevices];
nearbyDevicesCopy.reverse();
Copy link
Contributor

Choose a reason for hiding this comment

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

why reverse? So that device discovered most recently is displayed first?
Please add comment; it's clear what it does, but not why.

>(
`${BLUETOOTH_PREFIX}/bluetoothConnectDeviceThunk`,
async ({ id }, { fulfillWithValue, dispatch }) => {
console.log('_____calling: bluetoothIpc.disconnectDevice(id)', id);
Copy link
Contributor

Choose a reason for hiding this comment

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

same as in bluetoothConnectDeviceThunk.ts


const onDisconnect = async () => {
await dispatch(bluetoothDisconnectDeviceThunk({ id: device.id })).unwrap();
};
Copy link
Contributor

Choose a reason for hiding this comment

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

this thunk can also result in !result.success right? So onError should be called?

connected: onDisconnect,
connecting: undefined,
disconnected: onConnect,
paired: undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

when paired, the Button is neither disabled, nor loading, but doesn't do anything, is that right?

<Text variant="warning">{secAgo}</Text>s ago
</>
);
};
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be extracted as a reusable component..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-project This label is used to specify that PR doesn't need to be added to a project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants