1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-15 04:07:32 +01:00

Bluetooth Intent API: Add disconnect action (#4090)

This commit is contained in:
José Rebelo 2024-09-09 00:33:13 +01:00
parent b1c064d9ca
commit 6843271ac2

View File

@ -296,6 +296,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
private boolean reconnectViaScan = GBPrefs.RECONNECT_SCAN_DEFAULT; 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_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_CONNECTED = "nodomain.freeyourgadget.gadgetbridge.BLUETOOTH_CONNECTED";
private final String API_LEGACY_ACTION_DEVICE_SCANNED = "nodomain.freeyourgadget.gadgetbridge.BLUETOOTH_SCANNED"; private final String API_LEGACY_ACTION_DEVICE_SCANNED = "nodomain.freeyourgadget.gadgetbridge.BLUETOOTH_SCANNED";
@ -317,8 +318,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
BroadcastReceiver bluetoothCommandReceiver = new BroadcastReceiver() { BroadcastReceiver bluetoothCommandReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
switch (intent.getAction()){
case API_LEGACY_COMMAND_BLUETOOTH_CONNECT:
if (!allowBluetoothIntentApi){ if (!allowBluetoothIntentApi){
GB.log("Connection API not allowed in settings", GB.ERROR, null); GB.log("Connection API not allowed in settings", GB.ERROR, null);
return; return;
@ -328,21 +327,17 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
GB.log("no extras provided in Intent", GB.ERROR, null); GB.log("no extras provided in Intent", GB.ERROR, null);
return; 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", ""); String address = extras.getString("EXTRA_DEVICE_ADDRESS", "");
if (address.isEmpty()){ if (address.isEmpty()){
GB.log("no bluetooth address provided in Intent", GB.ERROR, null); GB.log("no bluetooth address provided in Intent", GB.ERROR, null);
return; return;
} }
if(isDeviceConnected(address)){ GBDevice targetDevice = GBApplication.app()
GB.log(String.format("device %s already connected", address), GB.INFO, null);
sendDeviceConnectedBroadcast(address);
return;
}
List<GBDevice> devices = GBApplication.app().getDeviceManager().getDevices();
GBDevice targetDevice = GBApplication
.app()
.getDeviceManager() .getDeviceManager()
.getDeviceByAddress(address); .getDeviceByAddress(address);
@ -351,12 +346,23 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
return; return;
} }
switch (action) {
case API_LEGACY_COMMAND_BLUETOOTH_CONNECT:
if (isDeviceConnected(address)){
GB.log(String.format("device %s already connected", address), GB.INFO, null);
sendDeviceConnectedBroadcast(address);
return;
}
GB.log(String.format("connecting to %s", address), GB.INFO, null); GB.log(String.format("connecting to %s", address), GB.INFO, null);
GBApplication GBApplication.deviceService(targetDevice).connect();
.deviceService(targetDevice) break;
.connect(); case API_LEGACY_COMMAND_BLUETOOTH_DISCONNECT:
GB.log(String.format("disconnecting from %s", address), GB.INFO, null);
GBApplication.deviceService(targetDevice).disconnect();
break; break;
} }
} }
@ -512,6 +518,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
IntentFilter bluetoothCommandFilter = new IntentFilter(); IntentFilter bluetoothCommandFilter = new IntentFilter();
bluetoothCommandFilter.addAction(API_LEGACY_COMMAND_BLUETOOTH_CONNECT); bluetoothCommandFilter.addAction(API_LEGACY_COMMAND_BLUETOOTH_CONNECT);
bluetoothCommandFilter.addAction(API_LEGACY_COMMAND_BLUETOOTH_DISCONNECT);
ContextCompat.registerReceiver(this, bluetoothCommandReceiver, bluetoothCommandFilter, ContextCompat.RECEIVER_EXPORTED); ContextCompat.registerReceiver(this, bluetoothCommandReceiver, bluetoothCommandFilter, ContextCompat.RECEIVER_EXPORTED);
final IntentFilter deviceSettingsIntentFilter = new IntentFilter(); final IntentFilter deviceSettingsIntentFilter = new IntentFilter();