From 8e8ff11adff3e84404a3171d554b716b133f495b Mon Sep 17 00:00:00 2001 From: Anna Tikhomirova Date: Wed, 18 Jan 2023 22:54:49 +0300 Subject: [PATCH] Wi-Fi sensor improvements Use rssi instead of scanResults to reliably get signal strength. Get some info (IP, link speed, frequency, signal strength) and determine connection status even if Location is disabled on a device. --- .../common/sensors/NetworkSensorManager.kt | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/common/src/main/java/io/homeassistant/companion/android/common/sensors/NetworkSensorManager.kt b/common/src/main/java/io/homeassistant/companion/android/common/sensors/NetworkSensorManager.kt index a145710ad6d..ac4a9bc0b2e 100644 --- a/common/src/main/java/io/homeassistant/companion/android/common/sensors/NetworkSensorManager.kt +++ b/common/src/main/java/io/homeassistant/companion/android/common/sensors/NetworkSensorManager.kt @@ -182,20 +182,27 @@ class NetworkSensorManager : SensorManager { var conInfo: WifiInfo? = null var ssid = "Unknown" + var connected = false if (checkPermission(context, wifiConnection.id)) { val wifiManager = context.applicationContext.getSystemService()!! conInfo = wifiManager.connectionInfo - ssid = if (conInfo.networkId == -1) { - "" + if (conInfo.networkId == -1) { + if (conInfo.linkSpeed == -1) { + ssid = "" + } else { + ssid = "" + connected = true + } } else { - conInfo.ssid.removePrefix("\"").removeSuffix("\"") + ssid = conInfo.ssid.removePrefix("\"").removeSuffix("\"") + connected = true } } - val icon = if (ssid != "") "mdi:wifi" else "mdi:wifi-off" + val icon = if (connected) "mdi:wifi" else "mdi:wifi-off" val attributes = conInfo?.let { mapOf("is_hidden" to conInfo.hiddenSSID) @@ -264,7 +271,7 @@ class NetworkSensorManager : SensorManager { context.applicationContext.getSystemService()!! val conInfo = wifiManager.connectionInfo - deviceIp = if (conInfo.networkId == -1) { + deviceIp = if (conInfo.networkId == -1 && conInfo.linkSpeed == -1) { "" } else { getIpAddress(conInfo.ipAddress) @@ -285,27 +292,27 @@ class NetworkSensorManager : SensorManager { return var linkSpeed = 0 - var lastScanStrength = -1 + var rssi = -1 if (checkPermission(context, wifiLinkSpeed.id)) { val wifiManager = context.applicationContext.getSystemService()!! val conInfo = wifiManager.connectionInfo - linkSpeed = if (conInfo.networkId == -1) { + linkSpeed = if (conInfo.linkSpeed == -1) { 0 } else { conInfo.linkSpeed } - lastScanStrength = wifiManager.scanResults.firstOrNull { - it.BSSID == conInfo.bssid - }?.level ?: -1 + if (conInfo.networkId != -1 || conInfo.linkSpeed != -1) { + rssi = conInfo.rssi + } } var signalStrength = -1 - if (lastScanStrength != -1) { - signalStrength = WifiManager.calculateSignalLevel(lastScanStrength, 4) + if (rssi != -1) { + signalStrength = WifiManager.calculateSignalLevel(rssi, 4) } val icon = "mdi:wifi-strength-" + when (signalStrength) { @@ -357,7 +364,7 @@ class NetworkSensorManager : SensorManager { context.applicationContext.getSystemService()!! val conInfo = wifiManager.connectionInfo - frequency = if (conInfo.networkId == -1) { + frequency = if (conInfo.networkId == -1 && conInfo.linkSpeed == -1) { 0 } else { conInfo.frequency @@ -377,21 +384,21 @@ class NetworkSensorManager : SensorManager { if (!isEnabled(context, wifiSignalStrength.id)) return - var lastScanStrength = -1 + var rssi = -1 if (checkPermission(context, wifiSignalStrength.id)) { val wifiManager = context.applicationContext.getSystemService()!! val conInfo = wifiManager.connectionInfo - lastScanStrength = wifiManager.scanResults.firstOrNull { - it.BSSID == conInfo.bssid - }?.level ?: -1 + if (conInfo.networkId != -1 || conInfo.linkSpeed != -1) { + rssi = conInfo.rssi + } } var signalStrength = -1 - if (lastScanStrength != -1) { - signalStrength = WifiManager.calculateSignalLevel(lastScanStrength, 4) + if (rssi != -1) { + signalStrength = WifiManager.calculateSignalLevel(rssi, 4) } val icon = "mdi:wifi-strength-" + when (signalStrength) { @@ -403,7 +410,7 @@ class NetworkSensorManager : SensorManager { onSensorUpdated( context, wifiSignalStrength, - lastScanStrength, + rssi, icon, mapOf() )