/**
  * Start Streaming Data to Unity
  *
  * @param robot the Robot to stream data from
  * @param divisor Divisor of the maximum sensor sampling rate (400 Hz)
  * @param packetFrames Number of samples created on the device before it sends a packet to the
  *     phone with samples
  * @param sensorMask Bitwise selector of data sources to stream
  * @param packetCount Packet count (set to 0 for unlimited streaming)
  */
 public void setDataStreaming(
     Robot robot, int divisor, int packetFrames, long sensorMask, int packetCount) {
   // Remove old profile
   if (sensorMask == SetDataStreamingCommand.DATA_STREAMING_MASK_OFF) {
     DeviceMessenger.getInstance().removeAsyncDataListener(robot, mDataListener);
   }
   // Override current one
   else {
     DeviceMessenger.getInstance().addAsyncDataListener(robot, mDataListener);
   }
   SetDataStreamingCommand.sendCommand(robot, divisor, packetFrames, sensorMask, packetCount);
 }
  private void startStreaming() {

    if (mRobot != null) {

      // Set up a bitmask containing the sensor information we want to stream
      final int mask =
          SetDataStreamingCommand.DATA_STREAMING_MASK_ACCELEROMETER_X_FILTERED
              | SetDataStreamingCommand.DATA_STREAMING_MASK_ACCELEROMETER_Y_FILTERED
              | SetDataStreamingCommand.DATA_STREAMING_MASK_ACCELEROMETER_Z_FILTERED
              | SetDataStreamingCommand.DATA_STREAMING_MASK_IMU_PITCH_ANGLE_FILTERED
              | SetDataStreamingCommand.DATA_STREAMING_MASK_IMU_ROLL_ANGLE_FILTERED
              | SetDataStreamingCommand.DATA_STREAMING_MASK_IMU_YAW_ANGLE_FILTERED;

      // Specify a divisor. The frequency of responses that will be sent is 400hz divided by this
      // divisor.
      // 50
      final int divisor = 50;

      // Specify the number of frames that will be in each response. You can use a higher number to
      // "save up" responses and send them at once with a lower frequency, but more packets per
      // response.
      final int packet_frames = 1;

      // Total number of responses before streaming ends. 0 is infinite.
      final int response_count = 0;

      // Send this command to Sphero to start streaming
      SetDataStreamingCommand.sendCommand(mRobot, divisor, packet_frames, mask, response_count);

      // Set the AsyncDataListener that will process each response.
      DeviceMessenger.getInstance().addAsyncDataListener(mRobot, mDataListener);
    }
  }
  /**
   * Disable controller data streaming for a Robot
   *
   * @param robot the Robot to stream data from
   */
  public void disableControllerStreaming(Robot robot) {
    // Turn on stabilization and turn off back LED
    StabilizationCommand.sendCommand(robot, true);
    BackLEDOutputCommand.sendCommand(robot, 0.0f);

    // Disable data streaming and delete profile
    SetDataStreamingCommand.sendCommand(robot, 0, 0, 0, 0);
    DeviceMessenger.getInstance().removeAsyncDataListener(robot, mDataListener);
  }
  @Override
  protected void onStop() {
    super.onStop();

    if (screenWakeLock != null) {
      screenWakeLock.release();
      screenWakeLock = null;
    }

    // remove the flag that disables the screen dimming
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    // remove use of the location listener
    locationManager.removeUpdates(locationListener);

    // turn stabilization back on
    StabilizationCommand.sendCommand(mRobot, true);

    // turn rear light off
    FrontLEDOutputCommand.sendCommand(mRobot, 0.0f);

    // unregister the async data listener to prevent a memory leak.
    DeviceMessenger.getInstance().removeAsyncDataListener(mRobot, mDataListener);

    // stop the streaming data when we leave
    SetDataStreamingCommand.sendCommand(
        mRobot, 0, 0, SetDataStreamingCommand.DATA_STREAMING_MASK_OFF, 0);

    // pause here for a tenth of a second to allow the previous commands to go through before we
    // shutdown
    // the connection to the ball
    try {
      Thread.sleep(100);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    // disconnect from the ball
    RobotProvider.getDefaultProvider().removeAllControls();

    // mRobot = null;
  }