From 6843271ac27c5fa9a211a678a6651ab1c88dedcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Mon, 9 Sep 2024 00:33:13 +0100 Subject: [PATCH] Bluetooth Intent API: Add disconnect action (#4090) --- .../service/DeviceCommunicationService.java | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 472e075ba..5d0987906 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -296,6 +296,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere private boolean reconnectViaScan = GBPrefs.RECONNECT_SCAN_DEFAULT; private final String API_LEGACY_COMMAND_BLUETOOTH_CONNECT = "nodomain.freeyourgadget.gadgetbridge.BLUETOOTH_CONNECT"; + private final String API_LEGACY_COMMAND_BLUETOOTH_DISCONNECT = "nodomain.freeyourgadget.gadgetbridge.BLUETOOTH_DISCONNECT"; private final String API_LEGACY_ACTION_DEVICE_CONNECTED = "nodomain.freeyourgadget.gadgetbridge.BLUETOOTH_CONNECTED"; private final String API_LEGACY_ACTION_DEVICE_SCANNED = "nodomain.freeyourgadget.gadgetbridge.BLUETOOTH_SCANNED"; @@ -317,46 +318,51 @@ public class DeviceCommunicationService extends Service implements SharedPrefere BroadcastReceiver bluetoothCommandReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - switch (intent.getAction()){ + if (!allowBluetoothIntentApi){ + GB.log("Connection API not allowed in settings", GB.ERROR, null); + return; + } + Bundle extras = intent.getExtras(); + if (extras == null) { + GB.log("no extras provided in Intent", GB.ERROR, null); + return; + } + final String action = intent.getAction(); + if (action == null) { + GB.log("Action for bluetooth command is null", GB.ERROR, null); + return; + } + String address = extras.getString("EXTRA_DEVICE_ADDRESS", ""); + if (address.isEmpty()){ + GB.log("no bluetooth address provided in Intent", GB.ERROR, null); + return; + } + GBDevice targetDevice = GBApplication.app() + .getDeviceManager() + .getDeviceByAddress(address); + + if (targetDevice == null){ + GB.log(String.format("device %s not registered", address), GB.ERROR, null); + return; + } + + switch (action) { case API_LEGACY_COMMAND_BLUETOOTH_CONNECT: - if(!allowBluetoothIntentApi){ - GB.log("Connection API not allowed in settings", GB.ERROR, null); - return; - } - Bundle extras = intent.getExtras(); - if(extras == null){ - GB.log("no extras provided in Intent", GB.ERROR, null); - return; - } - String address = extras.getString("EXTRA_DEVICE_ADDRESS", ""); - if(address.isEmpty()){ - GB.log("no bluetooth address provided in Intent", GB.ERROR, null); - return; - } - if(isDeviceConnected(address)){ + if (isDeviceConnected(address)){ GB.log(String.format("device %s already connected", address), GB.INFO, null); sendDeviceConnectedBroadcast(address); return; } - List devices = GBApplication.app().getDeviceManager().getDevices(); - GBDevice targetDevice = GBApplication - .app() - .getDeviceManager() - .getDeviceByAddress(address); - - if(targetDevice == null){ - GB.log(String.format("device %s not registered", address), GB.ERROR, null); - return; - } - GB.log(String.format("connecting to %s", address), GB.INFO, null); - GBApplication - .deviceService(targetDevice) - .connect(); + GBApplication.deviceService(targetDevice).connect(); + break; + case API_LEGACY_COMMAND_BLUETOOTH_DISCONNECT: + GB.log(String.format("disconnecting from %s", address), GB.INFO, null); + GBApplication.deviceService(targetDevice).disconnect(); break; } } @@ -512,6 +518,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere IntentFilter bluetoothCommandFilter = new IntentFilter(); bluetoothCommandFilter.addAction(API_LEGACY_COMMAND_BLUETOOTH_CONNECT); + bluetoothCommandFilter.addAction(API_LEGACY_COMMAND_BLUETOOTH_DISCONNECT); ContextCompat.registerReceiver(this, bluetoothCommandReceiver, bluetoothCommandFilter, ContextCompat.RECEIVER_EXPORTED); final IntentFilter deviceSettingsIntentFilter = new IntentFilter();