connect method

Future<BtcConnection> connect({
  1. required String address,
  2. String uuid = BtcUuid.spp,
  3. bool secure = true,
  4. Duration? timeout,
})

Connects to the device at address using the given uuid.

uuid defaults to BtcUuid.spp (the Serial Port Profile), which is what HC-05/HC-06, ESP32/ESP8266, Arduino, and most serial devices use — so for the common case you only need connect(address: ...).

If secure is true (default), uses authenticated/encrypted RFCOMM. Returns a BtcConnection for reading/writing data.

If timeout is provided, the attempt fails with a BtcTimeoutException when it does not complete in time. Note: the native connection attempt may still resolve afterwards on some platforms; the returned connection (if any) is then orphaned — call disconnect if you track its id.

Platform Supported
Android Yes
Windows Yes
macOS Yes
Linux Yes
iOS ⚠️ (MFi accessories via protocol string)

Throws BtcAddressException if address is not a valid MAC. Throws BtcUuidException if uuid is not a valid UUID. Throws BtcTimeoutException if timeout elapses first.

Implementation

Future<BtcConnection> connect({
  required String address,
  String uuid = BtcUuid.spp,
  bool secure = true,
  Duration? timeout,
}) {
  _validateAddress(address);
  _validateUuid(uuid);
  final future =
      _platform.connect(address: address, uuid: uuid, secure: secure);
  if (timeout == null) return future;
  return future.timeout(
    timeout,
    onTimeout: () => throw BtcTimeoutException(
      message: 'Connection to $address timed out',
      timeoutMs: timeout.inMilliseconds,
    ),
  );
}