gpt4 book ai didi

android - WifiInfo 返回 null

转载 作者:行者123 更新时间:2023-12-05 00:02:01 24 4
gpt4 key购买 nike

在关注此 guide 之后,我已经为此工作了几天在线的。我正在使用目标 API 30(在使用 Android 11 的设备上构建)。尝试使用 networkCapabilities 获取 Wifi SSID这一直返回 null 而贬值的 wifiManager.connectionInfo.ssid返回正确的 SSID。任何想法我做错了什么?我知道这需要更改 API 31 以包含 FLAG_INCLUDE_LOCATION_INFO但我相信这应该适用于 API 30。有什么想法吗?

import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.util.Log
import androidx.lifecycle.LiveData

private const val TAG = "NetworkStatus"

enum class NetworkStatus {
UNAVAILABLE,
CONNECTED_TO_XXXX,
CONNECTED_TO_OTHER,
CONNECTION_LOST
}

class NetworkStatusHelper(private val context: Context) : LiveData<Pair<NetworkStatus, String>>() {

private lateinit var connectivityManagerCallback: ConnectivityManager.NetworkCallback
var connectivityManager: ConnectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

val wifiManager =
context.getSystemService(Context.WIFI_SERVICE) as WifiManager

private fun getConnectivityManagerCallback() = object : ConnectivityManager.NetworkCallback() {

override fun onUnavailable() {
super.onUnavailable()
announceStatus(Pair(NetworkStatus.UNAVAILABLE, ""))
}

override fun onAvailable(network: Network) {
super.onAvailable(network)

}

override fun onCapabilitiesChanged(
network: Network,
networkCapabilities: NetworkCapabilities
) {
super.onCapabilitiesChanged(network, networkCapabilities)

Log.d(TAG, "onCapabilitiesChanged: transport info = ${connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)?.transportInfo}")
try {
val wifiInfo = networkCapabilities.transportInfo as WifiInfo

Log.d(TAG, "onCapabilitiesChanged: wifi SSID ${wifiInfo}")

if (wifiInfo.ssid.contains("XXXX-")) {
announceStatus(Pair(NetworkStatus.CONNECTED_TO_XXXX, wifiInfo.ssid))
} else {
announceStatus(Pair(NetworkStatus.CONNECTED_TO_OTHER, wifiInfo.ssid))
}

}
catch (e: Exception) {
Log.d(TAG, "onCapabilitiesChanged: exception = $e")
}

try {
Log.d(TAG, "onCapabilitiesChanged: ${wifiManager.connectionInfo.ssid}")
} catch (e: Exception) {
Log.d(TAG, "onAvailable: exception = $e")
}

}

override fun onLost(network: Network) {
super.onLost(network)
announceStatus(Pair(NetworkStatus.CONNECTION_LOST, ""))
}

}

fun announceStatus(networkResposePair: Pair<NetworkStatus, String>) {
postValue(networkResposePair)
}

override fun onActive() {
super.onActive()
connectivityManagerCallback = getConnectivityManagerCallback()
val networkRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build()
connectivityManager.registerNetworkCallback(networkRequest, connectivityManagerCallback)
}

override fun onInactive() {
super.onInactive()
connectivityManager.unregisterNetworkCallback(connectivityManagerCallback)
}
}
Gradle 中的权限:
    <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
SDK 版本
    compileSdkVersion 31
buildToolsVersion "30.0.3"
minSdkVersion 30
targetSdkVersion 31

最佳答案

创建您的 NetworkCallback 时对象,尝试传递给构造函数FLAG_INCLUDE_LOCATION_INFO像这样:

object : ConnectivityManager.NetworkCallback(FLAG_INCLUDE_LOCATION_INFO) { /* your code */ }
这样我们就告诉我们想要接收一些关于网络的敏感数据,但遗憾的是它只能在 API 级别 31 或更高级别上工作。
更多信息 here

关于android - WifiInfo 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71233591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com