connectionState property

Stream<BrilliantDevice> get connectionState

Implementation

Stream<BrilliantDevice> get connectionState {
  // changed to only listen for connectionState data coming from the Frame device rather than all events from all devices as before
  return device.connectionState
      .where((event) =>
          event == BluetoothConnectionState.connected ||
          (event == BluetoothConnectionState.disconnected &&
              device.disconnectReason != null &&
              device.disconnectReason!.code != 23789258))
      .asyncMap((event) async {
    if (event == BluetoothConnectionState.connected) {
      _log.info("Connection state stream: Connected");
      try {
        return await BrilliantBluetooth._enableServices(device);
      } catch (error) {
        _log.warning("Connection state stream: Invalid due to $error");
        return Future.error(BrilliantBluetoothException(error.toString()));
      }
    }
    _log.info(
        "Connection state stream: Disconnected due to ${device.disconnectReason!.description}");
    // Note: automatic reconnection isn't suitable for all cases, so it might
    // be better to leave this up to the sdk user to specify. iOS appears to
    // use FBP's native autoconnect, so if Android behaviour would change then
    // iOS probably should as well
    // if (Platform.isAndroid) {
    //   event.device.connect(timeout: const Duration(days: 365));
    // }
    return BrilliantDevice(
      state: BrilliantConnectionState.disconnected,
      device: device,
    );
  });
}