Android Open Source - nxt-remote-controller N X T Connector






From Project

Back to project page nxt-remote-controller.

License

The source code is released under:

MIT License

If you think the Android project nxt-remote-controller listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  Copyright (c) 2014 EgaTuts & Esa Garca - All Rights Reserved                 *
 *                                                                                 *
 *  Open-source code licensed under the MIT License (the "License").               *
 *                                                                                 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy   *
 *  of this software and associated documentation files (the "Software"), to deal  *
 *  in the Software without restriction, including without limitation the rights   *
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      *
 *  copies of the Software, and to permit persons to whom the Software is          *
 *  furnished to do so, subject to the following conditions:                       *
 *                                                                                 *
 *  The above copyright notice and this permission notice shall be included in     *
 *  all copies or substantial portions of the Software.                            *
 *                                                                                 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     *
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       *
 *  FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE    *
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         *
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  *
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN      *
 *  THE SOFTWARE.                                                                  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
//from   w  ww  .  j a v  a 2 s . c o m
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  You can find the entire project at:                                                                                                                        *
 *                                                                                                                                                             *
 *    https://github.com/Egatuts/nxt-remote-controller                                                                                                         *
 *                                                                                                                                                             *
 *  And the corresponding file at:                                                                                                                             *
 *                                                                                                                                                             *
 *    https://github.com/Egatuts/nxt-remote-controller/blob/master/Android%20App/app/src/main/java/git/egatuts/nxtremotecontroller/bluetooth/NXTConnector.java *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package git.egatuts.nxtremotecontroller.bluetooth;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;

import git.egatuts.nxtremotecontroller.activity.ControllerActivity;
import git.egatuts.nxtremotecontroller.device.PairedDevice;
import git.egatuts.nxtremotecontroller.exception.SocketCreationException;
import git.egatuts.nxtremotecontroller.thread.ConnectThread;
import git.egatuts.nxtremotecontroller.thread.ConnectedThread;

/*
 *  Wrapper used in the BaseThread class used to store all the variables like
 *  the context and activity, resources, bluetooth utils, global utils, etc.
 */
public class NXTConnector {

  public static final int EMPTY_FIELD = 0;

  public static final int WHAT_CHANGE_STATE      = 1;
  public static final int WHAT_ERROR_ENCOUNTERED = 2;

  public static final int STATE_CHANGE_PREPARING_CONNECTION = 3;
  public static final int STATE_CHANGE_CREATING_SOCKET      = 4;
  public static final int STATE_CHANGE_CONNECTING           = 5;
  public static final int STATE_CHANGE_CONNECTED            = 6;

  public static final int ERROR_UNEXPECTED         = 7;
  public static final int ERROR_REQUEST_FAILED     = 8;
  public static final int ERROR_SOCKET_CREATE      = 9;
  public static final int ERROR_CONNECTION_CLOSED  = 10;
  public static final int ERROR_CONNECTION_LOST    = 11;

  private ControllerActivity activity;
  private Handler handler;
  private BluetoothDevice device;
  private BluetoothSocket socket;

  private ConnectThread connectThread;
  private ConnectedThread connectedThread;

  private int state;
  private int error;

  /*
   *  Constructor.
   */
  public NXTConnector () {}

  public NXTConnector (ControllerActivity activity, PairedDevice device, Handler handler) {
    this.activity = activity;
    this.device = device.getBluetoothDevice();
    this.handler = handler;
  }

  /*
   *  Getter and setter for the Handler.
   */
  public void setHandler (Handler handler) {
    this.handler = handler;
  }

  public Handler getHandler () {
    return this.handler;
  }

  /*
   *  Getter and setter for the BluetoothDevice.
   */
  public void setDevice (BluetoothDevice device) {
    this.device = device;
  }

  public BluetoothDevice getDevice () {
    return this.device;
  }

  /*
   *  Getter and setter for BluetoothSocket.
   */
  public void setSocket (BluetoothSocket socket) {
    this.socket = socket;
  }

  public BluetoothSocket getSocket () {
    return this.socket;
  }

  /*
   *  Getter and setter for the BaseActivity.
   */
  public void setActivity (ControllerActivity activity) {
    this.activity = activity;
  }

  public ControllerActivity getActivity () {
    return this.activity;
  }

  /*
   *  Sends a message through the handler.
   */
  public void sendMessage (int category, int state, int state2) {
    if (this.handler != null) {
      this.handler.obtainMessage(category, state, state2).sendToTarget();
    }
  }

  /*
   *  Changes the state and sends it through the handler.
   */
  public synchronized void setState (int state) {
    this.state = state;
    this.sendMessage(NXTConnector.WHAT_CHANGE_STATE, state, this.error);
  }

  /*
   *  Changes the error and sends it through the handler.
   */
  public synchronized void setError (int error) {
    this.error = error;
    this.sendMessage(NXTConnector.WHAT_ERROR_ENCOUNTERED, error, this.state);
  }

  /*
   *  Getters for state and error.
   */
  public synchronized int getState () {
    return this.state;
  }

  public synchronized int getError () {
    return this.error;
  }

  /*
   *  Getters for the state and the error that removes them.
   */
  public synchronized int popState () {
    int state = this.state;
    this.state = NXTConnector.EMPTY_FIELD;
    return state;
  }

  public synchronized int popError () {
    int error = this.error;
    this.error = NXTConnector.EMPTY_FIELD;
    return error;
  }

  /*
   *  Getters for the Threads.
   */
  public ConnectThread getConnectThread () {
    return this.connectThread;
  }

  public ConnectedThread getConnectedThread () {
    return this.connectedThread;
  }

  /*
   *  Closes all the connections and it's threads.
   */
  public synchronized void closeConnectThread () {
    if (this.connectThread != null) {
      this.connectThread.stopThread();
      this.connectThread = null;
    }
  }

  public synchronized void closeConnectedThread () {
    if (this.connectedThread != null) {
      this.connectedThread.stopThread();
      this.connectedThread = null;
    }
  }

  public synchronized void closeAllThreads () {
    this.closeConnectThread();
    this.closeConnectedThread();
  }

  /*
   *  State checkers for states.
   */
  public static boolean isDisconnected (int state) {
    return state == NXTConnector.EMPTY_FIELD;
  }

  public static boolean isPreparingConnection (int state) {
    return state == NXTConnector.STATE_CHANGE_PREPARING_CONNECTION;
  }

  public static boolean isCreatingSocket (int state) {
    return state == NXTConnector.STATE_CHANGE_CREATING_SOCKET;
  }

  public static boolean isConnecting (int state) {
    return state == NXTConnector.STATE_CHANGE_CONNECTING;
  }

  public static boolean isConnected (int state) {
    return state == NXTConnector.STATE_CHANGE_CONNECTED;
  }

  /*
   *  State checkers for errors.
   */
  public static boolean connectionFailed (int state, int error) {
    return state == NXTConnector.EMPTY_FIELD && error != NXTConnector.EMPTY_FIELD;
  }

  public static boolean connectionUnexpectedFailed (int state, int error) {
    return state == NXTConnector.EMPTY_FIELD && error == NXTConnector.ERROR_UNEXPECTED;
  }

  public static boolean connectionSocketFailed (int state, int error) {
    return state == NXTConnector.EMPTY_FIELD && error == NXTConnector.ERROR_SOCKET_CREATE;
  }

  public static boolean connectionRequestFailed (int state, int error) {
    return state == NXTConnector.EMPTY_FIELD && error == NXTConnector.ERROR_REQUEST_FAILED;
  }

  public static boolean connectionClosed (int state, int error) {
    return state == NXTConnector.EMPTY_FIELD && error == NXTConnector.ERROR_CONNECTION_CLOSED;
  }

  public static boolean connectionLost (int state, int error) {
    return state == NXTConnector.EMPTY_FIELD && error == NXTConnector.ERROR_CONNECTION_LOST;
  }

  /*
   *  Methods used to define the states.
   */
  public synchronized void setStatePreparing () {
    this.error = NXTConnector.EMPTY_FIELD;
    this.setState(NXTConnector.STATE_CHANGE_PREPARING_CONNECTION);
  }

  public synchronized void setStateCreatingSocket () {
    this.error = NXTConnector.EMPTY_FIELD;
    this.setState(NXTConnector.STATE_CHANGE_CREATING_SOCKET);
  }

  public synchronized void setStateConnecting () {
    this.error = NXTConnector.EMPTY_FIELD;
    this.setState(NXTConnector.STATE_CHANGE_CONNECTING);
  }

  public synchronized void setStateConnected () {
    this.error = NXTConnector.EMPTY_FIELD;
    this.setState(NXTConnector.STATE_CHANGE_CONNECTED);
  }

  /*
   *  Methods used to define errors.
   */
  public synchronized void setConnectionUnexpectedError () {
    this.state = NXTConnector.EMPTY_FIELD;
    this.setError(NXTConnector.ERROR_UNEXPECTED);
  }

  public synchronized void setConnectionSocketFailed () {
    this.state = NXTConnector.EMPTY_FIELD;
    this.setError(NXTConnector.ERROR_SOCKET_CREATE);
  }

  public synchronized void setConnectionRequestFailed () {
    this.state = NXTConnector.EMPTY_FIELD;
    this.setError(NXTConnector.ERROR_SOCKET_CREATE);
  }

  public synchronized void setConnectionClosed () {
    this.state = NXTConnector.EMPTY_FIELD;
    this.setError(NXTConnector.ERROR_CONNECTION_CLOSED);
  }

  public synchronized void setConnectionLost () {
    this.state = NXTConnector.EMPTY_FIELD;
    this.setError(NXTConnector.ERROR_CONNECTION_LOST);
  }

  /*
   *  Returns the from the activity BluetoothUtils
   */
  public BluetoothUtils getBluetoothUtils () {
    return this.activity.getBluetoothUtils();
  }

  /*
   *  Creates a socket from the stored device.
   */
  public void createSocket () throws SocketCreationException {
    this.socket = this.getBluetoothUtils().getSocketFrom(this.device);
  }

  /*
   *  These methods configure the device or the socket closing all the connections.
   */
  public synchronized void configureSocket (BluetoothSocket socket) {
    this.socket = socket;
    this.device = socket.getRemoteDevice();
  }

  public synchronized void configureDevice (BluetoothDevice device) {
    this.device = device;
    try {
      this.createSocket();
    } catch (SocketCreationException e) {
      this.setError(NXTConnector.ERROR_SOCKET_CREATE);
    }
  }

  /*
   *  Threads starters and stoppers.
   */
  public synchronized void startConnectThread () {
    if (this.connectThread != null && this.connectThread.isRunning()) {
      this.connectThread.stopThread();
    }
    this.connectThread = new ConnectThread(this);
    this.connectThread.startThread();
  }

  public synchronized void stopConnectThread () {
    if (this.connectThread != null) {
      this.connectThread.stopThread();
    }
  }

  public synchronized void startConnectedThread () {
    if (this.connectedThread != null && this.connectedThread.isRunning()) {
      this.connectedThread.stopThread();
    }
    this.connectedThread = new ConnectedThread(this);
    this.connectedThread.startThread();
  }

  public synchronized void stopConnectedThread () {
    if (this.connectedThread != null) {
      this.connectedThread.stopThread();
    }
  }

  /*
   *  Controls motors.
   */
  public void motorA (double power, boolean speedReg, boolean syncMotors) {
    this.connectedThread.write(BluetoothConstants.motorA(power, speedReg, syncMotors));
  }

  public void motorB (double power, boolean speedReg, boolean syncMotors) {
    this.connectedThread.write(BluetoothConstants.motorB(power, speedReg, syncMotors));
  }

  public void motorC (double power, boolean speedReg, boolean syncMotors) {
    this.connectedThread.write(BluetoothConstants.motorC(power, speedReg, syncMotors));
  }

  public void motorBC (double powerLeft, double powerRight, boolean speedReg, boolean syncMotors) {
    this.connectedThread.write(BluetoothConstants.motorBC(powerLeft, powerRight, speedReg, syncMotors));
  }

}




Java Source Code List

.OldPadControllerFragment.java
com.andexert.library.ApplicationTest.java
com.andexert.library.RippleView.java
com.andexert.rippleeffect.ApplicationTest.java
com.andexert.rippleeffect.CustomAdapter.java
com.andexert.rippleeffect.MainActivity.java
com.andexert.rippleeffect.OnTapListener.java
com.gc.materialdesign.utils.Utils.java
com.gc.materialdesign.views.ButtonFlat.java
com.gc.materialdesign.views.ButtonFloatSmall.java
com.gc.materialdesign.views.ButtonFloat.java
com.gc.materialdesign.views.ButtonIcon.java
com.gc.materialdesign.views.ButtonRectangle.java
com.gc.materialdesign.views.Button.java
com.gc.materialdesign.views.Card.java
com.gc.materialdesign.views.CheckBox.java
com.gc.materialdesign.views.CustomView.java
com.gc.materialdesign.views.LayoutRipple.java
com.gc.materialdesign.views.ProgressBarCircularIndeterminate.java
com.gc.materialdesign.views.ProgressBarDeterminate.java
com.gc.materialdesign.views.ProgressBarIndeterminateDeterminate.java
com.gc.materialdesign.views.ProgressBarIndeterminate.java
com.gc.materialdesign.views.RippleView.java
com.gc.materialdesign.views.ScrollView.java
com.gc.materialdesign.views.Slider.java
com.gc.materialdesign.views.Switch.java
com.gc.materialdesign.widgets.ColorSelector.java
com.gc.materialdesign.widgets.Dialog.java
com.gc.materialdesign.widgets.SnackBar.java
git.egatuts.nxtremotecontroller.ApplicationTest.java
git.egatuts.nxtremotecontroller.GlobalUtils.java
git.egatuts.nxtremotecontroller.activity.ActivityPendingTransition.java
git.egatuts.nxtremotecontroller.activity.BaseActivity.java
git.egatuts.nxtremotecontroller.activity.ControllerActivity.java
git.egatuts.nxtremotecontroller.activity.DefaultActivityPendingTransition.java
git.egatuts.nxtremotecontroller.activity.MainActivity.java
git.egatuts.nxtremotecontroller.activity.SettingsActivity.java
git.egatuts.nxtremotecontroller.bluetooth.BluetoothConstants.java
git.egatuts.nxtremotecontroller.bluetooth.BluetoothUtils.java
git.egatuts.nxtremotecontroller.bluetooth.NXTConnector.java
git.egatuts.nxtremotecontroller.device.PairedDeviceAdapter.java
git.egatuts.nxtremotecontroller.device.PairedDeviceItemClickListener.java
git.egatuts.nxtremotecontroller.device.PairedDeviceViewHolder.java
git.egatuts.nxtremotecontroller.device.PairedDevice.java
git.egatuts.nxtremotecontroller.exception.SocketCreationException.java
git.egatuts.nxtremotecontroller.fragment.ActivityBaseFragment.java
git.egatuts.nxtremotecontroller.fragment.BaseFragment.java
git.egatuts.nxtremotecontroller.fragment.BluetoothFragment.java
git.egatuts.nxtremotecontroller.fragment.ControllerBaseFragment.java
git.egatuts.nxtremotecontroller.fragment.DefaultFragmentPendingTransition.java
git.egatuts.nxtremotecontroller.fragment.FragmentPendingTransition.java
git.egatuts.nxtremotecontroller.fragment.HomeFragment.java
git.egatuts.nxtremotecontroller.fragment.LocalControllerFragment.java
git.egatuts.nxtremotecontroller.fragment.OnlineControllerFragment.java
git.egatuts.nxtremotecontroller.fragment.ScanFragment.java
git.egatuts.nxtremotecontroller.fragment.SettingsFragment.java
git.egatuts.nxtremotecontroller.listener.AnimationEndListener.java
git.egatuts.nxtremotecontroller.listener.AppKillerListener.java
git.egatuts.nxtremotecontroller.listener.BaseListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothDiscoveryListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothEnableListener.java
git.egatuts.nxtremotecontroller.listener.BluetoothPairingListener.java
git.egatuts.nxtremotecontroller.navigation.DrawerItemViewHolder.java
git.egatuts.nxtremotecontroller.navigation.DrawerItem.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerAdapter.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerCallback.java
git.egatuts.nxtremotecontroller.navigation.NavigationDrawerFragment.java
git.egatuts.nxtremotecontroller.preference.PreferencesUtils.java
git.egatuts.nxtremotecontroller.receiver.AppKillerReceiver.java
git.egatuts.nxtremotecontroller.receiver.BaseReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothDiscoveryReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothEnableReceiver.java
git.egatuts.nxtremotecontroller.receiver.BluetoothPairingReceiver.java
git.egatuts.nxtremotecontroller.thread.BaseThread.java
git.egatuts.nxtremotecontroller.thread.ConnectThread.java
git.egatuts.nxtremotecontroller.thread.ConnectedThread.java
git.egatuts.nxtremotecontroller.views.BaseIndeterminateProgressDialog.java
git.egatuts.nxtremotecontroller.views.BaseProgressDialog.java
git.egatuts.nxtremotecontroller.views.JoystickView.java
git.egatuts.nxtremotecontroller.views.LongIndeterminateProgressDialog.java
git.egatuts.nxtremotecontroller.views.ShortIndeterminateProgressDialog.java