Returning a list as a result to method channel is throwing java.lang.illegalArgumentException?

huangapple go评论80阅读模式
英文:

Returning a list as a result to method channel is throwing java.lang.illegalArgumentException?

问题

 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()
            }
        }
    }
   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
    }

Error message:

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)
英文:
 override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel).setMethodCallHandler{ call, result -&gt;
            if (call.method == &quot;getBatteryLevel&quot;) {
               val batteryLevel = getBatteryLevel()
                if (batteryLevel != -1) {
                    result.success(batteryLevel)
                } else {
                    result.error(&quot;UNAVAILABLE&quot;, &quot;Battery level not available.&quot;, null)
                }
            } else if(call.method == &quot;getWifiConnectionList&quot;){
                val ans : List&lt;ScanResult&gt; = 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&lt;ScanResult&gt; {
        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)

答案1

得分: 1

我们不能返回类类型,所以我将其转换为字符串,并在Flutter中使用json.decode()方法进行解码。

英文:

We can't return class types instead I converted it to string and decoded it in flutter using json.decode() method.

huangapple
  • 本文由 发表于 2020年9月12日 03:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63853412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定