Skip to content

Commit

Permalink
Use WifiManager deprecations again for instant 'location' info (#3705)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpelgrom authored Jul 24, 2023
1 parent a0a3c51 commit 1cbfd11
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.homeassistant.companion.android.common.data.wifi

import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
import javax.inject.Inject
Expand Down Expand Up @@ -46,22 +45,8 @@ class WifiHelperImpl @Inject constructor(
}

override fun getWifiSsid(): String? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
connectivityManager.activeNetwork?.let {
val info = connectivityManager.getNetworkCapabilities(it)?.transportInfo ?: return null
(info as? WifiInfo)?.ssid
}
} else {
wifiManager?.connectionInfo?.ssid
}
wifiManager?.connectionInfo?.ssid // Deprecated but callback doesn't provide SSID info instantly

override fun getWifiBssid(): String? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
connectivityManager.activeNetwork?.let {
val info = connectivityManager.getNetworkCapabilities(it)?.transportInfo ?: return null
(info as? WifiInfo)?.bssid
}
} else {
wifiManager?.connectionInfo?.bssid
}
wifiManager?.connectionInfo?.bssid // Deprecated but callback doesn't provide BSSID info instantly
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ class NetworkSensorManager : SensorManager {
var connected = false

if (checkPermission(context, wifiConnection.id)) {
conInfo = getWifiConnectionInfo(context)
@Suppress("DEPRECATION") // Unable to get SSID info (instantly) using callback
conInfo = context.getSystemService<WifiManager>()?.connectionInfo

if (conInfo == null || conInfo.networkId == -1) {
if (conInfo == null || conInfo.linkSpeed == -1) {
Expand Down Expand Up @@ -230,7 +231,8 @@ class NetworkSensorManager : SensorManager {
var conInfo: WifiInfo? = null

if (checkPermission(context, bssidState.id)) {
conInfo = getWifiConnectionInfo(context)
@Suppress("DEPRECATION") // Unable to get BSSID info (instantly) using callback
conInfo = context.getSystemService<WifiManager>()?.connectionInfo
}

var bssid = if (conInfo?.bssid == null) "<not connected>" else conInfo.bssid
Expand Down Expand Up @@ -522,6 +524,7 @@ class NetworkSensorManager : SensorManager {
)
}

/** Get WiFi connection info (without location data such as (B)SSID on Android >=S) */
private fun getWifiConnectionInfo(context: Context): WifiInfo? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val connectivityManager = context.applicationContext.getSystemService<ConnectivityManager>()
Expand Down

0 comments on commit 1cbfd11

Please sign in to comment.