Files
NxtAR-cam/src/ve/ucv/ciens/ccg/nxtcam/MainActivity.java

325 lines
10 KiB
Java
Raw Normal View History

2013-11-05 11:44:26 -04:30
package ve.ucv.ciens.ccg.nxtcam;
2013-11-05 18:22:25 -04:30
import java.io.IOException;
import java.net.DatagramPacket;
2013-11-05 11:44:26 -04:30
import java.net.InetAddress;
2013-11-05 18:22:25 -04:30
import java.net.MulticastSocket;
2013-11-05 11:44:26 -04:30
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;
2013-11-05 11:44:26 -04:30
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
2013-11-05 18:22:25 -04:30
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
2013-11-05 11:44:26 -04:30
import android.app.Activity;
import android.app.DialogFragment;
import android.bluetooth.BluetoothAdapter;
2013-11-05 11:44:26 -04:30
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
2013-11-05 18:22:25 -04:30
import android.net.wifi.WifiManager.MulticastLock;
2013-11-05 11:44:26 -04:30
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
2013-11-05 11:44:26 -04:30
import android.widget.Toast;
2013-11-05 18:22:25 -04:30
/**
* Entry point por the NxtCAM application.
*
* This activity shows a splashscreen and handles the search for the controller device
* via a simle ad hoc UDP based service discovery method. Basically, it just listens for
* multicast packets sent by the controller device on the multicast address defined in
* ProjectConstants.java. When the packet is received the next activity of the application
* is launched with the ip address found. The service discovery process continues until a
* datagram carrying the string "NxtAR server here!" is received.
*
* @author miky
*/
public class MainActivity extends Activity implements WifiOnDialogListener, ConnectRobotDialogListener{
2013-11-05 18:22:25 -04:30
// Cosntant fields.
2013-11-05 11:44:26 -04:30
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;
2013-11-05 11:44:26 -04:30
2013-11-05 18:22:25 -04:30
// Resources.
private BTCommunicator btManager;
2013-11-05 11:44:26 -04:30
private WifiManager wifiManager;
2013-11-05 18:22:25 -04:30
// Variables.
2013-11-05 11:44:26 -04:30
private boolean wifiOnByMe;
private boolean btOnByMe;
2013-11-05 11:44:26 -04:30
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
2013-11-05 18:22:25 -04:30
// 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);
2013-11-05 18:22:25 -04:30
// Set up services.
btManager = BTCommunicator.getInstance();
2013-11-05 11:44:26 -04:30
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);
}
2013-11-05 11:44:26 -04:30
}
@Override
public void onResume(){
super.onResume();
if(!btManager.isBTEnabled()){
enableBT();
}else if(btManager.isBTEnabled() && !wifiManager.isWifiEnabled()){
enableWifi();
}
2013-11-05 11:44:26 -04:30
}
@Override
public void onPause(){
super.onPause();
if(btManager.isBTEnabled() && btOnByMe)
btManager.disableBT();
2013-11-05 18:22:25 -04:30
if(wifiManager.isWifiEnabled() && wifiOnByMe)
setWifi(false);
2013-11-05 11:44:26 -04:30
}
@Override
public void onDestroy(){
super.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());
}
}
}
2013-11-05 11:44:26 -04:30
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
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();
}
}
2013-11-05 18:22:25 -04:30
/**
* Start the camera capture activity if a server was found through service discovery.
*
* @param serverFound Indicates if a server was found, doh!
* @param ipAddress The ip address of the server.
*/
private void startCamActivity(boolean serverFound, String ipAddress){
if(serverFound){
Logger.log_d(TAG, CLASS_NAME + ".startCamActivity() :: Launching camera activity.");
2013-11-05 11:44:26 -04:30
Intent intent = new Intent(this, CamActivity.class);
2013-11-05 18:22:25 -04:30
intent.putExtra("address", ipAddress);
2013-11-05 11:44:26 -04:30
startActivity(intent);
}else{
Logger.log_d(TAG, CLASS_NAME + ".startCamActivity() :: Cannot launch camera activity.");
2013-11-05 11:44:26 -04:30
Toast.makeText(this, R.string.badIpToast, Toast.LENGTH_SHORT).show();
}
}
2013-11-05 18:22:25 -04:30
/**
* Sets the state of the device's WiFi radio.
*
* @param radioState The state to set the radio to; true for on, false for off.
*/
private void setWifi(boolean radioState){
wifiManager.setWifiEnabled(radioState);
Logger.log_d(TAG, CLASS_NAME + ".setWifi() :: setting wifi to " + (radioState ? "on" : "off"));
2013-11-05 18:22:25 -04:30
if(radioState)
2013-11-05 11:44:26 -04:30
wifiOnByMe = true;
else
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();
}
2013-11-05 18:22:25 -04:30
/**
* Asynchronous task for ad hoc UDP service discovery.
*
* @author Miguel Angel Astor Romero
*/
private class ServiceDiscoveryTask extends AsyncTask<Void, Void, Boolean>{
private final String CLASS_NAME = ServiceDiscoveryTask.class.getSimpleName();
private MulticastSocket udpSocket;
private DatagramPacket packet;
private MulticastLock multicastLock;
public ServiceDiscoveryTask(){
// Open a multicast socket and join the project's multicast group.
try{
udpSocket = new MulticastSocket(ProjectConstants.SERVER_UDP_PORT);
InetAddress group = InetAddress.getByName(ProjectConstants.MULTICAST_ADDRESS);
udpSocket.joinGroup(group);
}catch(IOException io){
Logger.log_e(TAG ,CLASS_NAME + ".ServiceDiscoveryTask() :: " + io.getMessage());
2013-11-05 18:22:25 -04:30
}
}
@Override
protected Boolean doInBackground(Void... params){
boolean result, done = false;
byte[] buffer = (new String("Server is here")).getBytes();
// Create a buffer and tell Android we want to receive multicast datagrams.
packet = new DatagramPacket(buffer, buffer.length);
multicastLock = wifiManager.createMulticastLock(TAG);
multicastLock.setReferenceCounted(true);
multicastLock.acquire();
// Listen for a UDP datagram on the multicast group.
// If the datagram received contains a string with it's content equal to "NxtAR server here!"
// then assume the server found is a valid controller device.
try{
while(!done){
udpSocket.receive(packet);
Logger.log_d(TAG, CLASS_NAME + ".run() :: Found a server at " + packet.getAddress().getHostAddress());
2013-11-05 18:22:25 -04:30
String received = new String(packet.getData());
if(received.compareTo("NxtAR server here!") == 0)
done = true;
}
result = true;
}catch(IOException io){
Logger.log_e(TAG, CLASS_NAME + ".doInBackground() :: " + io.getMessage());
2013-11-05 18:22:25 -04:30
result = false;
}
// Tell Android we do not want to receive more UDP datagrams to save battery life.
if(multicastLock != null){
multicastLock.release();
multicastLock = null;
}
return result;
}
@Override
protected void onPostExecute(Boolean result){
super.onPostExecute(result);
// If a server was found then start the next activity.
if(packet != null)
startCamActivity(result, packet.getAddress().getHostAddress());
else
startCamActivity(false, null);
2013-11-05 11:44:26 -04:30
}
}
2013-11-05 11:44:26 -04:30
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);
}
}
}
2013-11-05 11:44:26 -04:30
@Override
public void onWifiOnDialogPositiveClick(DialogFragment dialog) {
Toast.makeText(this, R.string.wifi_on_success, Toast.LENGTH_SHORT).show();
wifiOnByMe = true;
}
2013-11-05 11:44:26 -04:30
@Override
public void onWifiOnDialogNegativeClick(DialogFragment dialog) {
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();
}
2013-11-05 11:44:26 -04:30
}