connectToAdvertisingDevice method

Stream<ConnectionStateUpdate> connectToAdvertisingDevice({
  1. required String id,
  2. required List<Uuid> withServices,
  3. required Duration prescanDuration,
  4. Map<Uuid, List<Uuid>>? servicesWithCharacteristicsToDiscover,
  5. Duration? connectionTimeout,
})

Scans for a specific device and connects to it in case a device containing the specified id is found and that is advertising the services specified in withServices.

Disconnecting the device is achieved by cancelling the stream subscription.

The prescanDuration is the amount of time BLE discovery should run in order to find the device. 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> connectToAdvertisingDevice({
  required String id,
  required List<Uuid> withServices,
  required Duration prescanDuration,
  Map<Uuid, List<Uuid>>? servicesWithCharacteristicsToDiscover,
  Duration? connectionTimeout,
}) =>
    initialize().asStream().asyncExpand(
          (_) => _deviceConnector.connectToAdvertisingDevice(
            id: id,
            withServices: withServices,
            prescanDuration: prescanDuration,
            servicesWithCharacteristicsToDiscover: servicesWithCharacteristicsToDiscover,
            connectionTimeout: connectionTimeout,
          ),
        );