Compare commits

...

2 Commits

Author SHA1 Message Date
lazarosfs
6c79bbb730 honor autoreconnect
We should also honor autoreconnect option before we auto connect on startup. Pressing Connect button does not check autoreconnect, it just sets autoreconnect and connects.
2017-07-31 18:39:54 +03:00
lazarosfs
b961397597 EnableBtOnConnect
This enables bt on connect with user intervention (adding general preference)
It also tries to connect when starting if bt is enabled
2017-07-26 13:32:32 +03:00
4 changed files with 37 additions and 1 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,37 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
Prefs prefs = getPrefs();
switch (action) {
case ACTION_START:
boolean autoReconnect = getGBPrefs().getAutoReconnect();
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if ((mBluetoothAdapter.isEnabled())&&(autoReconnect == true)) {
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) {
@@ -297,7 +325,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
btDeviceAddress = gbDevice.getAddress();
}
boolean autoReconnect = GBPrefs.AUTO_RECONNECT_DEFAULT;
autoReconnect = GBPrefs.AUTO_RECONNECT_DEFAULT;
if (prefs != null && prefs.getPreferences() != null) {
prefs.getPreferences().edit().putString("last_device_address", btDeviceAddress).apply();
autoReconnect = getGBPrefs().getAutoReconnect();

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"