1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-26 17:47:34 +01:00

Allow filtering of BLE characteristics

Use a list for filter instead of string

Make text clearer
This commit is contained in:
LLan 2025-01-13 02:17:21 +01:00
parent 69e8b678c2
commit adef157275
5 changed files with 28 additions and 2 deletions

View File

@ -592,6 +592,7 @@ public class DeviceSettingsPreferenceConst {
public static final String PREFS_KEY_DEVICE_BLE_API_DEVICE_STATE = "prefs_device_ble_api_state";
public static final String PREFS_KEY_DEVICE_BLE_API_DEVICE_READ_WRITE = "prefs_device_ble_api_characteristic_read_write";
public static final String PREFS_KEY_DEVICE_BLE_API_DEVICE_NOTIFY = "prefs_device_ble_api_characteristic_notify";
public static final String PREFS_KEY_DEVICE_BLE_API_CHARACTERISTIC = "prefs_device_ble_api_filter_char";
public static final String PREFS_KEY_DEVICE_BLE_API_PACKAGE = "prefs_device_ble_api_package";
public static final String PREF_BATTERY_DISCHARGE_INTERVAL1_WATT = "battery_discharge_interval1_watt";

View File

@ -879,6 +879,7 @@ public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment i
addPreferenceHandlerFor(PREFS_KEY_DEVICE_BLE_API_DEVICE_STATE);
addPreferenceHandlerFor(PREFS_KEY_DEVICE_BLE_API_DEVICE_READ_WRITE);
addPreferenceHandlerFor(PREFS_KEY_DEVICE_BLE_API_DEVICE_NOTIFY);
addPreferenceHandlerFor(PREFS_KEY_DEVICE_BLE_API_CHARACTERISTIC);
addPreferenceHandlerFor(PREFS_KEY_DEVICE_BLE_API_PACKAGE);
addPreferenceHandlerFor("lock");

View File

@ -1,5 +1,6 @@
package nodomain.freeyourgadget.gadgetbridge.service.btle;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREFS_KEY_DEVICE_BLE_API_CHARACTERISTIC;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREFS_KEY_DEVICE_BLE_API_DEVICE_NOTIFY;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREFS_KEY_DEVICE_BLE_API_DEVICE_READ_WRITE;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREFS_KEY_DEVICE_BLE_API_DEVICE_STATE;
@ -20,8 +21,11 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.UUID;
import java.util.List;
import java.util.stream.Collectors;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
@ -39,6 +43,7 @@ public class BleIntentApi {
private boolean intentApiEnabledDeviceState = false;
private boolean intentApiEnabledReadWrite= false;
private boolean intentApiEnabledNotifications= false;
private List<String> intentApiCharacteristicFilter = new ArrayList<>();
private String intentApiPackage = "";
private boolean intentApiCharacteristicReceiverRegistered = false;
private boolean intentApiDeviceStateReceiverRegistered = false;
@ -122,6 +127,9 @@ public class BleIntentApi {
if(!intentApiEnabledNotifications) {
return;
}
if(!intentApiCharacteristicFilter.isEmpty() && !intentApiCharacteristicFilter.contains(characteristic.getUuid().toString())) {
return;
}
Intent intent = getBleApiIntent(BLE_API_EVENT_CHARACTERISTIC_CHANGED);
if(!StringUtils.isNullOrEmpty(intentApiPackage)) {
intent.setPackage(intentApiPackage);
@ -135,7 +143,14 @@ public class BleIntentApi {
public void initializeDevice(TransactionBuilder builder) {
if(intentApiEnabledNotifications) {
for (BluetoothGattCharacteristic characteristic : characteristics.values()) {
builder.notify(characteristic, true);
if (intentApiCharacteristicFilter.isEmpty()) {
builder.notify(characteristic, true);
logger.info("Subscribed to {}", characteristic.getUuid());
}
else if(intentApiCharacteristicFilter.contains(characteristic.getUuid().toString())) {
builder.notify(characteristic, true);
logger.info("Subscribed to filtered {}", characteristic.getUuid());
}
}
}
}
@ -204,6 +219,8 @@ public class BleIntentApi {
this.intentApiEnabledReadWrite = devicePrefs.getBoolean(PREFS_KEY_DEVICE_BLE_API_DEVICE_READ_WRITE, false);
this.intentApiEnabledNotifications = devicePrefs.getBoolean(PREFS_KEY_DEVICE_BLE_API_DEVICE_NOTIFY, false);
this.intentApiEnabledDeviceState = devicePrefs.getBoolean(PREFS_KEY_DEVICE_BLE_API_DEVICE_STATE, false);
// Also trim whitespace from the list of UUIDs
this.intentApiCharacteristicFilter = devicePrefs.getList(PREFS_KEY_DEVICE_BLE_API_CHARACTERISTIC, Collections.emptyList()).stream().map(String::trim).collect(Collectors.toList());
this.intentApiPackage = devicePrefs.getString(PREFS_KEY_DEVICE_BLE_API_PACKAGE, "");
registerBleApiCharacteristicReceivers(this.intentApiEnabledReadWrite);

View File

@ -3448,7 +3448,9 @@
<string name="prefs_title_gatt_client_allow_gatt_interactions">Allow GATT interaction through BLE Intent API</string>
<string name="prefs_summary_gatt_client_allow_gatt_interactions">Allow to send BLE characteristic read/write and connect commands</string>
<string name="prefs_summary_gatt_client_device_state_updates">Receive BLE connection state changes via Intents</string>
<string name="prefs_title_gatt_client_filter_char">BLE Characteristics</string>
<string name="prefs_title_gatt_client_api_package">BLE API package</string>
<string name="prefs_summary_gatt_client_filter_char">Filter for specific BLE characteristics, split by a comma</string>
<string name="prefs_summary_gatt_client_api_package">Restrict BLE Intent API communication to this package</string>
<string name="prefs_title_ble_intent_api">BLE Intent API</string>
<string name="activity_db_management_backup_restore_label">Backup and Restore</string>

View File

@ -19,6 +19,11 @@
android:summary="@string/prefs_summary_gatt_client_notification_intents"
android:defaultValue="false" />
<EditTextPreference
android:key="prefs_device_ble_api_filter_char"
android:title="@string/prefs_title_gatt_client_filter_char"
android:summary="@string/prefs_summary_gatt_client_filter_char" />
<EditTextPreference
android:key="prefs_device_ble_api_package"
android:title="@string/prefs_title_gatt_client_api_package"