Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

While calling scnaNetworks() app crashed. #7

Closed
DhavalRKansara opened this issue Mar 2, 2020 · 4 comments
Closed

While calling scnaNetworks() app crashed. #7

DhavalRKansara opened this issue Mar 2, 2020 · 4 comments

Comments

@DhavalRKansara
Copy link

While calling ScanNetworks() method Application get crashed and receive below error.

  • Code:
_getAvailableWifi() async {
  print("Fetching wifis");
  List<WifiNetwork> filteredList = [];
  bool match = true;

  final networks1 = WifiFlutter.scanNetworks();
  networks1.then((val) {
    val.forEach((result) {
      if (filteredList.isEmpty) {
        filteredList.add(result);
      } else {
        for (WifiNetwork fResult in filteredList) {
          if (result.ssid == fResult.ssid && match) {
            match = false;
          }
        }
        if (match) {
          filteredList.add(result);
        }
        match = true;
      }
    });
    for (WifiNetwork wifi in filteredList) {
      print("secure ${wifi.isSecure} ssid ${wifi.ssid}");
    }
    });
}
  • Exception:
I/flutter (10303): Fetching wifis
E/flutter (10303): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(startScan, Unable to start scan., false)
E/flutter (10303): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (10303): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter (10303): <asynchronous suspension>
E/flutter (10303): #2      WifiFlutter.scanNetworks (package:wifi_flutter/wifi_flutter.dart:26:37)
E/flutter (10303): <asynchronous suspension>
E/flutter (10303): #3      _getAvailableWifi (package:mqttdemo/main.dart:133:33)
E/flutter (10303): <asynchronous suspension>
E/flutter (10303): #4      _MyHomePageState.build.<anonymous closure> (package:mqttdemo/main.dart:55:17)
E/flutter (10303): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
E/flutter (10303): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
E/flutter (10303): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (10303): #8      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter (10303): #9      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
E/flutter (10303): #10     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
E/flutter (10303): #11     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
E/flutter (10303): #12     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
E/flutter (10303): #13     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (10303): #14     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (10303): #15     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (10303): #16     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (10303): #17     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (10303): #18     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (10303): #19     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (10303): #20     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (10303): #21     _invoke1 (dart:ui/hooks.dart:263:10)
E/flutter (10303): #22     _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)
E/flutter (10303): 
D/AndroidRuntime(10303): Shutting down VM
E/AndroidRuntime(10303): FATAL EXCEPTION: main
E/AndroidRuntime(10303): Process: com.example.mqttdemo, PID: 10303
E/AndroidRuntime(10303): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 pkg=com.example.mqttdemo (has extras) } in com.weplenish.wifi_flutter.WifiScanReceiver@7a2aa6d
E/AndroidRuntime(10303): 	at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1560)
E/AndroidRuntime(10303): 	at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
E/AndroidRuntime(10303): 	at android.os.Handler.handleCallback(Handler.java:883)
E/AndroidRuntime(10303): 	at android.os.Handler.dispatchMessage(Handler.java:100)
E/AndroidRuntime(10303): 	at android.os.Looper.loop(Looper.java:214)
E/AndroidRuntime(10303): 	at android.app.ActivityThread.main(ActivityThread.java:7356)
E/AndroidRuntime(10303): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(10303): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/AndroidRuntime(10303): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
E/AndroidRuntime(10303): Caused by: java.lang.IllegalStateException: Reply already submitted
E/AndroidRuntime(10303): 	at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:151)
E/AndroidRuntime(10303): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:225)
E/AndroidRuntime(10303): 	at com.weplenish.wifi_flutter.WifiScanReceiver.onReceive(WifiFlutterPlugin.kt:78)
E/AndroidRuntime(10303): 	at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1550)
E/AndroidRuntime(10303): 	... 8 more
@DhavalRKansara
Copy link
Author

Google has now documented the limitations for startScan() function in Android P:

"We are further limiting the number of scans apps can request to improve network performance and improve battery life.

The WifiManager.startScan() usage is limited to: - Each foreground app is restricted to 4 scans every 2 minutes. - All background apps combined are restricted to one scan every 30 minutes."

Source: https://issuetracker.google.com/issues/79906367

Edit 8-Aug-2018: Information has been added also here: https://developer.android.com/guide/topics/connectivity/wifi-scan

@zeeshan0309
Copy link

zeeshan0309 commented Jan 6, 2021

Hey! I too am facing this error, and probably its due to this throttle limitation only. So can I get some help for knowing how exactly can I use this scanNetworks() method encountering this error? Is it totally impossible for us to call the scanNetworks() function, even after (lets say) a gap of 45 seconds or a minute? I tried giving this much of delay between two scanNetworks() calls, but the error persists...

@vipero07
Copy link
Member

vipero07 commented Jan 7, 2021

You need to ensure that you have the correct permissions to make a scan wifi request and you need to make the requests in code before scan wifi is called:
https://pub.dev/packages/permission_handler can get you there.
Don't forget the lines in android's manifest too.

I don't suggest wrapping the error in a try catch as in reality you should require the user grant the necessary permissions before trying to scan networks.

@vipero07
Copy link
Member

vipero07 commented Jan 7, 2021

Depending on your use case though https://github.com/weplenish/flutter_wifi_connect may be a better fit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants