reconnect static method

Future<BrilliantDevice> reconnect(
  1. String uuid
)

Reconnects to a device by UUID.

Implementation

static Future<BrilliantDevice> reconnect(String uuid) async {
  try {
    _log.info("Will re-connect to device: $uuid once found");

    BluetoothDevice device = BluetoothDevice.fromId(uuid);

    await device.connect(
      timeout: const Duration(days: 365),
      autoConnect: Platform.isIOS ? true : false,
      mtu: null,
    ); // TODO Should wait but it throws an error on Android after some time

    final connectionState = await device.connectionState.firstWhere((state) =>
        state == BluetoothConnectionState.connected ||
        (state == BluetoothConnectionState.disconnected &&
            device.disconnectReason != null));

    _log.info("Found reconnectable device: $uuid");

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

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