Finished connecting with the robot via Bluetooth
This commit is contained in:
@@ -1,10 +1,18 @@
|
||||
package ve.ucv.ciens.ccg.nxtcam.dialogs;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import ve.ucv.ciens.ccg.nxtcam.R;
|
||||
import ve.ucv.ciens.ccg.nxtcam.network.BTCommunicator;
|
||||
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
||||
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.app.DialogFragment;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
|
||||
@@ -13,10 +21,12 @@ public class ConnectRobotDialog extends DialogFragment {
|
||||
private final String CLASS_NAME = ConnectRobotDialog.class.getSimpleName();
|
||||
|
||||
private ConnectRobotDialogListener listener;
|
||||
private BTCommunicator btManager;
|
||||
private List<String> devices;
|
||||
private String[] devicesArray;
|
||||
|
||||
public interface ConnectRobotDialogListener{
|
||||
public void onConnectRobotDialogPositiveClick(DialogFragment dialog);
|
||||
public void onConnectRobotDialogNegativeClick(DialogFragment dialog);
|
||||
public void onConnectRobotDialogItemClick(DialogFragment dialog, String item);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -24,16 +34,28 @@ public class ConnectRobotDialog extends DialogFragment {
|
||||
// Use the Builder class for convenient dialog construction
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
|
||||
builder.setMessage(R.string.wifi_on_msg).setPositiveButton(R.string.wifi_on_button, new DialogInterface.OnClickListener() {
|
||||
// Fill a list with all the paired LEGO robots.
|
||||
btManager = BTCommunicator.getInstance();
|
||||
devices = new ArrayList<String>();
|
||||
Set<BluetoothDevice> pairedDevices = btManager.getPairedDevices();
|
||||
for (BluetoothDevice device : pairedDevices) {
|
||||
// Put the device in the list only if it's MAC address belongs to LEGO.
|
||||
if(device.getAddress().substring(0, 8).compareTo(ProjectConstants.OUI_LEGO) == 0)
|
||||
devices.add(device.getName() + "\n" + device.getAddress());
|
||||
}
|
||||
devicesArray = devices.toArray(new String[devices.size()]);
|
||||
|
||||
public void onClick(DialogInterface dialog, int id){
|
||||
builder.setTitle(R.string.robot_choice).setItems(devicesArray, new DialogInterface.OnClickListener() {
|
||||
|
||||
public void onClick(DialogInterface dialog, int which){
|
||||
Logger.log_d(TAG, CLASS_NAME + ".setItems().onClick( :: User chose: " + devices.get(which));
|
||||
listener.onConnectRobotDialogItemClick(ConnectRobotDialog.this, devices.get(which));
|
||||
}
|
||||
|
||||
}).setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
|
||||
|
||||
public void onClick(DialogInterface dialog, int id){
|
||||
|
||||
Logger.log_d(TAG, CLASS_NAME + ".setNegativeButton().onClick( :: User canceled.");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@@ -11,9 +11,10 @@ import android.net.wifi.WifiManager;
|
||||
import android.os.Bundle;
|
||||
|
||||
public class WifiOnDialog extends DialogFragment {
|
||||
private final String TAG = "NXTCAM_MAIN";
|
||||
private final String TAG = "NXTCAM_WIFI_DIALOG";
|
||||
private final String CLASS_NAME = WifiOnDialog.class.getSimpleName();
|
||||
|
||||
private WifiOnDialogListener listener;
|
||||
private WifiManager wifiManager;
|
||||
|
||||
public interface WifiOnDialogListener{
|
||||
@@ -26,23 +27,32 @@ public class WifiOnDialog extends DialogFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState){
|
||||
// Use the Builder class for convenient dialog construction
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
|
||||
builder.setMessage(R.string.wifi_on_msg).setPositiveButton(R.string.wifi_on_button, new DialogInterface.OnClickListener() {
|
||||
|
||||
public void onClick(DialogInterface dialog, int id) {
|
||||
public void onClick(DialogInterface dialog, int id){
|
||||
if(wifiManager != null){
|
||||
wifiManager.setWifiEnabled(true);
|
||||
Logger.log_d(TAG, CLASS_NAME + " :: setting wifi to on.");
|
||||
}else
|
||||
Logger.log_wtf(TAG, CLASS_NAME + " :: wifiManager is null! Doing nothing.");
|
||||
Logger.log_d(TAG, CLASS_NAME + ".setPositiveButton().onClick() :: setting wifi to on.");
|
||||
if(listener != null)
|
||||
listener.onWifiOnDialogPositiveClick(WifiOnDialog.this);
|
||||
}else{
|
||||
Logger.log_wtf(TAG, CLASS_NAME + ".setPositiveButton().onClick( :: wifiManager is null! Doing nothing.");
|
||||
if(listener != null)
|
||||
listener.onWifiOnDialogNegativeClick(WifiOnDialog.this);
|
||||
}
|
||||
}
|
||||
|
||||
}).setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
|
||||
|
||||
public void onClick(DialogInterface dialog, int id) { }
|
||||
public void onClick(DialogInterface dialog, int id){
|
||||
Logger.log_d(TAG, CLASS_NAME + ".setPositiveButton().onClick( :: User canceled.");
|
||||
if(listener != null)
|
||||
listener.onWifiOnDialogNegativeClick(WifiOnDialog.this);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
@@ -52,11 +62,13 @@ public class WifiOnDialog extends DialogFragment {
|
||||
|
||||
@Override
|
||||
public void onAttach(Activity activity){
|
||||
super.onAttach(activity);
|
||||
|
||||
try{
|
||||
@SuppressWarnings("unused")
|
||||
WifiOnDialogListener listener = (WifiOnDialogListener)activity;
|
||||
listener = (WifiOnDialogListener)activity;
|
||||
}catch(ClassCastException cce){
|
||||
throw new ClassCastException(activity.toString() + "Must implement WifiOnDialogListener.");
|
||||
listener = null;
|
||||
throw new ClassCastException(CLASS_NAME + ".onAttach() :: " + activity.toString() + "Must implement WifiOnDialogListener.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user