disconnectFrame method

Future<void> disconnectFrame()
inherited

Implementation

Future<void> disconnectFrame() async {
  if (frame != null) {
    try {
      _log.fine('Disconnecting from Frame');
      // break first in case it's sleeping - otherwise the reset won't work
      await frame!.sendBreakSignal();
      _log.fine('Break signal sent');
      // TODO the break signal needs some more time to be processed before we can reliably send the reset signal, by the looks of it
      await Future.delayed(const Duration(milliseconds: 500));

      // cancel the stdout and data subscriptions
      _rxStdOut?.cancel();
      _log.fine('StdOut subscription canceled');
      _rxAppData?.cancel();
      _log.fine('AppData subscription canceled');

      // try to reset device back to running main.lua
      await frame!.sendResetSignal();
      _log.fine('Reset signal sent');
      // TODO the reset signal doesn't seem to be processed in time if we disconnect immediately, so we introduce a delay here to give it more time
      // The sdk's sendResetSignal actually already adds 100ms delay
      // perhaps it's not quite enough.
      await Future.delayed(const Duration(milliseconds: 500));
    } catch (e) {
      _log.fine('Error while sending reset signal: $e');
    }

    try {
      // try to disconnect cleanly if the device allows
      await frame!.disconnect();
    } catch (e) {
      _log.fine('Error while calling disconnect(): $e');
    }
  } else {
    _log.fine('Current device is null, disconnection not possible');
  }

  _batt = null;
  currentState = ApplicationState.disconnected;
  if (mounted) setState(() {});
}