Split up bluetooth state into its own sensor, part 1 (#901)

* Split up bluetooth state into its own sensor, part 1

* Fix typo and add note to remove attribute

* Lint

Co-authored-by: Justin Bassett <bassett.justint@gmail.com>
This commit is contained in:
Daniel Shokouhi 2020-09-08 13:18:18 -07:00 committed by GitHub
parent 10c5769349
commit f771048f73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 7 deletions

View file

@ -18,6 +18,12 @@ class BluetoothSensorManager : SensorManager {
R.string.sensor_description_bluetooth_connection,
unitOfMeasurement = "connection(s)"
)
val bluetoothState = SensorManager.BasicSensor(
"bluetooth_state",
"binary_sensor",
R.string.basic_sensor_name_bluetooth_state,
R.string.sensor_description_bluetooth_state
)
}
override val enabledByDefault: Boolean
@ -25,7 +31,7 @@ class BluetoothSensorManager : SensorManager {
override val name: Int
get() = R.string.sensor_name_bluetooth
override val availableSensors: List<SensorManager.BasicSensor>
get() = listOf(bluetoothConnection)
get() = listOf(bluetoothConnection, bluetoothState)
override fun requiredPermissions(): Array<String> {
return arrayOf(Manifest.permission.BLUETOOTH)
@ -35,6 +41,7 @@ class BluetoothSensorManager : SensorManager {
context: Context
) {
updateBluetoothConnectionSensor(context)
updateBluetoothState(context)
}
private fun updateBluetoothConnectionSensor(context: Context) {
@ -91,12 +98,39 @@ class BluetoothSensorManager : SensorManager {
mapOf(
"connected_paired_devices" to connectedPairedDevices,
"connected_not_paired_devices" to connectedNotPairedDevices,
"is_bt_on" to isBtOn,
"is_bt_on" to isBtOn, // Remove after next release
"paired_devices" to bondedString
)
)
}
private fun updateBluetoothState(context: Context) {
if (!isEnabled(context, bluetoothState.id))
return
var isBtOn = false
if (checkPermission(context)) {
val bluetoothManager =
(context.applicationContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager)
if (bluetoothManager.adapter != null) {
val adapter = bluetoothManager.adapter
isBtOn = adapter.isEnabled
}
}
val icon = if (isBtOn) "mdi:bluetooth" else "mdi:bluetooth-off"
onSensorUpdated(
context,
bluetoothState,
isBtOn,
icon,
mapOf()
)
}
private fun isConnected(device: BluetoothDevice): Boolean {
return try {
val m: Method = device.javaClass.getMethod("isConnected")

View file

@ -73,12 +73,19 @@ class SensorReceiver : BroadcastReceiver() {
}
}
"android.bluetooth.device.action.ACL_CONNECTED",
"android.bluetooth.device.action.ACL_DISCONNECTED",
BluetoothAdapter.ACTION_STATE_CHANGED -> {
"android.bluetooth.device.action.ACL_DISCONNECTED" -> {
val sensorDao = AppDatabase.getInstance(context).sensorDao()
val sensor = sensorDao.get(BluetoothSensorManager.bluetoothConnection.id)
if (sensor?.enabled != true) {
Log.d(TAG, "Bluetooth Sensor disabled, skipping sensors update")
val sensorBtConn = sensorDao.get(BluetoothSensorManager.bluetoothConnection.id)
if (sensorBtConn?.enabled != true) {
Log.d(TAG, "Bluetooth Connection Sensor disabled, skipping sensors update")
return
}
}
BluetoothAdapter.ACTION_STATE_CHANGED -> {
val sensorDao = AppDatabase.getInstance(context).sensorDao()
val sensorBtState = sensorDao.get(BluetoothSensorManager.bluetoothState.id)
if (sensorBtState?.enabled != true) {
Log.d(TAG, "Bluetooth State Sensor disabled, skipping sensors update")
return
}
}

View file

@ -157,6 +157,7 @@ like to connect to:</string>
<string name="sensor_description_interactive">Whether or not the device is in an interactive state</string>
<string name="sensor_description_doze">Whether or not the device is in Doze mode</string>
<string name="sensor_description_power_save">Whether or not the device is in Power Save mode</string>
<string name="sensor_description_bluetooth_state">Whether or not bluetooth is enabled on the device</string>
<string name="sensor_description_charging">Whether or not the device is actively charging</string>
<string name="sensor_description_charger_type">The type of charger plugged into the device currently</string>
<string name="sensor_description_battery_health">The health of the battery</string>
@ -179,6 +180,7 @@ like to connect to:</string>
<string name="basic_sensor_name_phone">Phone State</string>
<string name="basic_sensor_name_sim1">SIM 1</string>
<string name="basic_sensor_name_sim2">SIM 2</string>
<string name="basic_sensor_name_bluetooth_state">Bluetooth State</string>
<string name="basic_sensor_name_charging">Is Charging</string>
<string name="basic_sensor_name_charger_type">Charger Type</string>
<string name="basic_sensor_name_battery_health">Battery Health</string>