checkHeartbeats method

  1. @override
Future<void> checkHeartbeats()

Sends or waits for heartbeats to or from the other device.

Implementation

@override
Future<void> checkHeartbeats() async {
  if (_isChecking) return;
  // 1. Clear state and send a heartbeat
  _isChecking = true;
  _heartbeats = 0;
  final wasConnected = isConnected;
  sendMessage(Connect(sender: Device.DASHBOARD, receiver: device));
  // 2. Wait a bit and count the number of responses
  await Future<void>.delayed(heartbeatWaitDelay);
  if (_heartbeats > 0) {
    connectionStrength.value += connectionIncrement * _heartbeats;
  } else {
    connectionStrength.value -= connectionIncrement;
  }
  // 3. Assess the current state
  connectionStrength.value = connectionStrength.value.clamp(0, 1);
  if (isConnected && !wasConnected) connectionStatus.value = true;
  if (wasConnected && !isConnected) connectionStatus.value = false;
  _isChecking = false;
}