From 57a95233d3129a66d5ea05001afc875e1bb50863 Mon Sep 17 00:00:00 2001 From: ne0fhyk Date: Tue, 27 Jan 2015 22:43:06 -0800 Subject: [PATCH] Fixed connection issues with bluetooth. --- Android/res/values/preferences_keys.xml | 1 + Android/res/values/strings.xml | 2 + .../helpers/BluetoothDevicesActivity.java | 4 +- .../dialogs/ClearBTDialogPreference.java | 13 +++++++ .../android/fragments/SettingsFragment.java | 39 ++++++++++++++++++- .../utils/prefs/DroidPlannerPrefs.java | 8 ++++ 6 files changed, 64 insertions(+), 3 deletions(-) diff --git a/Android/res/values/preferences_keys.xml b/Android/res/values/preferences_keys.xml index 236c658bfa..743843ac8f 100644 --- a/Android/res/values/preferences_keys.xml +++ b/Android/res/values/preferences_keys.xml @@ -19,6 +19,7 @@ This file is used to store the preferences keys so that it's accessible and modi pref_server_ip pref_server_port pref_udp_server_port + pref_bluetooth_device_name pref_bluetooth_device_address pref_vehicle_type pref_rc_quickmode_right diff --git a/Android/res/values/strings.xml b/Android/res/values/strings.xml index ffc6380190..115cbe6fe2 100644 --- a/Android/res/values/strings.xml +++ b/Android/res/values/strings.xml @@ -422,6 +422,7 @@ BLUETOOTH CONNECTION Bluetooth device + Forget %s device Forget default device Are you sure? Forget @@ -529,5 +530,6 @@ select all Targeting selected location. Vehicle follows the user while remaining locked on a point of interest. Long click on the map to select the point of interest. + No saved bluetooth device. diff --git a/Android/src/org/droidplanner/android/activities/helpers/BluetoothDevicesActivity.java b/Android/src/org/droidplanner/android/activities/helpers/BluetoothDevicesActivity.java index 768b159e9b..62a73eeb4f 100644 --- a/Android/src/org/droidplanner/android/activities/helpers/BluetoothDevicesActivity.java +++ b/Android/src/org/droidplanner/android/activities/helpers/BluetoothDevicesActivity.java @@ -135,8 +135,8 @@ public void onItemClick(AdapterView parent, View view, int position, long id) // so the bluetooth client can retrieve it on connection. final Context context = getApplicationContext(); DroidPlannerPrefs mAppPrefs = new DroidPlannerPrefs(context); - mAppPrefs.setBluetoothDeviceAddress(device.getAddress() + ";" + device.getName()); - + mAppPrefs.setBluetoothDeviceName(device.getName()); + mAppPrefs.setBluetoothDeviceAddress(device.getAddress()); // Toggle the drone connection DroidPlannerApp.connectToDrone(context); diff --git a/Android/src/org/droidplanner/android/dialogs/ClearBTDialogPreference.java b/Android/src/org/droidplanner/android/dialogs/ClearBTDialogPreference.java index 1439f7fcc9..2c823358fa 100644 --- a/Android/src/org/droidplanner/android/dialogs/ClearBTDialogPreference.java +++ b/Android/src/org/droidplanner/android/dialogs/ClearBTDialogPreference.java @@ -8,8 +8,14 @@ public class ClearBTDialogPreference extends DialogPreference { + public interface OnResultListener { + void onResult(boolean result); + } + private DroidPlannerPrefs mAppPrefs; + private OnResultListener listener; + public ClearBTDialogPreference(Context context, AttributeSet attrs) { super(context, attrs); mAppPrefs = new DroidPlannerPrefs(context.getApplicationContext()); @@ -21,6 +27,13 @@ protected void onDialogClosed(boolean positiveResult) { if (positiveResult) { mAppPrefs.setBluetoothDeviceAddress(""); } + + if(listener != null) + listener.onResult(positiveResult); } + public void setOnResultListener(OnResultListener listener){ + this.listener = listener; + } + } diff --git a/Android/src/org/droidplanner/android/fragments/SettingsFragment.java b/Android/src/org/droidplanner/android/fragments/SettingsFragment.java index ede3b582f0..cbbf6a8edb 100644 --- a/Android/src/org/droidplanner/android/fragments/SettingsFragment.java +++ b/Android/src/org/droidplanner/android/fragments/SettingsFragment.java @@ -19,6 +19,7 @@ import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; import android.support.v4.content.LocalBroadcastManager; +import android.text.TextUtils; import android.util.Log; import android.widget.Toast; @@ -35,6 +36,7 @@ import org.droidplanner.android.DroidPlannerApp; import org.droidplanner.android.R; import org.droidplanner.android.activities.helpers.MapPreferencesActivity; +import org.droidplanner.android.dialogs.ClearBTDialogPreference; import org.droidplanner.android.maps.providers.DPMapProvider; import org.droidplanner.android.utils.Utils; import org.droidplanner.android.utils.analytics.GAUtils; @@ -229,6 +231,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { setupConnectionPreferences(); setupAdvancedMenuToggle(); setupUnitSystemPreferences(); + setupBluetoothDevicePreferences(); } private void setupAdvancedMenuToggle(){ @@ -298,6 +301,41 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { } } + private void setupBluetoothDevicePreferences(){ + final ClearBTDialogPreference preference = (ClearBTDialogPreference) findPreference(getString(R.string + .pref_bluetooth_device_address_key)); + if(preference != null){ + updateBluetoothDevicePreference(preference, dpPrefs.getBluetoothDeviceAddress()); + preference.setOnResultListener(new ClearBTDialogPreference.OnResultListener() { + @Override + public void onResult(boolean result) { + if(result){ + updateBluetoothDevicePreference(preference, dpPrefs.getBluetoothDeviceAddress()); + } + } + }); + } + } + + private void updateBluetoothDevicePreference(Preference preference, String deviceAddress){ + if(TextUtils.isEmpty(deviceAddress)) { + preference.setEnabled(false); + preference.setTitle(R.string.pref_no_saved_bluetooth_device_title); + preference.setSummary(""); + } + else{ + preference.setEnabled(true); + preference.setSummary(deviceAddress); + + final String deviceName = dpPrefs.getBluetoothDeviceName(); + if(deviceName != null){ + preference.setTitle(getString(R.string.pref_forget_bluetooth_device_title, deviceName)); + } + else + preference.setTitle(getString(R.string.pref_forget_bluetooth_device_address)); + } + } + private void updateConnectionPreferenceSummary(Preference preference, int connectionType) { String connectionName; switch (connectionType) { @@ -333,7 +371,6 @@ private void initSummaryPerPrefs() { mDefaultSummaryPrefs.add(getString(R.string.pref_server_port_key)); mDefaultSummaryPrefs.add(getString(R.string.pref_server_ip_key)); mDefaultSummaryPrefs.add(getString(R.string.pref_udp_server_port_key)); - mDefaultSummaryPrefs.add(getString(R.string.pref_bluetooth_device_address_key)); mDefaultSummaryPrefs.add(getString(R.string.pref_rc_quickmode_left_key)); mDefaultSummaryPrefs.add(getString(R.string.pref_rc_quickmode_right_key)); } diff --git a/Android/src/org/droidplanner/android/utils/prefs/DroidPlannerPrefs.java b/Android/src/org/droidplanner/android/utils/prefs/DroidPlannerPrefs.java index 4f7a8b6af0..69a36f1a0b 100644 --- a/Android/src/org/droidplanner/android/utils/prefs/DroidPlannerPrefs.java +++ b/Android/src/org/droidplanner/android/utils/prefs/DroidPlannerPrefs.java @@ -196,6 +196,14 @@ public int getUdpServerPort(){ .pref_udp_server_port_key), DEFAULT_UDP_SERVER_PORT)); } + public String getBluetoothDeviceName(){ + return prefs.getString(context.getString(R.string.pref_bluetooth_device_name_key), null); + } + + public void setBluetoothDeviceName(String deviceName){ + prefs.edit().putString(context.getString(R.string.pref_bluetooth_device_name_key), deviceName).apply(); + } + public String getBluetoothDeviceAddress() { return prefs.getString(context.getString(R.string.pref_bluetooth_device_address_key), null); }