Finished connecting with the robot via Bluetooth
This commit is contained in:
@@ -5,12 +5,16 @@ import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
|
||||
import ve.ucv.ciens.ccg.nxtcam.dialogs.ConnectRobotDialog;
|
||||
import ve.ucv.ciens.ccg.nxtcam.dialogs.ConnectRobotDialog.ConnectRobotDialogListener;
|
||||
import ve.ucv.ciens.ccg.nxtcam.dialogs.WifiOnDialog;
|
||||
import ve.ucv.ciens.ccg.nxtcam.dialogs.WifiOnDialog.WifiOnDialogListener;
|
||||
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.DialogFragment;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.wifi.WifiManager;
|
||||
@@ -18,6 +22,8 @@ import android.net.wifi.WifiManager.MulticastLock;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
@@ -33,16 +39,23 @@ import android.widget.Toast;
|
||||
* @author miky
|
||||
*
|
||||
*/
|
||||
public class MainActivity extends Activity implements WifiOnDialogListener{
|
||||
public class MainActivity extends Activity implements WifiOnDialogListener, ConnectRobotDialogListener{
|
||||
// Cosntant fields.
|
||||
private final String TAG = "NXTCAM_MAIN";
|
||||
private final String CLASS_NAME = MainActivity.class.getSimpleName();
|
||||
private static final int REQUEST_ENABLE_BT = 1;
|
||||
|
||||
// Gui components
|
||||
private Button startButton;
|
||||
private Button connectButton;
|
||||
|
||||
// Resources.
|
||||
private BTCommunicator btManager;
|
||||
private WifiManager wifiManager;
|
||||
|
||||
// Variables.
|
||||
private boolean wifiOnByMe;
|
||||
private boolean btOnByMe;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -51,18 +64,33 @@ public class MainActivity extends Activity implements WifiOnDialogListener{
|
||||
|
||||
// Set up fields.
|
||||
wifiOnByMe = false;
|
||||
btOnByMe = false;
|
||||
|
||||
// Set up gui components.
|
||||
startButton = (Button)findViewById(R.id.startButton);
|
||||
startButton.setEnabled(false);
|
||||
connectButton = (Button)findViewById(R.id.connectButton);
|
||||
|
||||
// Set up services.
|
||||
btManager = BTCommunicator.getInstance();
|
||||
wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
|
||||
|
||||
if(!btManager.isBTSupported()){
|
||||
// El dispositivo no soporta BlueTooth.
|
||||
Toast.makeText(this, R.string.bt_no_support, Toast.LENGTH_LONG).show();
|
||||
finish();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume(){
|
||||
super.onResume();
|
||||
|
||||
if(!wifiManager.isWifiEnabled()){
|
||||
DialogFragment wifiOn = new WifiOnDialog();
|
||||
wifiOn.show(getFragmentManager(), "wifi_on");
|
||||
if(!btManager.isBTEnabled()){
|
||||
enableBT();
|
||||
}else if(btManager.isBTEnabled() && !wifiManager.isWifiEnabled()){
|
||||
enableWifi();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,10 +98,24 @@ public class MainActivity extends Activity implements WifiOnDialogListener{
|
||||
public void onPause(){
|
||||
super.onPause();
|
||||
|
||||
if(btManager.isBTEnabled() && btOnByMe)
|
||||
btManager.disableBT();
|
||||
|
||||
if(wifiManager.isWifiEnabled() && wifiOnByMe)
|
||||
setWifi(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy(){
|
||||
if(btManager.isConnected()){
|
||||
try{
|
||||
btManager.stopConnection();
|
||||
}catch(IOException io){
|
||||
Logger.log_e(TAG, CLASS_NAME + ".onDestroy() :: Error closing the connection with the robot: " + io.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
@@ -81,6 +123,15 @@ public class MainActivity extends Activity implements WifiOnDialogListener{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void onActivityResult(int request, int result, Intent data){
|
||||
if(request == REQUEST_ENABLE_BT && result == RESULT_OK){
|
||||
if(!wifiManager.isWifiEnabled())
|
||||
enableWifi();
|
||||
}else{
|
||||
Toast.makeText(this, R.string.bt_on_fail, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the camera capture activity if a server was found through service discovery.
|
||||
*
|
||||
@@ -113,6 +164,25 @@ public class MainActivity extends Activity implements WifiOnDialogListener{
|
||||
wifiOnByMe = false;
|
||||
}
|
||||
|
||||
private void enableWifi(){
|
||||
if(!wifiManager.isWifiEnabled()){
|
||||
DialogFragment wifiOn = new WifiOnDialog();
|
||||
((WifiOnDialog)wifiOn).setWifiManager(wifiManager);
|
||||
wifiOn.show(getFragmentManager(), "wifi_on");
|
||||
}
|
||||
}
|
||||
|
||||
private void enableBT(){
|
||||
Logger.log_d(TAG, CLASS_NAME + ".enableBT() :: Enabling the Bluetooth radio.");
|
||||
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
|
||||
btOnByMe = true;
|
||||
}
|
||||
|
||||
protected void showToast(int stringId, int length){
|
||||
Toast.makeText(this, stringId, length).show();
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronous task for ad hoc UDP service discovery.
|
||||
*
|
||||
@@ -185,17 +255,70 @@ public class MainActivity extends Activity implements WifiOnDialogListener{
|
||||
}
|
||||
}
|
||||
|
||||
/* ServiceDiscoveryTask serviceDiscovery = new ServiceDiscoveryTask();
|
||||
serviceDiscovery.execute(); */
|
||||
private class ConnectRobotTask extends AsyncTask<Void, Void, Boolean>{
|
||||
private final String CLASS_NAME = ConnectRobotTask.class.getSimpleName();
|
||||
private String macAddress;
|
||||
|
||||
public ConnectRobotTask(String macAddress){
|
||||
this.macAddress = macAddress;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... params){
|
||||
boolean connSet;
|
||||
Logger.log_d(TAG, CLASS_NAME + "doInBackground() :: Establishing connection with the robot.");
|
||||
try{
|
||||
connSet = btManager.establishConnection(macAddress);
|
||||
}catch(IOException e){
|
||||
Logger.log_e(TAG, CLASS_NAME + "doInBackground() :: Error during the connection attempt.");
|
||||
connSet = false;
|
||||
}
|
||||
return connSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result){
|
||||
if(result){
|
||||
Logger.log_d(TAG, CLASS_NAME + "doInBackground() :: Connection successful.");
|
||||
showToast(R.string.conn_established, Toast.LENGTH_SHORT);
|
||||
}else{
|
||||
Logger.log_d(TAG, CLASS_NAME + "doInBackground() :: Connection failed.");
|
||||
showToast(R.string.conn_failed, Toast.LENGTH_LONG);
|
||||
connectButton.setEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWifiOnDialogPositiveClick(DialogFragment dialog) {
|
||||
Toast.makeText(this, R.string.wifi_on_success, Toast.LENGTH_SHORT).show();
|
||||
wifiOnByMe = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWifiOnDialogNegativeClick(DialogFragment dialog) {
|
||||
Toast.makeText(this, R.string.wifi_on_fail, Toast.LENGTH_SHORT).show();
|
||||
System.exit(0);
|
||||
Toast.makeText(this, R.string.wifi_on_fail, Toast.LENGTH_LONG).show();
|
||||
finish();
|
||||
};
|
||||
|
||||
public void connectWithRobot(View view){
|
||||
if(btManager.isBTEnabled()){
|
||||
DialogFragment connectBot = new ConnectRobotDialog();
|
||||
connectBot.show(getFragmentManager(), "connect_bot");
|
||||
}
|
||||
}
|
||||
|
||||
public void startConnections(View view){
|
||||
ServiceDiscoveryTask serviceDiscovery = new ServiceDiscoveryTask();
|
||||
serviceDiscovery.execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectRobotDialogItemClick(DialogFragment dialog, String item) {
|
||||
String macAddress = item.substring(item.indexOf('\n')+1);
|
||||
Logger.log_d(TAG, CLASS_NAME + ".onConnectRobotDialogItemClick() :: MAC address: " + macAddress);
|
||||
connectButton.setEnabled(false);
|
||||
ConnectRobotTask robotTask = new ConnectRobotTask(macAddress);
|
||||
robotTask.execute();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user