Finished connecting with the robot via Bluetooth
This commit is contained in:
@@ -2,10 +2,42 @@
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:baselineAligned="false"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
android:baselineAligned="false"
|
||||
tools:context=".MainActivity" >
|
||||
|
||||
<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_failure">La cámara no pudo abrirse</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="ipAddressLabel">Dirección IP de NxtAR</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="wifi_on_button">Encender</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>
|
||||
|
@@ -8,7 +8,7 @@
|
||||
<string name="camera_failure">Camera could not be opened</string>
|
||||
<string name="title_activity_cam">CamActivity</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="badIpToast">Invalid IP address</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="cancel_button">Cancel</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_fail">This app cannot work without wifi. Closing.</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="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>
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -4,10 +4,9 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
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.ProjectConstants;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothSocket;
|
||||
@@ -22,16 +21,13 @@ import android.util.Log;
|
||||
*/
|
||||
public class BTCommunicator{
|
||||
private static final String TAG = "NXT_TEST_BTCOMM";
|
||||
private final String CLASS_NAME = MainActivity.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 final String CLASS_NAME = BTCommunicator.class.getSimpleName();
|
||||
|
||||
private boolean connected;
|
||||
private BluetoothAdapter bt_adapter = null;
|
||||
private BluetoothSocket bt_socket = null;
|
||||
private OutputStream nxt_out_stream = null;
|
||||
private InputStream nxt_in_stream = null;
|
||||
private BluetoothAdapter btAdapter = null;
|
||||
private BluetoothSocket btSocket = null;
|
||||
private OutputStream nxtOutputStream = null;
|
||||
private InputStream nxtInputStream = null;
|
||||
|
||||
private static class SingletonHolder{
|
||||
public static final BTCommunicator INSTANCE = new BTCommunicator();
|
||||
@@ -51,10 +47,10 @@ public class BTCommunicator{
|
||||
*/
|
||||
private BTCommunicator(){
|
||||
connected = false;
|
||||
bt_adapter = BluetoothAdapter.getDefaultAdapter();
|
||||
bt_socket = null;
|
||||
nxt_in_stream = null;
|
||||
nxt_out_stream = null;
|
||||
btAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
btSocket = null;
|
||||
nxtInputStream = null;
|
||||
nxtOutputStream = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +59,7 @@ public class BTCommunicator{
|
||||
* @return true if the default Bluetooth adapter exists, otherwise false.
|
||||
*/
|
||||
public boolean isBTSupported(){
|
||||
return bt_adapter != null;
|
||||
return btAdapter != null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,7 +77,7 @@ public class BTCommunicator{
|
||||
* @see android.bluetooth.BluetoothAdapter
|
||||
*/
|
||||
public boolean isBTEnabled(){
|
||||
return bt_adapter.isEnabled();
|
||||
return btAdapter.isEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,7 +86,7 @@ public class BTCommunicator{
|
||||
* @see android.bluetooth.BluetoothAdapter
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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.
|
||||
* 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.
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean establishConnection(String mac_address) throws IOException{
|
||||
if (!bt_adapter.isEnabled()){
|
||||
public boolean establishConnection(String macAddress) throws IOException{
|
||||
if (!btAdapter.isEnabled()){
|
||||
return false;
|
||||
}
|
||||
if(connected){
|
||||
return false;
|
||||
}
|
||||
if(bt_adapter.isEnabled()){
|
||||
if(mac_address == "NONE"){
|
||||
if(btAdapter.isEnabled()){
|
||||
if(macAddress == "NONE"){
|
||||
return false;
|
||||
}else{
|
||||
if(mac_address.substring(0, 8).compareTo(OUI_LEGO) != 0){
|
||||
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Not a Lego MAC. Prefix : " + mac_address.substring(0, 8) + " :: OUI : " + OUI_LEGO);
|
||||
if(macAddress.substring(0, 8).compareTo(ProjectConstants.OUI_LEGO) != 0){
|
||||
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Not a Lego MAC. Prefix : " + macAddress.substring(0, 8) + " :: OUI : " + ProjectConstants.OUI_LEGO);
|
||||
return false;
|
||||
}else{
|
||||
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;
|
||||
nxtDevice = bt_adapter.getRemoteDevice(mac_address);
|
||||
nxtDevice = btAdapter.getRemoteDevice(macAddress);
|
||||
if (nxtDevice == null) {
|
||||
Logger.log_e(TAG, CLASS_NAME + ".establishConnection() :: No device found.");
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
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.");
|
||||
bt_socket.connect();
|
||||
btSocket.connect();
|
||||
|
||||
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Opening IO streams.");
|
||||
nxt_in_stream = bt_socket.getInputStream();
|
||||
nxt_out_stream = bt_socket.getOutputStream();
|
||||
nxtInputStream = btSocket.getInputStream();
|
||||
nxtOutputStream = btSocket.getOutputStream();
|
||||
|
||||
Logger.log_d(TAG, CLASS_NAME + ".establishConnection() :: Connection established.");
|
||||
connected = true;
|
||||
@@ -171,12 +167,12 @@ public class BTCommunicator{
|
||||
*/
|
||||
public boolean stopConnection() throws IOException{
|
||||
try{
|
||||
if(bt_socket != null){
|
||||
if(btSocket != null){
|
||||
Logger.log_d(TAG, CLASS_NAME + ".stopConnection() :: Closing connection.");
|
||||
bt_socket.close();
|
||||
bt_socket = null;
|
||||
nxt_in_stream = null;
|
||||
nxt_out_stream = null;
|
||||
btSocket.close();
|
||||
btSocket = null;
|
||||
nxtInputStream = null;
|
||||
nxtOutputStream = null;
|
||||
connected = false;
|
||||
Logger.log_d(TAG, CLASS_NAME + ".stopConnection() :: Connection closed.");
|
||||
return true;
|
||||
@@ -198,7 +194,7 @@ public class BTCommunicator{
|
||||
public synchronized void writeMessage(byte[] message) throws IOException{
|
||||
if(connected){
|
||||
try{
|
||||
nxt_out_stream.write(message);
|
||||
nxtOutputStream.write(message);
|
||||
}catch(IOException e){
|
||||
Logger.log_e(TAG, CLASS_NAME + ".writeMessage()");
|
||||
Logger.log_e(TAG, Log.getStackTraceString(e));
|
||||
@@ -220,7 +216,7 @@ public class BTCommunicator{
|
||||
for(int i = 0; i < message.length; ++i){
|
||||
message[i] = 0x00;
|
||||
}
|
||||
nxt_in_stream.read(message, 0, bytes);
|
||||
nxtInputStream.read(message, 0, bytes);
|
||||
return message;
|
||||
}catch(IOException e){
|
||||
Logger.log_e(TAG, CLASS_NAME + ".readMessage()");
|
||||
|
@@ -1,10 +1,13 @@
|
||||
package ve.ucv.ciens.ccg.nxtcam.utils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public abstract class ProjectConstants {
|
||||
public static final int SERVER_UDP_PORT = 8889;
|
||||
public static final int SERVER_TCP_PORT_1 = 9989;
|
||||
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 boolean DEBUG = true;
|
||||
|
Reference in New Issue
Block a user