Skip to content

Commit

Permalink
fix: fix smart trainer disconnect issues
Browse files Browse the repository at this point in the history
Fix smart trainer disconnect issues by
adding event listener that actually handles it
  • Loading branch information
morteako committed Jan 12, 2025
1 parent 9ac893c commit 7ba2162
Showing 1 changed file with 18 additions and 0 deletions.
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

0 comments on commit 7ba2162

Please sign in to comment.