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

Fix smart trainer disconnect issues #466

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions apps/frontend/src/hooks/useSmartTrainerInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ export const useSmartTrainerInterface = (): SmartTrainerInterface => {
setData({ power, cadence, speed });
};

const onDisconnected = React.useCallback(() => {
toast({
title: 'Trainer Disconnected!',
isClosable: true,
duration: 2000,
status: 'warning',
});
setCurrentResistance(0);
dispatch({
type: 'set_error',
errorMessage: 'Disconnected',
});
logEvent('heart rate monitor auto-disconnected');
}, []);

const requestPermission = async () => {
dispatch({ type: 'set_connecting' });

Expand Down Expand Up @@ -204,6 +219,9 @@ export const useSmartTrainerInterface = (): SmartTrainerInterface => {
indoorBikeDataCharacteristic,
fitnessMachineControlPointCharacteristic,
});

device.removeEventListener('gattserverdisconnected', onDisconnected);
device.addEventListener('gattserverdisconnected', onDisconnected);
} catch (e) {
logEvent(
'Could not connect to device as Fitness Machine. Trying to connect to cycling power.'
Expand Down
Loading