mirror of
https://github.com/home-assistant/android
synced 2024-09-06 17:23:32 +00:00
Use a foreground service for reliable beacon scanning in background (#3369)
* Use a foreground service for reliable beacon scanning in background * Clean up * Add action to stop beacon scanning * Match sensor name * Use existing Disable string for action * ktlint
This commit is contained in:
parent
f5d2b978ad
commit
c3fe6b3b6e
|
@ -1,6 +1,18 @@
|
|||
package io.homeassistant.companion.android.common.bluetooth.ble
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.content.getSystemService
|
||||
import io.homeassistant.companion.android.common.BuildConfig
|
||||
import io.homeassistant.companion.android.common.R
|
||||
import io.homeassistant.companion.android.common.sensors.SensorReceiverBase
|
||||
import io.homeassistant.companion.android.common.sensors.SensorUpdateReceiver
|
||||
import io.homeassistant.companion.android.common.util.beaconMonitorChannel
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
|
@ -33,6 +45,10 @@ class MonitoringManager {
|
|||
if (!this::beaconManager.isInitialized) {
|
||||
beaconManager = BeaconManager.getInstanceForApplication(context)
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
BeaconManager.setDebug(true)
|
||||
}
|
||||
|
||||
// find iBeacons
|
||||
beaconManager.beaconParsers.add(
|
||||
BeaconParser()
|
||||
|
@ -58,6 +74,20 @@ class MonitoringManager {
|
|||
}
|
||||
}
|
||||
|
||||
val builder = NotificationCompat.Builder(context, beaconMonitorChannel)
|
||||
builder.setSmallIcon(R.drawable.ic_stat_ic_notification)
|
||||
builder.setContentTitle(context.getString(R.string.beacon_scanning))
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val channel = NotificationChannel(beaconMonitorChannel, context.getString(R.string.beacon_scanning), NotificationManager.IMPORTANCE_LOW)
|
||||
val notifManager = context.getSystemService<NotificationManager>()!!
|
||||
notifManager.createNotificationChannel(channel)
|
||||
}
|
||||
val stopScanningIntent = Intent(context, SensorUpdateReceiver::class.java)
|
||||
stopScanningIntent.action = SensorReceiverBase.ACTION_STOP_BEACON_SCANNING
|
||||
val stopScanningPendingIntent = PendingIntent.getBroadcast(context, 0, stopScanningIntent, PendingIntent.FLAG_MUTABLE)
|
||||
builder.addAction(0, context.getString(R.string.disable), stopScanningPendingIntent)
|
||||
beaconManager.enableForegroundServiceScanning(builder.build(), 444)
|
||||
beaconManager.setEnableScheduledScanJobs(false)
|
||||
beaconManager.startRangingBeacons(region)
|
||||
haMonitor.sensorManager.updateBeaconMonitoringSensor(context)
|
||||
}
|
||||
|
@ -65,6 +95,7 @@ class MonitoringManager {
|
|||
fun stopMonitoring(context: Context, haMonitor: IBeaconMonitor) {
|
||||
if (isMonitoring()) {
|
||||
beaconManager.stopRangingBeacons(region)
|
||||
beaconManager.disableForegroundServiceScanning()
|
||||
haMonitor.sensorManager.updateBeaconMonitoringSensor(context)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -375,6 +375,7 @@ class BluetoothSensorManager : SensorManager {
|
|||
|
||||
fun updateBeaconMonitoringSensor(context: Context) {
|
||||
if (!isEnabled(context, beaconMonitor)) {
|
||||
monitoringManager.stopMonitoring(context, beaconMonitoringDevice)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,7 @@ abstract class SensorReceiverBase : BroadcastReceiver() {
|
|||
companion object {
|
||||
const val ACTION_UPDATE_SENSOR = "io.homeassistant.companion.android.UPDATE_SENSOR"
|
||||
const val ACTION_UPDATE_SENSORS = "io.homeassistant.companion.android.UPDATE_SENSORS"
|
||||
const val ACTION_STOP_BEACON_SCANNING = "io.homeassistant.companion.android.STOP_BEACON_SCANNING"
|
||||
const val EXTRA_SENSOR_ID = "sensorId"
|
||||
|
||||
fun shouldDoFastUpdates(context: Context): Boolean {
|
||||
|
@ -103,6 +104,11 @@ abstract class SensorReceiverBase : BroadcastReceiver() {
|
|||
}
|
||||
}
|
||||
|
||||
if (intent.action == ACTION_STOP_BEACON_SCANNING) {
|
||||
BluetoothSensorManager.enableDisableBeaconMonitor(context, false)
|
||||
return
|
||||
}
|
||||
|
||||
if (isSensorEnabled(LastUpdateManager.lastUpdate.id)) {
|
||||
LastUpdateManager().sendLastUpdate(context, intent.action)
|
||||
val allSettings = sensorDao.getSettings(LastUpdateManager.lastUpdate.id)
|
||||
|
|
|
@ -9,6 +9,7 @@ const val databaseChannel = "App Database"
|
|||
const val locationDisabledChannel = "Location disabled"
|
||||
const val downloadsChannel = "downloads"
|
||||
const val generalChannel = "general"
|
||||
const val beaconMonitorChannel = "beacon"
|
||||
|
||||
val appCreatedChannels = listOf(
|
||||
sensorWorkerChannel,
|
||||
|
@ -19,5 +20,6 @@ val appCreatedChannels = listOf(
|
|||
databaseChannel,
|
||||
locationDisabledChannel,
|
||||
downloadsChannel,
|
||||
generalChannel
|
||||
generalChannel,
|
||||
beaconMonitorChannel
|
||||
)
|
||||
|
|
|
@ -502,7 +502,7 @@
|
|||
<string name="sensor_description_battery_state">The current charging state of the battery</string>
|
||||
<string name="sensor_description_battery_temperature">The current battery temperature</string>
|
||||
<string name="sensor_description_bluetooth_ble_emitter">Send BLE iBeacon with configured interval, used to track presence around house, e.g. together with roomassistant, esp32-mqtt-room or espresence projects.\n\nWarning: this can affect battery life, particularly if the \"Transmitter power\" setting is set to High or \"Advertise Mode\" is set to Low latency.\n\nSettings allow for specifying:\n- \"UUID\" (standard UUID format), \"Major\" and \"Minor\" (should be 0 - 65535), to tailor identifiers and groups\n- \"Transmitter Power\" and \"Advertise Mode\" to help to preserve battery life (use lowest values if possible)\n - \"Measured Power\" to specify power measured at 1m (initial default -59)\n\nNote:\nAdditionally a separate setting exists (\"Enable Transmitter\") to stop or start transmitting.</string>
|
||||
<string name="sensor_description_bluetooth_ble_beacon_monitor">Scans for iBeacons and shows the IDs of nearby beacons and their distance in meters.\n\nWarning: this can affect battery life, especially with a short \"Scan Interval\".\n\nSettings allow for specifying:\n- \"Filter Iterations\" (should be 1 - 100, default: 10), higher values will result in more stable measurements but also less responsiveness.\n- \"Filter RSSI Multiplier\" (should be 1.0 - 2.0, default: 1.05), can be used to archive more stable measurements when beacons are farther away. This will also affect responsiveness.\n- \"Scan Interval\" (default: 500) milliseconds between scans. Shorter intervals will drain the battery more quickly.\n- \"Scan Period\" (default: 1100) milliseconds to scan for beacons. Most beacons will send a signal every second so this value should be at least 1100ms.\n- \"UUID Filter\" allows to restrict the reported beacons by including (or excluding) those with the selected UUIDs.\n- \"Exclude selected UUIDs\", if false (default) only the beacons with the selected UUIDs are reported. If true all beacons except the selected ones are reported. Not available when \"UUID Filter\" is empty.\n\nNote:\nAdditionally a separate setting exists (\"Enable Beacon Monitor\") to stop or start scanning.</string>
|
||||
<string name="sensor_description_bluetooth_ble_beacon_monitor">Scans for iBeacons and shows the IDs of nearby beacons and their distance in meters. A notification will be shown on the device when scanning is actively running.\n\nWarning: this can affect battery life, especially with a short \"Scan Interval\".\n\nSettings allow for specifying:\n- \"Filter Iterations\" (should be 1 - 100, default: 10), higher values will result in more stable measurements but also less responsiveness.\n- \"Filter RSSI Multiplier\" (should be 1.0 - 2.0, default: 1.05), can be used to archive more stable measurements when beacons are farther away. This will also affect responsiveness.\n- \"Scan Interval\" (default: 500) milliseconds between scans. Shorter intervals will drain the battery more quickly.\n- \"Scan Period\" (default: 1100) milliseconds to scan for beacons. Most beacons will send a signal every second so this value should be at least 1100ms.\n- \"UUID Filter\" allows to restrict the reported beacons by including (or excluding) those with the selected UUIDs.\n- \"Exclude selected UUIDs\", if false (default) only the beacons with the selected UUIDs are reported. If true all beacons except the selected ones are reported. Not available when \"UUID Filter\" is empty.\n\nNote:\nAdditionally a separate setting exists (\"Enable Beacon Monitor\") to stop or start scanning.</string>
|
||||
<string name="sensor_description_bluetooth_connection">Information about currently connected Bluetooth devices</string>
|
||||
<string name="sensor_description_bluetooth_state">Whether Bluetooth is enabled on the device</string>
|
||||
<string name="sensor_description_charger_type">The type of charger plugged into the device currently</string>
|
||||
|
@ -1041,4 +1041,5 @@
|
|||
<string name="not_registered">Please launch the Home Assistant app and login before you can use the assist feature.</string>
|
||||
<string name="ha_assist">HA: Assist</string>
|
||||
<string name="only_favorites">Only Show Favorites</string>
|
||||
<string name="beacon_scanning">Beacon Monitor Scanning</string>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue