/**
   * this function is called when the user wants to disconnect the device. This could happen if the
   * user choosen the wrong shimmer
   */
  private void disconnectDevice() {
    if (isDeviceConnected()) {
      serviceshim.disconnectShimmer(btAddress);
      serviceshim.stopStreaming(btAddress);
      serviceshim.setLogging(false, btAddress);
    } else
      Toast.makeText(ShimmerDevicesMenu.this, " No device to disconnect ", Toast.LENGTH_SHORT)
          .show();

    SystemClock.sleep(200);
    updateInterface();
  }
  /**
   * this function determines if the specific shimmer device is connected to the shimmer service. It
   * has it's own function because the fucntionality gets used so many times
   *
   * @return
   */
  private boolean isDeviceConnected() {

    if (serviceshim != null) {
      // if the user returns from the Bluetooth finder then we will have the btAddress
      // of the device already and we can set the radio button without having to create
      // a list of the connected shimmer devices through the hashMapShimmer
      if (btAddress != "" && serviceshim.isDeviceConnected(btAddress)) return true;
      else {
        HashMap<String, Object> temp = serviceshim.hashMapShimmer;
        Collection<Object> shimmers = temp.values();
        Iterator<Object> iterator = shimmers.iterator();
        while (iterator.hasNext()) {
          Shimmer sTemp = (Shimmer) iterator.next();
          int sensor = sTemp.getEnabledSensors();

          if ((sensor == Shimmer.SENSOR_ECG && deviceid == ECG_DEVICE)
              || (sensor == Shimmer.SENSOR_EMG && deviceid == EMG_DEVICE)
              || (sensor == Shimmer.SENSOR_GSR && deviceid == GSR_DEVICE)) {
            btAddress = sTemp.getBluetoothAddress();
            return true;
          }
          // TODO: add section for EEG device
        } // end while loop
      } // end else statement
    } // end if serviceshim not null
    return false;
  } // end function isDevicConnected()
  /**
   * This function is called when the user selects 'Start Logging' from the device menu. When
   * pressed, we will start streaming data from the device if it is not already streaming data. We
   * will then save data to a text file on the users phone or tablet. TODO: make logging compatible
   * with the profiles
   */
  private void startLogging() {
    if (isDeviceConnected()) {
      if (!isDeviceStreaming()) // to log data we must first be streaming the data
      startStreaming();

      if (!isDeviceLogging()) serviceshim.setLogging(true, btAddress);
    }

    updateInterface();
  }
  /** Make a simple graph of the data that is being plotted by the currently selected device. */
  private void plotData() {
    if (isDeviceConnected()) {
      if (!isDeviceStreaming()) serviceshim.startStreaming(btAddress);

      Intent graphIntent = new Intent(ShimmerDevicesMenu.this, ShimmerGraphActivity.class);
      graphIntent.putExtra("Bluetooth Address", btAddress);
      startActivity(graphIntent);
    } else
      Toast.makeText(ShimmerDevicesMenu.this, " Must connect device first", Toast.LENGTH_SHORT)
          .show();
  }
  /**
   * This function gets called when the user hits the 'Stop Streaming' button on the menu of this
   * activity. This will only be called if the device is currently streaming, and it will stop the
   * streaming of data from the currently connected device.
   */
  private void stopStreaming() {
    if (isDeviceConnected()) {
      if (isDeviceStreaming()) {
        // since we can't save data that we aren't streaming, set the logging to false
        if (isDeviceLogging()) serviceshim.setLogging(false, btAddress);

        serviceshim.stopStreaming(btAddress);
        Log.d(logName, "ShimmerDevicesMenu: " + devicename + " stopped streaming");
      } else
        Toast.makeText(
                ShimmerDevicesMenu.this, devicename + " is not streaming", Toast.LENGTH_SHORT)
            .show();
    } // end if is the device is connected
    else
      Toast.makeText(ShimmerDevicesMenu.this, " Must Connect a Device first", Toast.LENGTH_SHORT)
          .show();

    SystemClock.sleep(200);
    updateInterface();
  }
  /**
   * This function gets called when the user hits the 'Start Streaming' button on the menu of this
   * activity. This will start the logging of the data coming frmo the specific shimmer device that
   * this menu is made for
   */
  private void startStreaming() {
    if (isDeviceConnected()) {
      if (!isDeviceStreaming()) {
        serviceshim.startStreaming(btAddress);
        Log.d(logName, "ShimmerDevicesMenu: " + devicename + " started streaming");
      } else
        Toast.makeText(
                ShimmerDevicesMenu.this, devicename + " is already streaming", Toast.LENGTH_SHORT)
            .show();

    } // end if is the device is connected
    else
      Toast.makeText(ShimmerDevicesMenu.this, " Must Connect a Device first", Toast.LENGTH_SHORT)
          .show();

    SystemClock.sleep(100);
    updateInterface();
  }
  /**
   * This next function gets called automatically when the dialog windows that correspond to each
   * menu choice are closed and the view returns to this activity
   *
   * @param requestCode the requestCode that was sent to the dialog window
   * @param resultCode either success or failure code, lets us know if the action failed or
   *     succeeded
   * @param data the intent that gets passed back from the dialog window
   */
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    TRYING_TO_CONNECT = true;
    updateInterface();

    if ((resultCode == RESULT_OK && serviceshim != null)
        && data.getStringExtra("device_address") != "") {
      btAddress = data.getStringExtra("device_address");
      serviceshim.connectShimmer(btAddress, deviceid);
    }

    // we use this postDelayed to wait a few seconds for the device to connect before we update the
    // UI
    // we do it this way so as to not freeze the UI while still waiting for the shimmer device to
    // connect
    mHandler.postDelayed(
        new Runnable() {
          public void run() {
            TRYING_TO_CONNECT = false;
            updateInterface();
          }
        },
        25);
  } // end onActivityResult
  /**
   * This function determines if the current device is having its data logged to a file.
   *
   * @return boolean that corresponds to if the device is logging data currently
   */
  private boolean isDeviceLogging() {
    if (isDeviceConnected()) return (serviceshim.getLogging(btAddress));

    return false;
  }
 /**
  * this function determines if the specific shimmer device is currently streaming.
  *
  * @return boolean thats true if the device is streaming and false if not
  */
 private boolean isDeviceStreaming() {
   return (isDeviceConnected() && serviceshim.isDeviceStreaming(btAddress));
 }
 /** This function stops the application from logging data from a shimmer device to a text file */
 private void stopLogging() {
   if (isDeviceConnected()) {
     if (isDeviceLogging()) serviceshim.setLogging(false, btAddress);
   }
   updateInterface();
 }