getBLEPrinters method

Future<List<BLEPrinter>> getBLEPrinters({
  1. int timeout = 5000,
})

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

Implementation

Future<List<BLEPrinter>> getBLEPrinters({int timeout = 5000}) async {
  //BLE Scanning
  //FlutterBluePlus flutterBlue = FlutterBluePlus.instance;

  // Start scanning
  FlutterBluePlus.startScan(
    // Note: For some reason it does not find the printer even though this is the service
    //withServices: [Guid("49535343-FE7D-4AE5-8FA9-9FAFD205E455"),
    // Guid("F0DD799C-C883-4976-96A5-8BB4907F41D6")
    //],
      timeout: Duration(seconds: timeout ~/ 1000));

  Set<BLEPrinter> foundDevices = {};
  // Listen to scan results
  var subscription = FlutterBluePlus.scanResults.listen((results) {
    for (ScanResult r in results) {
      print("Scan Result: ${r.device}");

      BLEPrinter foundSt = BLEPrinter(localName: r.device.name);
      TbBlePrinter found = TbBlePrinter._(foundSt);

      // For now just filter by device name until we get service working.
      if (found.localName.startsWith(_printerInfo.printerModel.getName()) && !foundDevices.contains(found)) {
        foundDevices.add(found);
      }
    }
  });

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