Finished connecting with the robot via Bluetooth
This commit is contained in:
@@ -2,10 +2,42 @@
|
|||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
|
android:baselineAligned="false"
|
||||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||||
android:paddingTop="@dimen/activity_vertical_margin"
|
android:paddingTop="@dimen/activity_vertical_margin"
|
||||||
android:baselineAligned="false"
|
|
||||||
tools:context=".MainActivity" >
|
tools:context=".MainActivity" >
|
||||||
</RelativeLayout>
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/connectButton"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_below="@+id/imageView1"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:gravity="center"
|
||||||
|
android:onClick="connectWithRobot"
|
||||||
|
android:text="@string/robot_pair_button" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/startButton"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignLeft="@+id/connectButton"
|
||||||
|
android:layout_alignRight="@+id/connectButton"
|
||||||
|
android:layout_below="@+id/connectButton"
|
||||||
|
android:gravity="center"
|
||||||
|
android:onClick="startConnections"
|
||||||
|
android:text="@string/start_button" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/imageView1"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignParentTop="true"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:layout_marginTop="60dp"
|
||||||
|
android:contentDescription="@string/lens_icon"
|
||||||
|
android:src="@drawable/camera_lens" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
@@ -7,7 +7,7 @@
|
|||||||
<string name="camera_success">Cámara abierta exitosamente</string>
|
<string name="camera_success">Cámara abierta exitosamente</string>
|
||||||
<string name="camera_failure">La cámara no pudo abrirse</string>
|
<string name="camera_failure">La cámara no pudo abrirse</string>
|
||||||
<string name="get_server_button">Conectar con NxtAR</string>
|
<string name="get_server_button">Conectar con NxtAR</string>
|
||||||
<string name="start_button">Conectar con dispositivo controlador</string>
|
<string name="start_button">Comenzar streaming de video</string>
|
||||||
<string name="title_activity_cam">CamActivity</string>
|
<string name="title_activity_cam">CamActivity</string>
|
||||||
<string name="ipAddressLabel">Dirección IP de NxtAR</string>
|
<string name="ipAddressLabel">Dirección IP de NxtAR</string>
|
||||||
<string name="badIpToast">La dirección IP no es válida</string>
|
<string name="badIpToast">La dirección IP no es válida</string>
|
||||||
@@ -17,6 +17,13 @@
|
|||||||
<string name="cancel_button">Cancelar</string>
|
<string name="cancel_button">Cancelar</string>
|
||||||
<string name="wifi_on_button">Encender</string>
|
<string name="wifi_on_button">Encender</string>
|
||||||
<string name="wifi_on_success">El radio del WiFi está encendido</string>
|
<string name="wifi_on_success">El radio del WiFi está encendido</string>
|
||||||
<string name="wifi_on_fail">Esta app no puede funcionar sin wifi. Cerrando.</string>
|
<string name="wifi_on_fail">Esta app no puede funcionar sin wifi, cerrando.</string>
|
||||||
|
<string name= "bt_no_support">Este dispositivo no soporta Bluetooth</string>
|
||||||
|
<string name="bt_on_fail">Esta app no puede funcionar sin Bluetooth, cerrando</string>
|
||||||
|
<string name="lens_icon">Ícono de lente</string>
|
||||||
|
<string name="robot_pair_button">Conectar con el robot</string>
|
||||||
|
<string name="robot_choice">Escoja un robot</string>
|
||||||
|
<string name="conn_established">Conexión exitosa con el robot</string>
|
||||||
|
<string name="conn_failed">No se pudo conectar con el robot</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
@@ -8,7 +8,7 @@
|
|||||||
<string name="camera_failure">Camera could not be opened</string>
|
<string name="camera_failure">Camera could not be opened</string>
|
||||||
<string name="title_activity_cam">CamActivity</string>
|
<string name="title_activity_cam">CamActivity</string>
|
||||||
<string name="get_server_button">Connect with NxtAR</string>
|
<string name="get_server_button">Connect with NxtAR</string>
|
||||||
<string name="start_button">Connect with controller device</string>
|
<string name="start_button">Start video streaming</string>
|
||||||
<string name="ipAddressLabel">NxtAR IP Address</string>
|
<string name="ipAddressLabel">NxtAR IP Address</string>
|
||||||
<string name="badIpToast">Invalid IP address</string>
|
<string name="badIpToast">Invalid IP address</string>
|
||||||
<string name="emptyIpToast">Fill out the IP address field</string>
|
<string name="emptyIpToast">Fill out the IP address field</string>
|
||||||
@@ -16,7 +16,14 @@
|
|||||||
<string name="wifi_on_msg">Turn on the WiFi?</string>
|
<string name="wifi_on_msg">Turn on the WiFi?</string>
|
||||||
<string name="cancel_button">Cancel</string>
|
<string name="cancel_button">Cancel</string>
|
||||||
<string name="wifi_on_button">Turn on</string>
|
<string name="wifi_on_button">Turn on</string>
|
||||||
<string name="wifi_on_success">The Wifi radio is on now</string>
|
<string name="wifi_on_success">The WiFi radio is on now</string>
|
||||||
<string name="wifi_on_fail">This app cannot work without wifi. Closing.</string>
|
<string name="wifi_on_fail">This app cannot work without WiFi, closing</string>
|
||||||
|
<string name="bt_no_support">This device does not support Bluetooth</string>
|
||||||
|
<string name="bt_on_fail">This app cannot work without Bluetooth, closing</string>
|
||||||
|
<string name="lens_icon">Image lens icon</string>
|
||||||
|
<string name="robot_pair_button">Connect with robot</string>
|
||||||
|
<string name="robot_choice">Pick a robot</string>
|
||||||
|
<string name="conn_established">Successfully connected with the robot</string>
|
||||||
|
<string name="conn_failed">Could not connect with the robot</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
@@ -5,12 +5,16 @@ import java.net.DatagramPacket;
|
|||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.MulticastSocket;
|
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;
|
||||||
import ve.ucv.ciens.ccg.nxtcam.dialogs.WifiOnDialog.WifiOnDialogListener;
|
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.Logger;
|
||||||
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
|
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.DialogFragment;
|
import android.app.DialogFragment;
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
@@ -18,6 +22,8 @@ import android.net.wifi.WifiManager.MulticastLock;
|
|||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,16 +39,23 @@ import android.widget.Toast;
|
|||||||
* @author miky
|
* @author miky
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class MainActivity extends Activity implements WifiOnDialogListener{
|
public class MainActivity extends Activity implements WifiOnDialogListener, ConnectRobotDialogListener{
|
||||||
// Cosntant fields.
|
// Cosntant fields.
|
||||||
private final String TAG = "NXTCAM_MAIN";
|
private final String TAG = "NXTCAM_MAIN";
|
||||||
private final String CLASS_NAME = MainActivity.class.getSimpleName();
|
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.
|
// Resources.
|
||||||
|
private BTCommunicator btManager;
|
||||||
private WifiManager wifiManager;
|
private WifiManager wifiManager;
|
||||||
|
|
||||||
// Variables.
|
// Variables.
|
||||||
private boolean wifiOnByMe;
|
private boolean wifiOnByMe;
|
||||||
|
private boolean btOnByMe;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@@ -51,18 +64,33 @@ public class MainActivity extends Activity implements WifiOnDialogListener{
|
|||||||
|
|
||||||
// Set up fields.
|
// Set up fields.
|
||||||
wifiOnByMe = false;
|
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.
|
// Set up services.
|
||||||
|
btManager = BTCommunicator.getInstance();
|
||||||
wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
|
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
|
@Override
|
||||||
public void onResume(){
|
public void onResume(){
|
||||||
super.onResume();
|
super.onResume();
|
||||||
|
|
||||||
if(!wifiManager.isWifiEnabled()){
|
if(!btManager.isBTEnabled()){
|
||||||
DialogFragment wifiOn = new WifiOnDialog();
|
enableBT();
|
||||||
wifiOn.show(getFragmentManager(), "wifi_on");
|
}else if(btManager.isBTEnabled() && !wifiManager.isWifiEnabled()){
|
||||||
|
enableWifi();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,10 +98,24 @@ public class MainActivity extends Activity implements WifiOnDialogListener{
|
|||||||
public void onPause(){
|
public void onPause(){
|
||||||
super.onPause();
|
super.onPause();
|
||||||
|
|
||||||
|
if(btManager.isBTEnabled() && btOnByMe)
|
||||||
|
btManager.disableBT();
|
||||||
|
|
||||||
if(wifiManager.isWifiEnabled() && wifiOnByMe)
|
if(wifiManager.isWifiEnabled() && wifiOnByMe)
|
||||||
setWifi(false);
|
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
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
// Inflate the menu; this adds items to the action bar if it is present.
|
// 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;
|
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.
|
* 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;
|
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.
|
* Asynchronous task for ad hoc UDP service discovery.
|
||||||
*
|
*
|
||||||
@@ -185,17 +255,70 @@ public class MainActivity extends Activity implements WifiOnDialogListener{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ServiceDiscoveryTask serviceDiscovery = new ServiceDiscoveryTask();
|
private class ConnectRobotTask extends AsyncTask<Void, Void, Boolean>{
|
||||||
serviceDiscovery.execute(); */
|
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
|
@Override
|
||||||
public void onWifiOnDialogPositiveClick(DialogFragment dialog) {
|
public void onWifiOnDialogPositiveClick(DialogFragment dialog) {
|
||||||
Toast.makeText(this, R.string.wifi_on_success, Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, R.string.wifi_on_success, Toast.LENGTH_SHORT).show();
|
||||||
|
wifiOnByMe = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onWifiOnDialogNegativeClick(DialogFragment dialog) {
|
public void onWifiOnDialogNegativeClick(DialogFragment dialog) {
|
||||||
Toast.makeText(this, R.string.wifi_on_fail, Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, R.string.wifi_on_fail, Toast.LENGTH_LONG).show();
|
||||||
System.exit(0);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,10 +1,18 @@
|
|||||||
package ve.ucv.ciens.ccg.nxtcam.dialogs;
|
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.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.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.app.DialogFragment;
|
import android.app.DialogFragment;
|
||||||
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
@@ -13,10 +21,12 @@ public class ConnectRobotDialog extends DialogFragment {
|
|||||||
private final String CLASS_NAME = ConnectRobotDialog.class.getSimpleName();
|
private final String CLASS_NAME = ConnectRobotDialog.class.getSimpleName();
|
||||||
|
|
||||||
private ConnectRobotDialogListener listener;
|
private ConnectRobotDialogListener listener;
|
||||||
|
private BTCommunicator btManager;
|
||||||
|
private List<String> devices;
|
||||||
|
private String[] devicesArray;
|
||||||
|
|
||||||
public interface ConnectRobotDialogListener{
|
public interface ConnectRobotDialogListener{
|
||||||
public void onConnectRobotDialogPositiveClick(DialogFragment dialog);
|
public void onConnectRobotDialogItemClick(DialogFragment dialog, String item);
|
||||||
public void onConnectRobotDialogNegativeClick(DialogFragment dialog);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -24,16 +34,28 @@ public class ConnectRobotDialog extends DialogFragment {
|
|||||||
// Use the Builder class for convenient dialog construction
|
// Use the Builder class for convenient dialog construction
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
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() {
|
}).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 + ".setNegativeButton().onClick( :: User canceled.");
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@@ -11,9 +11,10 @@ import android.net.wifi.WifiManager;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
public class WifiOnDialog extends DialogFragment {
|
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 final String CLASS_NAME = WifiOnDialog.class.getSimpleName();
|
||||||
|
|
||||||
|
private WifiOnDialogListener listener;
|
||||||
private WifiManager wifiManager;
|
private WifiManager wifiManager;
|
||||||
|
|
||||||
public interface WifiOnDialogListener{
|
public interface WifiOnDialogListener{
|
||||||
@@ -26,23 +27,32 @@ public class WifiOnDialog extends DialogFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public Dialog onCreateDialog(Bundle savedInstanceState){
|
||||||
// Use the Builder class for convenient dialog construction
|
// Use the Builder class for convenient dialog construction
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||||
|
|
||||||
builder.setMessage(R.string.wifi_on_msg).setPositiveButton(R.string.wifi_on_button, new DialogInterface.OnClickListener() {
|
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){
|
if(wifiManager != null){
|
||||||
wifiManager.setWifiEnabled(true);
|
wifiManager.setWifiEnabled(true);
|
||||||
Logger.log_d(TAG, CLASS_NAME + " :: setting wifi to on.");
|
Logger.log_d(TAG, CLASS_NAME + ".setPositiveButton().onClick() :: setting wifi to on.");
|
||||||
}else
|
if(listener != null)
|
||||||
Logger.log_wtf(TAG, CLASS_NAME + " :: wifiManager is null! Doing nothing.");
|
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() {
|
}).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
|
@Override
|
||||||
public void onAttach(Activity activity){
|
public void onAttach(Activity activity){
|
||||||
|
super.onAttach(activity);
|
||||||
|
|
||||||
try{
|
try{
|
||||||
@SuppressWarnings("unused")
|
listener = (WifiOnDialogListener)activity;
|
||||||
WifiOnDialogListener listener = (WifiOnDialogListener)activity;
|
|
||||||
}catch(ClassCastException cce){
|
}catch(ClassCastException cce){
|
||||||
throw new ClassCastException(activity.toString() + "Must implement WifiOnDialogListener.");
|
listener = null;
|
||||||
|
throw new ClassCastException(CLASS_NAME + ".onAttach() :: " + activity.toString() + "Must implement WifiOnDialogListener.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,10 +4,9 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import ve.ucv.ciens.ccg.nxtcam.MainActivity;
|
|
||||||
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
|
||||||
|
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.bluetooth.BluetoothSocket;
|
import android.bluetooth.BluetoothSocket;
|
||||||
@@ -22,17 +21,14 @@ import android.util.Log;
|
|||||||
*/
|
*/
|
||||||
public class BTCommunicator{
|
public class BTCommunicator{
|
||||||
private static final String TAG = "NXT_TEST_BTCOMM";
|
private static final String TAG = "NXT_TEST_BTCOMM";
|
||||||
private final String CLASS_NAME = MainActivity.class.getSimpleName();
|
private final String CLASS_NAME = BTCommunicator.class.getSimpleName();
|
||||||
|
|
||||||
private static final UUID SERIAL_PORT_SERVICE_CLASS_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
|
|
||||||
private static final String OUI_LEGO = "00:16:53";
|
|
||||||
|
|
||||||
private boolean connected;
|
private boolean connected;
|
||||||
private BluetoothAdapter bt_adapter = null;
|
private BluetoothAdapter btAdapter = null;
|
||||||
private BluetoothSocket bt_socket = null;
|
private BluetoothSocket btSocket = null;
|
||||||
private OutputStream nxt_out_stream = null;
|
private OutputStream nxtOutputStream = null;
|
||||||
private InputStream nxt_in_stream = null;
|
private InputStream nxtInputStream = null;
|
||||||
|
|
||||||
private static class SingletonHolder{
|
private static class SingletonHolder{
|
||||||
public static final BTCommunicator INSTANCE = new BTCommunicator();
|
public static final BTCommunicator INSTANCE = new BTCommunicator();
|
||||||
}
|
}
|
||||||
@@ -51,10 +47,10 @@ public class BTCommunicator{
|
|||||||
*/
|
*/
|
||||||
private BTCommunicator(){
|
private BTCommunicator(){
|
||||||
connected = false;
|
connected = false;
|
||||||
bt_adapter = BluetoothAdapter.getDefaultAdapter();
|
btAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
bt_socket = null;
|
btSocket = null;
|
||||||
nxt_in_stream = null;
|
nxtInputStream = null;
|
||||||
nxt_out_stream = null;
|
nxtOutputStream = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,7 +59,7 @@ public class BTCommunicator{
|
|||||||
* @return true if the default Bluetooth adapter exists, otherwise false.
|
* @return true if the default Bluetooth adapter exists, otherwise false.
|
||||||
*/
|
*/
|
||||||
public boolean isBTSupported(){
|
public boolean isBTSupported(){
|
||||||
return bt_adapter != null;
|
return btAdapter != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -81,7 +77,7 @@ public class BTCommunicator{
|
|||||||
* @see android.bluetooth.BluetoothAdapter
|
* @see android.bluetooth.BluetoothAdapter
|
||||||
*/
|
*/
|
||||||
public boolean isBTEnabled(){
|
public boolean isBTEnabled(){
|
||||||
return bt_adapter.isEnabled();
|
return btAdapter.isEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,7 +86,7 @@ public class BTCommunicator{
|
|||||||
* @see android.bluetooth.BluetoothAdapter
|
* @see android.bluetooth.BluetoothAdapter
|
||||||
*/
|
*/
|
||||||
public void disableBT(){
|
public void disableBT(){
|
||||||
bt_adapter.disable();
|
btAdapter.disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -99,7 +95,7 @@ public class BTCommunicator{
|
|||||||
* @return A set containing all devices paired to this device.
|
* @return A set containing all devices paired to this device.
|
||||||
*/
|
*/
|
||||||
public Set<BluetoothDevice> getPairedDevices(){
|
public Set<BluetoothDevice> getPairedDevices(){
|
||||||
return bt_adapter.getBondedDevices();
|
return btAdapter.getBondedDevices();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -108,42 +104,42 @@ public class BTCommunicator{
|
|||||||
* Verifies if the target device is a valid NXT robot by checking agains Lego's OUI.
|
* Verifies if the target device is a valid NXT robot by checking agains Lego's OUI.
|
||||||
* Also creates the socket and the streams associated with the connection
|
* Also creates the socket and the streams associated with the connection
|
||||||
*
|
*
|
||||||
* @param mac_address The mac address of the target device.
|
* @param macAddress The mac address of the target device.
|
||||||
* @return true if the connection was established succesfully, otherwise false.
|
* @return true if the connection was established succesfully, otherwise false.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public boolean establishConnection(String mac_address) throws IOException{
|
public boolean establishConnection(String macAddress) throws IOException{
|
||||||
if (!bt_adapter.isEnabled()){
|
if (!btAdapter.isEnabled()){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(connected){
|
if(connected){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(bt_adapter.isEnabled()){
|
if(btAdapter.isEnabled()){
|
||||||
if(mac_address == "NONE"){
|
if(macAddress == "NONE"){
|
||||||
return false;
|
return false;
|
||||||
}else{
|
}else{
|
||||||
if(mac_address.substring(0, 8).compareTo(OUI_LEGO) != 0){
|
if(macAddress.substring(0, 8).compareTo(ProjectConstants.OUI_LEGO) != 0){
|
||||||
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Not a Lego MAC. Prefix : " + mac_address.substring(0, 8) + " :: OUI : " + OUI_LEGO);
|
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Not a Lego MAC. Prefix : " + macAddress.substring(0, 8) + " :: OUI : " + ProjectConstants.OUI_LEGO);
|
||||||
return false;
|
return false;
|
||||||
}else{
|
}else{
|
||||||
try{
|
try{
|
||||||
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Getting device with mac address: " + mac_address);
|
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Getting device with mac address: " + macAddress);
|
||||||
BluetoothDevice nxtDevice = null;
|
BluetoothDevice nxtDevice = null;
|
||||||
nxtDevice = bt_adapter.getRemoteDevice(mac_address);
|
nxtDevice = btAdapter.getRemoteDevice(macAddress);
|
||||||
if (nxtDevice == null) {
|
if (nxtDevice == null) {
|
||||||
Logger.log_e(TAG, CLASS_NAME + ".establishConnection() :: No device found.");
|
Logger.log_e(TAG, CLASS_NAME + ".establishConnection() :: No device found.");
|
||||||
throw new IOException();
|
throw new IOException();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Opening socket.");
|
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Opening socket.");
|
||||||
bt_socket = nxtDevice.createRfcommSocketToServiceRecord(SERIAL_PORT_SERVICE_CLASS_UUID);
|
btSocket = nxtDevice.createRfcommSocketToServiceRecord(ProjectConstants.SERIAL_PORT_SERVICE_CLASS_UUID);
|
||||||
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Connecting.");
|
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Connecting.");
|
||||||
bt_socket.connect();
|
btSocket.connect();
|
||||||
|
|
||||||
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Opening IO streams.");
|
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Opening IO streams.");
|
||||||
nxt_in_stream = bt_socket.getInputStream();
|
nxtInputStream = btSocket.getInputStream();
|
||||||
nxt_out_stream = bt_socket.getOutputStream();
|
nxtOutputStream = btSocket.getOutputStream();
|
||||||
|
|
||||||
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Connection established.");
|
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Connection established.");
|
||||||
connected = true;
|
connected = true;
|
||||||
@@ -171,12 +167,12 @@ public class BTCommunicator{
|
|||||||
*/
|
*/
|
||||||
public boolean stopConnection() throws IOException{
|
public boolean stopConnection() throws IOException{
|
||||||
try{
|
try{
|
||||||
if(bt_socket != null){
|
if(btSocket != null){
|
||||||
Logger.log_d(TAG, CLASS_NAME + ".stopConnection() :: Closing connection.");
|
Logger.log_d(TAG, CLASS_NAME + ".stopConnection() :: Closing connection.");
|
||||||
bt_socket.close();
|
btSocket.close();
|
||||||
bt_socket = null;
|
btSocket = null;
|
||||||
nxt_in_stream = null;
|
nxtInputStream = null;
|
||||||
nxt_out_stream = null;
|
nxtOutputStream = null;
|
||||||
connected = false;
|
connected = false;
|
||||||
Logger.log_d(TAG, CLASS_NAME + ".stopConnection() :: Connection closed.");
|
Logger.log_d(TAG, CLASS_NAME + ".stopConnection() :: Connection closed.");
|
||||||
return true;
|
return true;
|
||||||
@@ -198,7 +194,7 @@ public class BTCommunicator{
|
|||||||
public synchronized void writeMessage(byte[] message) throws IOException{
|
public synchronized void writeMessage(byte[] message) throws IOException{
|
||||||
if(connected){
|
if(connected){
|
||||||
try{
|
try{
|
||||||
nxt_out_stream.write(message);
|
nxtOutputStream.write(message);
|
||||||
}catch(IOException e){
|
}catch(IOException e){
|
||||||
Logger.log_e(TAG, CLASS_NAME + ".writeMessage()");
|
Logger.log_e(TAG, CLASS_NAME + ".writeMessage()");
|
||||||
Logger.log_e(TAG, Log.getStackTraceString(e));
|
Logger.log_e(TAG, Log.getStackTraceString(e));
|
||||||
@@ -220,7 +216,7 @@ public class BTCommunicator{
|
|||||||
for(int i = 0; i < message.length; ++i){
|
for(int i = 0; i < message.length; ++i){
|
||||||
message[i] = 0x00;
|
message[i] = 0x00;
|
||||||
}
|
}
|
||||||
nxt_in_stream.read(message, 0, bytes);
|
nxtInputStream.read(message, 0, bytes);
|
||||||
return message;
|
return message;
|
||||||
}catch(IOException e){
|
}catch(IOException e){
|
||||||
Logger.log_e(TAG, CLASS_NAME + ".readMessage()");
|
Logger.log_e(TAG, CLASS_NAME + ".readMessage()");
|
||||||
|
@@ -1,11 +1,14 @@
|
|||||||
package ve.ucv.ciens.ccg.nxtcam.utils;
|
package ve.ucv.ciens.ccg.nxtcam.utils;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public abstract class ProjectConstants {
|
public abstract class ProjectConstants {
|
||||||
public static final int SERVER_UDP_PORT = 8889;
|
public static final int SERVER_UDP_PORT = 8889;
|
||||||
public static final int SERVER_TCP_PORT_1 = 9989;
|
public static final int SERVER_TCP_PORT_1 = 9989;
|
||||||
public static final int SERVER_TCP_PORT_2 = 9990;
|
public static final int SERVER_TCP_PORT_2 = 9990;
|
||||||
|
public static final UUID SERIAL_PORT_SERVICE_CLASS_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
|
||||||
|
public static final String OUI_LEGO = "00:16:53";
|
||||||
public static final String MULTICAST_ADDRESS = "230.0.0.1";
|
public static final String MULTICAST_ADDRESS = "230.0.0.1";
|
||||||
|
|
||||||
public static final boolean DEBUG = true;
|
public static final boolean DEBUG = true;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user