connectToDevice method

Stream<ConnectionStateUpdate> connectToDevice({
  1. required String id,
  2. Map<Uuid, List<Uuid>>? servicesWithCharacteristicsToDiscover,
  3. Duration? connectionTimeout,
})

Establishes a connection to a BLE device.

Disconnecting the device is achieved by cancelling the stream subscription.

id is the unique device id of the BLE device: in iOS this is a uuid and on Android this is a Mac-Address. Use servicesWithCharacteristicsToDiscover to scan only for the specific services mentioned in this map, this can improve the connection speed on iOS since no full service discovery will be executed. On Android this variable is ignored since partial discovery is not possible. If connectionTimeout parameter is supplied and a connection is not established before connectionTimeout expires, the pending connection attempt will be cancelled and a TimeoutException error will be emitted into the returned stream. On Android when no timeout is specified the autoConnect flag is set in the connectGatt() call, otherwise it is cleared.

Implementation

Stream<ConnectionStateUpdate> connectToDevice({
  required String id,
  Map<Uuid, List<Uuid>>? servicesWithCharacteristicsToDiscover,
  Duration? connectionTimeout,
}) =>
    initialize().asStream().asyncExpand(
          (_) => _deviceConnector.connect(
            id: id,
            servicesWithCharacteristicsToDiscover: servicesWithCharacteristicsToDiscover,
            connectionTimeout: connectionTimeout,
          ),
        );