findAndConnectDevice method

  1. @override
Future<bool> findAndConnectDevice({
  1. required Set<UrpDeviceType> readerTypes,
  2. String? deviceAddress,
})

Returns true if the connection was successful, false otherwise. Will run findDevices and connect to the first reader found.

Implementation

@override
Future<bool> findAndConnectDevice(
    {required Set<UrpDeviceType> readerTypes, String? deviceAddress}) async {
  setStatus(ConnectionStatus.searching);

  await delay(Duration(seconds: 1));

  if (debugFailConnection) {
    setStatus(ConnectionStatus.idle);
    return false;
  }

  for (var reader in _virtualReaders) {
    await delay(Duration(milliseconds: 300));
    if (readerTypes.contains(reader.type) &&
        (deviceAddress == null || deviceAddress == reader.address)) {
      _connectedReaderType = reader.type;
      _connectedReaderAddress = reader.address;
      setStatus(ConnectionStatus.connected);
      return true;
    }
  }

  await delay(Duration(seconds: 5));

  setStatus(ConnectionStatus.idle);

  return false;
}