Bluetooth scan not work when using flutter in ios

Viewed 43

I'm using flutter_blue_plus package which is almost same with flutter_blue. When I test with iphone 13 mini connected with mac book xcode, my app scans bluetooth devices well. But when I extract my app to .ipa and install the app using xcode, my iphone 13 mini(which is not connected to mac book) not scan bluetooth devices anymore. Of course I've checked that I give same permission to the iphone both of the test. Is there any suggestion for test or anyone experienced same situation?
I'm very beginner of flutter and ios development (I just started it from a month ago), so please give me any advice if you have about this bluetooth problem..

My scan function is in the background mode, and I checked the scanned devices using snack bar. But I think it is not the problem, the app works well when I test with xcode and the iphone is connected to mac.

I added a part of my code for your information.

(My function for BLE Scanning)

    Future<void> initialScanning() async {
      // ios execute this function with .ipa installed but skips scanning.
      final FlutterBluePlus flutterBlue = FlutterBluePlus.instance;
      // Start scanning
      flutterBlue.startScan(timeout: Duration(seconds: _firstScanDuration));
      // Listen to scan results
      var subscription = flutterBlue.scanResults.listen((results) async {
        // Find the device with the name and Register
        for (ScanResult r in results) {
          print('${r.device.name} rssi: ${r.rssi}');
          if (r.device.name == 'raspberrypi') {
            if (!devices.contains(r.device)) {
              devices.add(r.device);
              print('device added!!!!');
            }
          }
        }
        for (BluetoothDevice device in devices) {
          if (!registeredDevices.contains(device.id.toString())) {
            registeredDevices.add(device.id.toString());
            await someBleRegister(device);
          }
        }
      });
      // Stop scanning
      flutterBlue.stopScan();
    }

(onEvent function for flutter_foreground_task)

    @override
    Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {
      if (_eventCount == 0) {
        await initialScanning();
      } else {
        print('number of devices: ${someBles.length}');
        for (SomeBle ble in someRegisteredBles) {
          await someBleReadAndWrite(ble);
        }
      }
1 Answers

I my self found out the answer that flutter_blue_plus.startScan (i.e. BLE scan) is not work well with flutter_foreground_task package in ios.

When I use the startScan in onStart( a override method of flutter_foreground_task package), the BLE scanning was work well with normal development setup with xcode. But the same code was not work when I made .ipa file and installed to iphone. It never scans anything and skips the scanning.

But when I moved the BLE scanning codes to the foreground, it worked well both xcode connected and .ipa file installed.

I don't know why.. but, anyway, that is true.


(Edit) flutter_reactive_ble not work too in flutter_foreground_task background mode with .ipa file installed.

Related