1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-02 03:16:07 +02:00

EnableBtOnConnect

This enables bt on connect with user intervention (adding general preference)
It also tries to connect when starting if bt is enabled
This commit is contained in:
lazarosfs 2017-07-26 13:32:32 +03:00
parent 23d12f7289
commit b961397597
4 changed files with 35 additions and 0 deletions

View File

@ -22,6 +22,7 @@ import android.Manifest;
import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
@ -280,10 +281,36 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
Prefs prefs = getPrefs();
switch (action) {
case ACTION_START:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter.isEnabled()) {
intent.setAction(ACTION_CONNECT); // if bt is enabled go to connect so after reboot we autoconnect wihout prompts
onStartCommand(intent,flags,startId);
break;
}
start();
break;
case ACTION_CONNECT:
start(); // ensure started
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean enableBtOnConnect = GBPrefs.ENABLEBLUETOOTH_ONCONNECT_DEFAULT;
if (prefs != null && prefs.getPreferences() != null) {
enableBtOnConnect = getGBPrefs().getEnableBtOnConnect();
}
if (enableBtOnConnect) { //try to enable bt if user prefs allow it
if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable(); // try to enable bt
try {
Thread.sleep(3000); // wait for bt adapter to really start
} catch (Exception e) {
break;
}
}
}
if (!mBluetoothAdapter.isEnabled()) break; // if still not enabled no point connecting in any case
GBDevice gbDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
String btDeviceAddress = null;
if (gbDevice == null) {

View File

@ -22,6 +22,8 @@ import java.util.Date;
public class GBPrefs {
public static final String PACKAGE_BLACKLIST = "package_blacklist";
public static final String AUTO_RECONNECT = "general_autocreconnect";
public static final String ENABLEBLUETOOTH_ONCONNECT = "general_enablebluetoothonconnect";
public static final boolean ENABLEBLUETOOTH_ONCONNECT_DEFAULT = false;
private static final String AUTO_START = "general_autostartonboot";
private static final boolean AUTO_START_DEFAULT = true;
public static boolean AUTO_RECONNECT_DEFAULT = true;
@ -44,6 +46,7 @@ public class GBPrefs {
return mPrefs.getBoolean(AUTO_START, AUTO_START_DEFAULT);
}
public boolean getEnableBtOnConnect() { return mPrefs.getBoolean(ENABLEBLUETOOTH_ONCONNECT, ENABLEBLUETOOTH_ONCONNECT_DEFAULT); }
public String getUserName() {
return mPrefs.getString(USER_NAME, USER_NAME_DEFAULT);
}

View File

@ -61,6 +61,7 @@
<string name="pref_title_general_autoconnectonbluetooth">Connect to device when Bluetooth turned on</string>
<string name="pref_title_general_autostartonboot">Start automatically</string>
<string name="pref_title_general_autocreonnect">Reconnect automatically</string>
<string name="pref_title_general_enablebluetoothonconnect">Try to enable Bluetooth on connect</string>
<string name="pref_title_audo_player">Preferred Audioplayer</string>
<string name="pref_default">Default</string>

View File

@ -7,6 +7,10 @@
android:defaultValue="true"
android:key="general_autostartonboot"
android:title="@string/pref_title_general_autostartonboot" />
<CheckBoxPreference
android:defaultValue="false"
android:key="general_enablebluetoothonconnect"
android:title="@string/pref_title_general_enablebluetoothonconnect" />
<CheckBoxPreference
android:defaultValue="false"
android:key="general_autoconnectonbluetooth"