Skip to content

Commit

Permalink
Breaking Change: Add friendly BT name next to the mac address if we h…
Browse files Browse the repository at this point in the history
…ave it (#2697)

* Breaking Change: Add friendly BT name next to the mac address if we have it

* Only add the name if we have it
  • Loading branch information
dshokouhi committed Jul 22, 2022
1 parent f4eea70 commit d072b90
Showing 1 changed file with 9 additions and 5 deletions.
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.os.Build
import io.homeassistant.companion.android.bluetooth.ble.IBeaconTransmitter
import io.homeassistant.companion.android.bluetooth.ble.TransmitterManager
import io.homeassistant.companion.android.common.bluetooth.BluetoothDevice
import io.homeassistant.companion.android.common.bluetooth.BluetoothUtils
import io.homeassistant.companion.android.common.bluetooth.BluetoothUtils.supportsTransmitter
import io.homeassistant.companion.android.common.sensors.SensorManager
Expand Down Expand Up @@ -40,7 +41,6 @@ class BluetoothSensorManager : SensorManager {
private const val DEFAULT_MEASURED_POWER_AT_1M = -59
private var priorBluetoothStateEnabled = false

// private const val TAG = "BluetoothSM"
private var bleTransmitterDevice = IBeaconTransmitter("", "", "", transmitPowerSetting = "", measuredPowerSetting = 0, advertiseModeSetting = "", transmitting = false, state = "", restartRequired = false)
val bluetoothConnection = SensorManager.BasicSensor(
"bluetooth_connection",
Expand Down Expand Up @@ -136,10 +136,10 @@ class BluetoothSensorManager : SensorManager {
if (checkPermission(context, bluetoothConnection.id)) {

val bluetoothDevices = BluetoothUtils.getBluetoothDevices(context, true)
pairedDevices = bluetoothDevices.filter { b -> b.paired }.map { it.address }
connectedPairedDevices = bluetoothDevices.filter { b -> b.paired && b.connected }.map { it.address }
connectedNotPairedDevices = bluetoothDevices.filter { b -> !b.paired && b.connected }.map { it.address }
totalConnectedDevices = bluetoothDevices.filter { b -> b.connected }.count()
pairedDevices = bluetoothDevices.filter { b -> b.paired }.map { checkNameAddress(it) }
connectedPairedDevices = bluetoothDevices.filter { b -> b.paired && b.connected }.map { checkNameAddress(it) }
connectedNotPairedDevices = bluetoothDevices.filter { b -> !b.paired && b.connected }.map { checkNameAddress(it) }
totalConnectedDevices = bluetoothDevices.count { b -> b.connected }
}
onSensorUpdated(
context,
Expand Down Expand Up @@ -260,4 +260,8 @@ class BluetoothSensorManager : SensorManager {
)
)
}

private fun checkNameAddress(bt: BluetoothDevice): String {
return if (bt.address != bt.name) "${bt.address} (${bt.name})" else bt.address
}
}

0 comments on commit d072b90

Please sign in to comment.