connect static method

Connects to a scanned device.

Implementation

static Future<BrilliantDevice> connect(BrilliantScannedDevice scanned) async {
  try {
    _log.info("Connecting");

    await FlutterBluePlus.stopScan();

    await scanned.device.connect(
      autoConnect: Platform.isIOS ? true : false,
      mtu: null,
    );

    final connectionState = await scanned.device.connectionState
        .firstWhere((event) => event == BluetoothConnectionState.connected)
        .timeout(const Duration(seconds: 3));

    if (connectionState == BluetoothConnectionState.connected) {
      if (Platform.isAndroid) await scanned.device.requestMtu(512);
      return await _enableServices(scanned.device);
    }

    throw ("${scanned.device.disconnectReason?.description}");
  } catch (error) {
    await scanned.device.disconnect();
    _log.warning("Couldn't connect. $error");
    return Future.error(BrilliantBluetoothException(error.toString()));
  }
}