getService method

Future<BleService> getService(
  1. String service, {
  2. bool preferCached = true,
  3. Duration? timeout,
})

Retrieves a specific service.

service is the UUID of the service. preferCached indicates whether to use cached services. If cache is empty, discoverServices() will be called. might throw NotFoundException

Implementation

Future<BleService> getService(
  String service, {
  bool preferCached = true,
  Duration? timeout,
}) async {
  List<BleService> discoveredServices = [];
  if (preferCached) {
    discoveredServices = CacheHandler.instance.getServices(deviceId) ?? [];
  }
  if (discoveredServices.isEmpty) {
    discoveredServices = await discoverServices(timeout: timeout);
  }

  if (discoveredServices.isEmpty) {
    throw ServiceNotFoundException('No services found');
  }

  return discoveredServices.firstWhere(
    (s) => BleUuidParser.compareStrings(s.uuid, service),
    orElse: () => throw ServiceNotFoundException(
      'Service "$service" not available',
    ),
  );
}