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

我正在尝试使用方法通道将可用的 wifi 网络发送到颤动。我是 Kotlin 的新手,请帮帮我

   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
    }

我试图从列表中取出一个项目并将其发送到颤动它可以工作,但是当我发送可用 wifi 网络列表时它会引发错误。

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)
4

1 回答 1

0

我们无法返回类类型,而是将其转换为字符串并使用 json.decode() 方法在颤振中对其进行解码。

于 2020-09-12T19:03:54.827 回答