-
-
Notifications
You must be signed in to change notification settings - Fork 279
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
base: feat/rust-bluetooth
Are you sure you want to change the base?
Changes from all commits
4ee3a38
937dae2
9a68d09
7ea2294
ba961f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { BLUETOOTH_PREFIX, bluetoothActions } from '@suite-common/bluetooth'; | ||
import { BLUETOOTH_PREFIX } from '@suite-common/bluetooth'; | ||
import { createThunk } from '@suite-common/redux-utils'; | ||
import { notificationsActions } from '@suite-common/toast-notifications'; | ||
import { bluetoothIpc } from '@trezor/transport-bluetooth'; | ||
|
@@ -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); | ||
|
||
if (!result.success) { | ||
dispatch( | ||
bluetoothActions.connectDeviceEventAction({ | ||
id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change of the connection state is now responsibility of the |
||
connectionStatus: { type: 'error', error: result.error }, | ||
}), | ||
); | ||
dispatch( | ||
notificationsActions.addToast({ | ||
type: 'error', | ||
error: result.error, | ||
}), | ||
); | ||
} else { | ||
dispatch( | ||
bluetoothActions.connectDeviceEventAction({ | ||
id, | ||
connectionStatus: { type: 'connected' }, | ||
}), | ||
); | ||
} | ||
|
||
return fulfillWithValue({ success: result.success }); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { BLUETOOTH_PREFIX } from '@suite-common/bluetooth'; | ||
import { createThunk } from '@suite-common/redux-utils'; | ||
import { notificationsActions } from '@suite-common/toast-notifications'; | ||
import { bluetoothIpc } from '@trezor/transport-bluetooth'; | ||
|
||
type BluetoothDisconnectDeviceThunkResult = { | ||
success: boolean; | ||
}; | ||
|
||
export const bluetoothDisconnectDeviceThunk = createThunk< | ||
BluetoothDisconnectDeviceThunkResult, | ||
{ id: string }, | ||
void | ||
>( | ||
`${BLUETOOTH_PREFIX}/bluetoothConnectDeviceThunk`, | ||
async ({ id }, { fulfillWithValue, dispatch }) => { | ||
console.log('_____calling: bluetoothIpc.disconnectDevice(id)', id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as in |
||
const result = await bluetoothIpc.disconnectDevice(id); | ||
|
||
if (!result.success) { | ||
dispatch( | ||
notificationsActions.addToast({ | ||
type: 'error', | ||
error: result.error, | ||
}), | ||
); | ||
} | ||
|
||
return fulfillWithValue({ success: result.success }); | ||
}, | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
selectKnownDevices, | ||
selectScanStatus, | ||
} from '@suite-common/bluetooth'; | ||
import { selectDevices } from '@suite-common/wallet-core'; | ||
import { Card, Column, ElevationUp } from '@trezor/components'; | ||
import { spacings } from '@trezor/theme'; | ||
import { BluetoothDevice } from '@trezor/transport-bluetooth'; | ||
|
@@ -21,10 +20,8 @@ | |
import { BluetoothTips } from './BluetoothTips'; | ||
import { BluetoothNotEnabled } from './errors/BluetoothNotEnabled'; | ||
import { BluetoothVersionNotCompatible } from './errors/BluetoothVersionNotCompatible'; | ||
import { bluetoothConnectDeviceThunk } from '../../../actions/bluetooth/bluetoothConnectDeviceThunk'; | ||
import { bluetoothStartScanningThunk } from '../../../actions/bluetooth/bluetoothStartScanningThunk'; | ||
import { bluetoothStopScanningThunk } from '../../../actions/bluetooth/bluetoothStopScanningThunk'; | ||
import { closeModalApp } from '../../../actions/suite/routerActions'; | ||
import { useDispatch, useSelector } from '../../../hooks/suite'; | ||
|
||
const SCAN_TIMEOUT = 30_000; | ||
|
@@ -43,38 +40,31 @@ | |
const [selectedDeviceId, setSelectedDeviceId] = useState<string | null>(null); | ||
const [scannerTimerId, setScannerTimerId] = useState<TimerId | null>(null); | ||
|
||
const trezorDevices = useSelector(selectDevices); | ||
|
||
const bluetoothAdapterStatus = useSelector(selectAdapterStatus); | ||
const scanStatus = useSelector(selectScanStatus); | ||
const allDevices = useSelector(selectAllDevices); | ||
const knownDevices = useSelector(selectKnownDevices); | ||
|
||
const lasUpdatedBoundaryTimestamp = | ||
Date.now() / 1000 - UNPAIRED_DEVICES_LAST_UPDATED_LIMIT_SECONDS; | ||
|
||
const devices = allDevices.filter(it => { | ||
const isDeviceAlreadyConnected = | ||
trezorDevices.find(trezorDevice => trezorDevice.bluetoothProps?.id === it.device.id) !== | ||
undefined; | ||
|
||
if (isDeviceAlreadyConnected) { | ||
return false; | ||
} | ||
|
||
const isDeviceUnresponsiveForTooLong = | ||
it.device.lastUpdatedTimestamp < lasUpdatedBoundaryTimestamp; | ||
|
||
if (isDeviceUnresponsiveForTooLong) { | ||
return knownDevices.find(knownDevice => knownDevice.id === it.device.id) !== undefined; | ||
} | ||
console.log('allDevices', allDevices); | ||
|
||
return true; | ||
}); | ||
const devices = allDevices; | ||
// .filter(it => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be uncommented, I need a fix for this from Szymon |
||
// const isDeviceUnresponsiveForTooLong = | ||
// it.lastUpdatedTimestamp < lasUpdatedBoundaryTimestamp; | ||
// | ||
// if (isDeviceUnresponsiveForTooLong) { | ||
// return knownDevices.find(knownDevice => knownDevice.id === it.id) !== undefined; | ||
// } | ||
// | ||
// return true; | ||
// }); | ||
|
||
const selectedDevice = | ||
selectedDeviceId !== null | ||
? devices.find(device => device.device.id === selectedDeviceId) | ||
? devices.find(device => device.id === selectedDeviceId) | ||
: undefined; | ||
|
||
useEffect(() => { | ||
|
@@ -111,13 +101,8 @@ | |
setScannerTimerId(timerId); | ||
}; | ||
|
||
const onSelect = async (id: string) => { | ||
const onSelect = (id: string) => { | ||
setSelectedDeviceId(id); | ||
const result = await dispatch(bluetoothConnectDeviceThunk({ id })).unwrap(); | ||
|
||
if (uiMode === 'card' && result.success) { | ||
dispatch(closeModalApp()); | ||
} | ||
}; | ||
|
||
if (bluetoothAdapterStatus === 'disabled') { | ||
|
@@ -143,21 +128,27 @@ | |
|
||
if ( | ||
selectedDevice !== undefined && | ||
selectedDevice.status !== null && | ||
selectedDevice.status.type === 'pairing' && | ||
(selectedDevice.status.pin?.length ?? 0) > 0 | ||
selectedDevice !== null && | ||
selectedDevice.connectionStatus.type === 'pairing' && | ||
(selectedDevice.connectionStatus?.pin?.length ?? 0) > 0 | ||
) { | ||
return ( | ||
<BluetoothPairingPin | ||
device={selectedDevice.device} | ||
pairingPin={selectedDevice.status.pin} | ||
device={selectedDevice} | ||
pairingPin={selectedDevice.connectionStatus.pin} | ||
onCancel={handlePairingCancel} | ||
/> | ||
); | ||
} | ||
|
||
if (selectedDevice !== undefined) { | ||
return <BluetoothSelectedDevice device={selectedDevice} onReScanClick={onReScanClick} />; | ||
return ( | ||
<BluetoothSelectedDevice | ||
device={selectedDevice} | ||
onReScanClick={onReScanClick} | ||
onCancel={handlePairingCancel} | ||
/> | ||
); | ||
} | ||
|
||
const content = scanFailed ? ( | ||
|
@@ -166,8 +157,10 @@ | |
<BluetoothDeviceList | ||
isDisabled={false} | ||
onSelect={onSelect} | ||
onError={handlePairingCancel} | ||
deviceList={devices} | ||
isScanning={isScanning} | ||
uiMode={uiMode} | ||
/> | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,29 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { Column, FlexProps, Icon, Row, Text } from '@trezor/components'; | ||
import { DeviceModelInternal } from '@trezor/connect'; | ||
import { models } from '@trezor/connect/src/data/models'; // Todo: solve this import issue | ||
import { RotateDeviceImage } from '@trezor/product-components'; | ||
import { spacings } from '@trezor/theme'; | ||
import { BluetoothDevice } from '@trezor/transport-bluetooth'; | ||
|
||
const TimeAgo = ({ timestamp }: { timestamp: number }) => { | ||
const [secAgo, setSecAgo] = useState(0); | ||
|
||
useEffect(() => { | ||
setSecAgo(Math.floor((Date.now() - timestamp) / 1000)); | ||
const interval = setInterval(() => setSecAgo(t => t + 1), 1000); | ||
|
||
return () => clearInterval(interval); | ||
}, [timestamp]); | ||
|
||
return ( | ||
<> | ||
<Text variant="warning">{secAgo}</Text>s ago | ||
</> | ||
); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be extracted as a reusable component.. |
||
|
||
type BluetoothDeviceProps = { | ||
device: BluetoothDevice; | ||
flex?: FlexProps['flex']; | ||
|
@@ -23,7 +42,7 @@ export const BluetoothDeviceComponent = ({ device, flex, margin }: BluetoothDevi | |
|
||
const model = getModelEnumFromBytesUtil(device.data[2]); | ||
const color = getColorEnumFromVariantBytesUtil(device.data[1]); | ||
const colorName = models[model].colors[color.toString()]; | ||
const colorName = color !== undefined ? models[model].colors[color.toString()] : ''; | ||
|
||
return ( | ||
<Row gap={spacings.md} alignItems="stretch" flex={flex} margin={margin}> | ||
|
@@ -36,7 +55,12 @@ export const BluetoothDeviceComponent = ({ device, flex, margin }: BluetoothDevi | |
|
||
<Column justifyContent="start" alignItems="start" flex="1"> | ||
<Text typographyStyle="body">Trezor Safe 7</Text> | ||
|
||
<Text> | ||
<Text typographyStyle="hint" variant="purple"> | ||
<pre>{device.macAddress}</pre> | ||
</Text> | ||
<TimeAgo timestamp={device.lastUpdatedTimestamp} /> | ||
</Text> | ||
<Row> | ||
<Text typographyStyle="hint" variant="tertiary"> | ||
{colorName} | ||
|
There was a problem hiding this comment.
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 whenbluetoothIpc.connectDevice
finishes, it will triggerdeviceActions.connectDevice
(so it'd be useless to call it twice), or how does it work?