getBLEPrinters method

Future<List<BLEPrinter>> getBLEPrinters(
  1. int timeout
)

Discover printers which are connectable via BLE. Available on Android 5.0 or later.

Implementation

Future<List<BLEPrinter>> getBLEPrinters(int timeout) async {

  // TODO Consider moving this to iOS side.
  if (Platform.isIOS) {
    //BLE Scanning
    FlutterBluePlus? flutterBlue = FlutterBluePlus();

    // Start scanning
    FlutterBluePlus.startScan(withServices: [Guid("A76EB9E0-F3AC-4990-84CF-3A94D2426B2B")], timeout: Duration(seconds: timeout~/1000));

    Set<BLEPrinter> foundDevices = {};
    // Listen to scan results
    var subscription = FlutterBluePlus.scanResults.listen((results) {
      for (ScanResult r in results) {
        BLEPrinter found = BLEPrinter(localName: r.device.name);
        if (!foundDevices.contains(found)) {
          foundDevices.add(found);
        }
      }
    });

    return await Future.delayed(Duration(seconds: timeout~/1000), () => foundDevices.toList());
  }

  var params = {
    "printerId": mPrinterId,
    "printInfo": mPrinterInfo.toMap(),
    "timeout" : timeout,
  };

  final List<dynamic> resultList = await _channel.invokeMethod("getBLEPrinters", params);

  final List<BLEPrinter> outList = resultList.map( (blePrinter) => BLEPrinter.fromMap(blePrinter)).toList();
  return outList;
}