getService method
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 UniversalBleException
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 UniversalBleException(
code: UniversalBleErrorCode.serviceNotFound,
message: 'No services found',
);
}
return discoveredServices.firstWhere(
(s) => BleUuidParser.compareStrings(s.uuid, service),
orElse: () => throw UniversalBleException(
code: UniversalBleErrorCode.serviceNotFound,
message: 'Service "$service" not available',
),
);
}