override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel).setMethodCallHandler{ call, result ->
if (call.method == "getBatteryLevel") {
val batteryLevel = getBatteryLevel()
if (batteryLevel != -1) {
result.success(batteryLevel)
} else {
result.error("UNAVAILABLE", "Battery level not available.", null)
}
} else if(call.method == "getWifiConnectionList"){
val ans : List<ScanResult> = getWifiScanResults(context)
result.success(ans)
} else {
result.notImplemented()
}
}
}
I'm try to send the available wifi networks to flutter using method channel. I'm new to Kotlin please help me out
private fun getWifiScanResults(context: Context): List<ScanResult> {
val mWifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
Toast.makeText(context, mWifiManager.scanResults[0].SSID, Toast.LENGTH_LONG).show()
return mWifiManager.scanResults
}
I tried to take an item from the list and sent it to flutter it works but when I send the list of available wifi networks it throws an error.
E/MethodChannel#com.codever: Failed to handle method call
java.lang.IllegalArgumentException: Unsupported value: SSID: Rahul Dubey , BSSID: 34:e8:94:28:57:3a, capabilities: [WPA2-PSK-CCMP][RSN-PSK-CCMP][ESS], level: -59, frequency: 2462, timestamp: 51104155586, distance: ?(cm), distanceSd: ?(cm), passpoint: no, ChannelBandwidth: 1, centerFreq0: 2452, centerFreq1: 0, 80211mcResponder: is not supported, Carrier AP: no, Carrier AP EAP Type: -1, Carrier name: null, Radio Chain Infos: null, wifiMode: 4, semVendorSpecificInfo: null, semBssLoadElement: 040000127A
at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:278)
at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:267)
at io.flutter.plugin.common.StandardMethodCodec.encodeSuccessEnvelope(StandardMethodCodec.java:59)
at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.success(MethodChannel.java:235)
at com.example.rahul_app.MainActivity$configureFlutterEngine$1.onMethodCall(MainActivity.kt:40)
at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:230)
at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:692)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:336)
at android.os.Looper.loop(Looper.java:197)
at android.app.ActivityThread.main(ActivityThread.java:8125)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
We can't return class types instead I converted it to string and decoded it in flutter using json.decode() method.