在Android Kotlin中获取WiFi信号强度(以dBm为单位)

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

Android Kotlin Get WiFi Signal Strength in dBm

问题

我正在开发一个需要获取WiFi信号强度(以dBm为单位)的Flutter应用程序。我的Flutter应用程序使用Android Kotlin和Android 12以及Android SDK 33。

我在互联网上找到了一些示例代码(当然还有Stackoverflow),但它们似乎在我使用的最新Android版本中已经过时。

我还在Android文档中找到了SignalStrength的信息,网址为https://developer.android.com/reference/kotlin/android/telephony/SignalStrength,但我不知道如何使用它,那里没有示例代码。

有人可以帮助我处理这个问题吗?

非常感谢。

英文:

I'm developing a Flutter app that requires getting WiFi Signal Strength in dBm. My Flutter app is using Android Kotlin and Android 12 with Android SDK 33

I found some sample code on the Internet (and Stackoverflow of course) but they seem to be deprecated in the latest Android version that I am using.

I also found SignalStrength in Android Docs at https://developer.android.com/reference/kotlin/android/telephony/SignalStrength but I don't know how to use this, there is no sample code.

Could anyone help me to work with this?

Thank you very much.

答案1

得分: 0

以下是您要翻译的代码部分:

对于那些正在寻找解决方案的人这是我的工作代码
<!-- language: lang-js -->

package com.example.myapp

import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.content.BroadcastReceiver
import android.net.wifi.WifiManager
import android.net.ConnectivityManager
import android.net.ConnectivityManager.NetworkCallback
import android.net.NetworkCapabilities
import android.net.Network
import android.net.TransportInfo
import android.net.wifi.WifiInfo
import android.net.NetworkRequest
import android.util.Log

import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
    private val CHANNEL = "myapp.flutter.dev"
    private var wifiSignalStrength: Int = 0

    val networkRequest: NetworkRequest = NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build()

    val networkCallback = object: NetworkCallback() {
        override fun onAvailable(network: Network) {}

        override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
            val wifiInfo: WifiInfo = networkCapabilities.getTransportInfo() as WifiInfo
            wifiSignalStrength = wifiInfo.getRssi()
            Log.d("[log]", "wifiSignalStrength ${wifiSignalStrength}")
        }
    }

    val myRssiChangeReceiver = object : BroadcastReceiver() {
        override fun onReceive(p0: Context?, p1: Intent?) {
            val wifiManager = getSystemService(Context.WIFI_SERVICE) as WifiManager
            if (VERSION.SDK_INT >= VERSION_CODES.S) {
                val wifiInfo = wifiManager.getConnectionInfo()
                wifiSignalStrength = wifiInfo.getRssi()
            }
        }
    }

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        if (VERSION.SDK_INT >= VERSION_CODES.S) {
            val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            connectivityManager.requestNetwork(networkRequest, networkCallback) // For request
            connectivityManager.registerNetworkCallback(networkRequest, networkCallback) // For listen
        }
        else {
            registerReceiver(myRssiChangeReceiver, IntentFilter(WifiManager.RSSI_CHANGED_ACTION))
        }
        
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
            call, result ->
                if (call.method == "getWifiSignalStrength") {
                    result.success(wifiSignalStrength)
                }
                else {
                    result.notImplemented()
                }
        }
    }
}

请注意,我已经保留了原始HTML代码中的<!-- language: lang-js -->,因为这是代码块的标记。

英文:

For those who is searching for the solution. Here is my working code:
<!-- language: lang-js -->

package com.example.myapp
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.content.BroadcastReceiver
import android.net.wifi.WifiManager
import android.net.ConnectivityManager
import android.net.ConnectivityManager.NetworkCallback
import android.net.NetworkCapabilities
import android.net.Network
import android.net.TransportInfo
import android.net.wifi.WifiInfo
import android.net.NetworkRequest
import android.util.Log
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity: FlutterActivity() {
private val CHANNEL = &quot;myapp.flutter.dev&quot;
private var wifiSignalStrength: Int = 0
val networkRequest: NetworkRequest = NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build()
val networkCallback = object: NetworkCallback() {
override fun onAvailable(network: Network) {}
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
val wifiInfo: WifiInfo = networkCapabilities.getTransportInfo() as WifiInfo
wifiSignalStrength = wifiInfo.getRssi()
Log.d(&quot;[log]&quot;, &quot;wifiSignalStrength ${wifiSignalStrength}&quot;)
}
}
val myRssiChangeReceiver = object : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
val wifiManager = getSystemService(Context.WIFI_SERVICE) as WifiManager
if (VERSION.SDK_INT &gt;= VERSION_CODES.S) {
val wifiInfo = wifiManager.getConnectionInfo()
wifiSignalStrength = wifiInfo.getRssi()
}
}
}
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
if (VERSION.SDK_INT &gt;= VERSION_CODES.S) {
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.requestNetwork(networkRequest, networkCallback) // For request
connectivityManager.registerNetworkCallback(networkRequest, networkCallback) // For listen
}
else {
registerReceiver(myRssiChangeReceiver, IntentFilter(WifiManager.RSSI_CHANGED_ACTION))
}
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
call, result -&gt;
if (call.method == &quot;getWifiSignalStrength&quot;) {
result.success(wifiSignalStrength)
}
else {
result.notImplemented()
}
}
}
}

huangapple
  • 本文由 发表于 2023年7月20日 18:44:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729083.html
匿名

发表评论

匿名网友

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

确定