WifiInfo 返回 null
在遵循此 在线指南。我正在使用目标 API 30(在使用 Android 11 的设备上构建)。尝试使用 networkCapability
获取 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
I have been working on this for a few days after following this guide online. I am using target API 30 (building on a device using Android 11). Trying to get the Wifi SSID using networkCapabilities
this keeps returning null whereas the depreciated wifiManager.connectionInfo.ssid
returns the correct SSID. Any ideas what I am doing wrong? I know this will need to change for API 31 to include the FLAG_INCLUDE_LOCATION_INFO
but I believe this should be working for API 30. Any ideas?
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)
}
}
Permissions in 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 versions
compileSdkVersion 31
buildToolsVersion "30.0.3"
minSdkVersion 30
targetSdkVersion 31
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建
NetworkCallback
对象时,尝试像这样传递给构造函数FLAG_INCLUDE_LOCATION_INFO
:这样我们就告诉我们想要接收一些有关网络的敏感数据,但它会起作用遗憾的是,仅适用于 API 级别 31 及更高级别。
更多信息此处
When creating your
NetworkCallback
object, try pass to the constructorFLAG_INCLUDE_LOCATION_INFO
like so:This way we are telling that we want to receive some sensitive data about the network, but it will work only with API level 31 and higher sadly.
More info here