reconnect static method

Future<BrilliantDevice> reconnect(
  1. String 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(
      // note: changed so that sdk users (apps) directly specify reconnect behaviour
      // otherwise there are spurious reconnects even after programmatically disconnecting
      timeout: const Duration(seconds: 5),
      autoConnect: false,
      mtu: null,
    );

    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) {
      return await _enableServices(device);
    }

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