Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to transmit BLE on Home SSID only #4277

Merged
merged 5 commits into from Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -10,6 +10,7 @@ data class IBeaconTransmitter(
var transmitPowerSetting: String,
var measuredPowerSetting: Int,
var advertiseModeSetting: String,
var onlyTransmitOnHomeWifiSetting: Boolean = false,
var restartRequired: Boolean = false,
val manufacturer: Int = 0x004c,
val beaconLayout: String = "m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"
Expand Down
Expand Up @@ -32,6 +32,7 @@ class BluetoothSensorManager : SensorManager {
const val SETTING_BLE_ID3 = "ble_minor"
const val SETTING_BLE_TRANSMIT_POWER = "ble_transmit_power"
const val SETTING_BLE_ADVERTISE_MODE = "ble_advertise_mode"
const val SETTING_BLE_HOME_WIFI_ONLY = "ble_home_wifi_only"
private const val SETTING_BLE_TRANSMIT_ENABLED = "ble_transmit_enabled"
const val SETTING_BLE_MEASURED_POWER = "ble_measured_power_at_1m"
const val BLE_ADVERTISE_LOW_LATENCY = "lowLatency"
Expand Down Expand Up @@ -61,7 +62,7 @@ class BluetoothSensorManager : SensorManager {
private const val DEFAULT_BEACON_MONITOR_FILTER_ITERATIONS = "10"
private const val DEFAULT_BEACON_MONITOR_FILTER_RSSI_MULTIPLIER = "1.05"

private var bleTransmitterDevice = IBeaconTransmitter("", "", "", transmitPowerSetting = "", measuredPowerSetting = 0, advertiseModeSetting = "", transmitting = false, state = "", restartRequired = false)
private var bleTransmitterDevice = IBeaconTransmitter("", "", "", transmitPowerSetting = "", measuredPowerSetting = 0, advertiseModeSetting = "", onlyTransmitOnHomeWifiSetting = false, transmitting = false, state = "", restartRequired = false)
private var beaconMonitoringDevice = IBeaconMonitor()
val bluetoothConnection = SensorManager.BasicSensor(
"bluetooth_connection",
Expand Down Expand Up @@ -247,6 +248,8 @@ class BluetoothSensorManager : SensorManager {
)
}

private fun isPermittedOnThisWifiNetwork(context: Context) = serverManager(context).defaultServers.any { it.connection.isHomeWifiSsid() }

private fun updateBLEDevice(context: Context) {
val transmitActive = getToggleSetting(context, bleTransmitter, SETTING_BLE_TRANSMIT_ENABLED, default = true)
val uuid = getSetting(context, bleTransmitter, SETTING_BLE_ID1, SensorSettingType.STRING, default = UUID.randomUUID().toString())
Expand Down Expand Up @@ -278,12 +281,14 @@ class BluetoothSensorManager : SensorManager {
),
default = DEFAULT_BLE_ADVERTISE_MODE
)
val homeWifiOnly = getToggleSetting(context, bleTransmitter, SETTING_BLE_HOME_WIFI_ONLY, default = false)

bleTransmitterDevice.restartRequired = false
if (bleTransmitterDevice.uuid != uuid || bleTransmitterDevice.major != major ||
bleTransmitterDevice.minor != minor || bleTransmitterDevice.transmitPowerSetting != transmitPower ||
bleTransmitterDevice.advertiseModeSetting != advertiseMode || bleTransmitterDevice.transmitRequested != transmitActive ||
bleTransmitterDevice.measuredPowerSetting != measuredPower || priorBluetoothStateEnabled != isBtOn(context)
bleTransmitterDevice.measuredPowerSetting != measuredPower || priorBluetoothStateEnabled != isBtOn(context) ||
bleTransmitterDevice.onlyTransmitOnHomeWifiSetting != homeWifiOnly
) {
bleTransmitterDevice.restartRequired = true
}
Expand All @@ -296,6 +301,7 @@ class BluetoothSensorManager : SensorManager {
bleTransmitterDevice.transmitPowerSetting = transmitPower
bleTransmitterDevice.measuredPowerSetting = measuredPower
bleTransmitterDevice.advertiseModeSetting = advertiseMode
bleTransmitterDevice.onlyTransmitOnHomeWifiSetting = homeWifiOnly
bleTransmitterDevice.transmitRequested = transmitActive
}

Expand Down Expand Up @@ -342,15 +348,17 @@ class BluetoothSensorManager : SensorManager {
TransmitterManager.stopTransmitting(bleTransmitterDevice)
return
}
// transmit when BT is on, if we are not already transmitting, or details have changed
// transmit when BT is on, if we are not already transmitting, or details have changed, and we're permitted on this wifi network
if (isBtOn(context)) {
if (bleTransmitterDevice.transmitRequested && (!bleTransmitterDevice.transmitting || bleTransmitterDevice.restartRequired)) {
if (bleTransmitterDevice.transmitRequested && (!bleTransmitterDevice.transmitting || bleTransmitterDevice.restartRequired) &&
(!bleTransmitterDevice.onlyTransmitOnHomeWifiSetting || isPermittedOnThisWifiNetwork(context))
) {
TransmitterManager.startTransmitting(context, bleTransmitterDevice)
}
}

// BT off, or TransmitToggled off, stop transmitting if we have been
if (!isBtOn(context) || !bleTransmitterDevice.transmitRequested) {
// BT off, or TransmitToggled off, or not permitted on this network - stop transmitting if we have been
if (!isBtOn(context) || !bleTransmitterDevice.transmitRequested || (bleTransmitterDevice.onlyTransmitOnHomeWifiSetting && !isPermittedOnThisWifiNetwork(context))) {
TransmitterManager.stopTransmitting(bleTransmitterDevice)
}

Expand Down
3 changes: 2 additions & 1 deletion common/src/main/res/values/strings.xml
Expand Up @@ -573,7 +573,7 @@
<string name="sensor_description_battery_level">The current battery level of the device</string>
<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. The default Minor value 40004 has the special meaning of forcing the beacon to be recognized by the iBeacon Tracker integration\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_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. The default Minor value 40004 has the special meaning of forcing the beacon to be recognized by the iBeacon Tracker integration\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\nIt is also possible to set beacons to only be transmitted when connected to a Home Network WiFi SSID, which may be desirable for privacy and battery life.\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. 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>
Expand Down Expand Up @@ -696,6 +696,7 @@
<string name="sensor_setting_ble_measured_power_at_1m_title">Measured power</string>
<string name="sensor_setting_ble_minor_title">Minor</string>
<string name="sensor_setting_ble_transmit_enabled_title">Enable transmitter</string>
<string name="sensor_setting_ble_home_wifi_only_title">Transmit on Home Network WiFi SSID only</string>
<string name="sensor_setting_ble_transmit_power_high_label">High</string>
<string name="sensor_setting_ble_transmit_power_low_label">Low</string>
<string name="sensor_setting_ble_transmit_power_medium_label">Medium</string>
Expand Down