discoverDevices method

Future<List<DiscoveredDevice>> discoverDevices({
  1. String? type,
  2. Duration timeout = const Duration(seconds: 5),
})

Implementation

Future<List<DiscoveredDevice>> discoverDevices(
    {String? type, Duration timeout: const Duration(seconds: 5)}) {
  return discoverClients(timeout: timeout).then((clients) {
    if (clients.isEmpty) {
      return [];
    }

    var uuids = clients
        .where((client) => client.usn.isNotEmpty)
        .map((client) => client.usn.split("::").first)
        .toSet();
    var devices = <DiscoveredDevice>[];

    for (var uuid in uuids) {
      var deviceClients = clients.where((client) {
        return client.usn.isNotEmpty && client.usn.split("::").first == uuid;
      }).toList();
      var location = deviceClients.first.location;
      var serviceTypes = deviceClients.map((it) => it.st).toSet().toList();
      var device = DiscoveredDevice(
          serviceTypes: serviceTypes, uuid: uuid, location: location);
      if (type == null || serviceTypes.contains(type)) {
        devices.add(device);
      }
    }

    for (var client in clients.where((it) => it.usn.isEmpty)) {
      var device = DiscoveredDevice(
          serviceTypes: [client.st], uuid: '', location: client.location);
      if (type == null || device.serviceTypes.contains(type)) {
        devices.add(device);
      }
    }

    return devices;
  });
}