Skip to content

Commit

Permalink
fix: use connect for easing bonds
Browse files Browse the repository at this point in the history
  • Loading branch information
Nodonisko committed Jan 30, 2025
1 parent 0237949 commit 99bdf82
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
28 changes: 5 additions & 23 deletions packages/transport-native-ble/src/api/nativeBleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,24 @@ class NativeBleManager {
const alreadyScannedDevice = this.devicesScanList.find(
d => d.bleDevice.id === scannedDevice.id,
);
console.log('Advertisment payload base64', scannedDevice.rawScanRecord);
console.log(
debugLog('Advertisment payload base64', scannedDevice.rawScanRecord);
debugLog(
'Advertisment payload as ascii',
Buffer.from(scannedDevice.rawScanRecord, 'base64').toString('ascii'),
);
console.log(
debugLog(
'Advertisment payload hex',
Buffer.from(scannedDevice.rawScanRecord, 'base64').toString('hex'),
);
console.log(
debugLog(
'Advertisment payload binary',
bufferToBinaryString(Buffer.from(scannedDevice.rawScanRecord, 'base64')),
);
if (alreadyScannedDevice) {
if (
alreadyScannedDevice.bleDevice.rawScanRecord !== scannedDevice.rawScanRecord
) {
console.log('!!! Advertisment payload changed !!!');
debugLog('!!! Advertisment payload changed !!!');
}
// Device already in the list, update the last seen timestamp
alreadyScannedDevice.lastSeenTimestamp = Date.now();
Expand Down Expand Up @@ -568,24 +568,6 @@ class NativeBleManager {
console.error('Error writing: ', JSON.stringify(e));
}
};

public eraseBondsForAllDevices = async () => {
// TEMPORARY until Connect will have message for erasing bonds
const eraseBondsMessage = Buffer.alloc(244);
eraseBondsMessage.fill(0);
eraseBondsMessage.set([63, 35, 35, 31, 70], 0);

return Promise.all(
this.appConnectedDevices.map(async device => {
await device.bleDevice.writeCharacteristicWithResponseForService(
NUS_SERVICE_UUID,
device.writeUuid,
eraseBondsMessage.toString('base64'),
);
await this.disconnectDevice(device.bleDevice.id);
}),
);
};
}

export const nativeBleManager = new NativeBleManager();
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const screenStyle = prepareNativeStyle(() => ({

export const ConnectingDeviceScreen = () => {
useOnDeviceReadyNavigation();
useHandleHardwareBackNavigation();
// comment this out so we can dismiss the screen by hardware back button because on BT devkit it will freeze there otherwise
// useHandleHardwareBackNavigation();

const { applyStyle } = useNativeStyles();

Expand Down
33 changes: 26 additions & 7 deletions suite-native/module-dev-utils/src/screens/DevUtilsScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Alert } from 'react-native';
import { useState } from 'react';

import { useNavigation } from '@react-navigation/native';
import * as Sentry from '@sentry/react-native';
Expand All @@ -18,6 +19,7 @@ import {
import { clearStorage } from '@suite-native/storage';
import { getCommitHash, getSuiteVersion } from '@trezor/env-utils';
import { logs, nativeBleManager } from '@trezor/transport-native-ble';
import TrezorConnect from '@trezor/connect';

import { BluetoothToggle } from '../components/BluetoothToggle';
import { DevicePassphraseSwitch } from '../components/DevicePassphraseSwitch';
Expand All @@ -35,6 +37,27 @@ type NavigationProps = StackToStackCompositeNavigationProps<
export const DevUtilsScreen = () => {
const navigation = useNavigation<NavigationProps>();
const copyToClipboard = useCopyToClipboard();
const [isErasingBonds, setIsErasingBonds] = useState(false);

const handleEraseBonds = async () => {
setIsErasingBonds(true);
try {
Alert.alert('Please confirm erasing BT bonds on device.');
const result = await TrezorConnect.eraseBonds({});
console.log('result', result);
if (!result.success) {
throw new Error(`${result.payload.code} - ${result.payload.error}`);
}
Alert.alert(
'BT bonds erased - please follow these steps:',
`1. Accept request on Trezor \n2. Restart Trezor device by cutting the power \n2. Click on "Forget device" in system settings \n3. Restart mobile app`,
);
} catch (error) {
console.error(error);
Alert.alert('Error erasing BT bonds', error.message);
}
setIsErasingBonds(false);
};

return (
<Screen screenHeader={<ScreenHeader content="DEV utils" />}>
Expand Down Expand Up @@ -66,14 +89,10 @@ export const DevUtilsScreen = () => {
</>
)}
<Button
onPress={() => {
nativeBleManager.eraseBondsForAllDevices();
Alert.alert(
'BT bonds erased - please follow these steps:',
`1. Accept request on Trezor \n2. Restart Trezor device by cutting the power \n2. Click on "Forget device" in system settings \n3. Restart mobile app`,
);
}}
onPress={handleEraseBonds}
colorScheme="redBold"
isLoading={isErasingBonds}
isDisabled={isErasingBonds}
>
🔵🗑️ Erase BT bonds
</Button>
Expand Down

0 comments on commit 99bdf82

Please sign in to comment.