Skip to content

Commit

Permalink
Fixed connection issues with bluetooth.
Browse files Browse the repository at this point in the history
  • Loading branch information
m4gr3d committed Jan 28, 2015
1 parent 65bc1be commit 57a9523
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions Android/res/values/preferences_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This file is used to store the preferences keys so that it's accessible and modi
<string name="pref_server_ip_key" translatable="false">pref_server_ip</string>
<string name="pref_server_port_key" translatable="false">pref_server_port</string>
<string name="pref_udp_server_port_key" translatable="false">pref_udp_server_port</string>
<string name="pref_bluetooth_device_name_key" translatable="false">pref_bluetooth_device_name</string>
<string name="pref_bluetooth_device_address_key" translatable="false">pref_bluetooth_device_address</string>
<string name="pref_vehicle_type_key" translatable="false">pref_vehicle_type</string>
<string name="pref_rc_quickmode_right_key" translatable="false">pref_rc_quickmode_right</string>
Expand Down
2 changes: 2 additions & 0 deletions Android/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@
<!-- BLuetooth -->
<string name="pref_bluetooth">BLUETOOTH CONNECTION</string>
<string name="pref_bluetooth_device_address">Bluetooth device</string>
<string name="pref_forget_bluetooth_device_title">Forget %s device</string>
<string name="pref_forget_bluetooth_device_address">Forget default device</string>
<string name="pref_forget_bluetooth_device_address_confirm">Are you sure?</string>
<string name="pref_bluetooth_forget">Forget</string>
Expand Down Expand Up @@ -529,5 +530,6 @@
<string name="label_select_all">select all</string>
<string name="guided_scan_roi_set_message">Targeting selected location.</string>
<string name="mode_follow_guided_scan">Vehicle follows the user while remaining locked on a point of interest. Long click on the map to select the point of interest.</string>
<string name="pref_no_saved_bluetooth_device_title">No saved bluetooth device.</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -229,6 +231,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
setupConnectionPreferences();
setupAdvancedMenuToggle();
setupUnitSystemPreferences();
setupBluetoothDevicePreferences();
}

private void setupAdvancedMenuToggle(){
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 57a9523

Please sign in to comment.