Android Open Source - rsmonitor-heartrate Client Thread






From Project

Back to project page rsmonitor-heartrate.

License

The source code is released under:

GNU General Public License

If you think the Android project rsmonitor-heartrate 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

package com.renaultsport.heartrate.utils;
//from   ww  w  .j  a  va2s.c o m
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

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




public class ClientThread extends Thread
{
  private static String  CREATE_SOCKET_METHOD  = "createRfcommSocket";
  private static int    CHANNEL_ANALOG_OUTPUT_6  = 21;

  private boolean          m_bActive      = false;
  private int            m_nBatteryIndicator  = 0;
  private int            m_nHeartRate    = 0;
  private int            m_nBufferLength    = 0;
  private int            m_nBytesReceived  = 0;
  private byte []          m_Buffer      = null;

  private Handler          m_Handler      = null;
  private BluetoothAdapter    m_Adapter      = null;
  private BluetoothDevice      m_Device      = null;
  private RunEncoder        m_Encoder      = null;




  public ClientThread (BluetoothDevice device, Handler handler)
  {
    super ();

    m_nBatteryIndicator  = 0;
    m_nHeartRate    = 0;
    m_bActive      = false;

    m_Adapter      = BluetoothAdapter.getDefaultAdapter ();
    m_Device      = device;
    m_Handler      = handler;

    m_nBufferLength    = 0;
    m_nBytesReceived  = 0;
    m_Buffer      = new byte [Constants.MAX_BUFFER_LENGTH];

    m_Encoder = new RunEncoder ();
  }




  @SuppressWarnings({ "rawtypes", "unchecked" })
  @Override
  public void run ()
  {
    int        iResult        = 0;
    int        batteryIndicator  = 0;
    int        heartRate      = 0;
    int        length        = 0;
    int        read        = 0;
    byte []      buffer        = null;
    
    Class      deviceClass      = null;
    Class []    deviceClassArray  = null;
    Method      method        = null;
    Object []    objectArray      = null;

    BluetoothSocket  socket        = null;
    InputStream    inputStream      = null;


    m_bActive = true;

    deviceClass        = m_Device.getClass ();
    deviceClassArray    = new Class [1];
    deviceClassArray [0]  = Integer.TYPE;

    try { method = deviceClass.getMethod (CREATE_SOCKET_METHOD, deviceClassArray); }
    catch (SecurityException    exception) { method = null; }
    catch (NoSuchMethodException  exception) { method = null; }

    if (method != null)
    {
      objectArray    = new Object[1];
      objectArray [0]  = Integer.valueOf (1);

      try { socket = (BluetoothSocket) method.invoke (m_Device, objectArray); }
      catch (IllegalArgumentException    exception) { socket = null; }
      catch (IllegalAccessException    exception) { socket = null; }
      catch (InvocationTargetException  exception) { socket = null; }
    }

    if (socket != null)
    {
      m_Handler.sendEmptyMessage (Constants.CONNECTING);

      m_Adapter.cancelDiscovery ();

      try { socket.connect (); }
      catch (IOException exception) { socket = null; }

      if (socket != null)
      {
        m_Handler.sendEmptyMessage (Constants.CONNECTED);

        try { inputStream = socket.getInputStream (); }
        catch (IOException exception) { inputStream = null; }
      }
      else { m_Handler.sendEmptyMessage (Constants.CONNECTION_FAILED); }
    }

    if (inputStream != null)
    {
      iResult = m_Encoder.start ();
      if (iResult < 0) { m_Handler.sendEmptyMessage (Constants.RUN_FILE_FAILED); }

      while (m_bActive)
      {
        while (m_bActive)
        {
          buffer = new byte [Constants.MAX_BUFFER_LENGTH];
          try { read = inputStream.read (buffer); }
          catch (IOException exception) { break; }

          if ((m_nBufferLength + read) < Constants.MAX_BUFFER_LENGTH)
          {
            System.arraycopy (buffer, 0, m_Buffer, m_nBufferLength, read);
            m_nBufferLength    += read;
            m_nBytesReceived  += read;
          }

          detectSoF ();


          // If not enough bytes present, do one more read

          if (m_nBufferLength < 6)
          {
            buffer = new byte [Constants.MAX_BUFFER_LENGTH];
            try { read = inputStream.read (buffer); }
            catch (IOException exception) { break; }

            if ((m_nBufferLength + read) < Constants.MAX_BUFFER_LENGTH)
            {
              System.arraycopy (buffer, 0, m_Buffer, m_nBufferLength, read);
              m_nBufferLength    += read;
              m_nBytesReceived  += read;
            }

            detectSoF ();
          }


          // Update display

          if (m_nBytesReceived > 999) { m_nBytesReceived = 0; }
          m_Handler.sendEmptyMessage (Constants.BYTES_RECEIVED);


          // If enough bytes present, process frame

          while (m_nBufferLength >= 6)
          {
            length = (0xFF & m_Buffer [1]);

            batteryIndicator = (0xFF & m_Buffer [4]);
            if (batteryIndicator != m_nBatteryIndicator)
            {
              m_nBatteryIndicator = batteryIndicator;
              m_Handler.sendEmptyMessage (Constants.BATTERY_UPDATE);
            }

            heartRate = (0xFF & m_Buffer [5]);
            if (heartRate != m_nHeartRate)
            {
              m_nHeartRate = heartRate;
              m_Handler.sendEmptyMessage (Constants.HEART_RATE_UPDATE);

              iResult = m_Encoder.add (CHANNEL_ANALOG_OUTPUT_6, m_nHeartRate);
              if (iResult >= 0) { m_Encoder.encode (); }
            }

            m_nBufferLength  -= length;
          }
        }
      }

      if (inputStream != null)
      {
        try { inputStream.close (); }
        catch (IOException exception) { }

        inputStream = null;
      }

      if (socket != null)
      {
        try { socket.close (); }
        catch (IOException exception) { }

        socket = null;
      }

      m_Encoder.stop ();
    }

    if (m_bActive)
    {
      m_bActive = false;
      m_Handler.sendEmptyMessage (Constants.CONNECTION_LOST);
    }
  }




  private void detectSoF ()
  {
    int indexSoF = 0;


    indexSoF = 0;
    while ((indexSoF < m_nBufferLength) && ((0xfe & m_Buffer [indexSoF + 1]) == 0xfe)) { indexSoF ++; }

    
    if (indexSoF >= m_nBufferLength) { m_nBufferLength = 0; }  // No SoF - discard buffer
    else if (indexSoF > 0)
    {
      System.arraycopy (m_Buffer, indexSoF, m_Buffer, 0, (m_nBufferLength - indexSoF));
      m_nBufferLength -= indexSoF;
    }
  }




  // Generated getters / setters

  public boolean isActive() {
    return m_bActive;
  }

  public void setActive(boolean value) {
    this.m_bActive = value;
  }

  public int getBatteryIndicator() {
    return m_nBatteryIndicator;
  }

  public int getHeartRate() {
    return m_nHeartRate;
  }

  public int getReceivedBytes() {
    return m_nBytesReceived;
  }
}




Java Source Code List

com.facebook.android.AsyncFacebookRunner.java
com.facebook.android.BaseDialogListener.java
com.facebook.android.BaseRequestListener.java
com.facebook.android.DialogError.java
com.facebook.android.FQLQuery.java
com.facebook.android.FacebookError.java
com.facebook.android.FacebookFunctions.java
com.facebook.android.Facebook.java
com.facebook.android.FbDialog.java
com.facebook.android.FieldsConnectionsDialog.java
com.facebook.android.FriendsGetProfilePics.java
com.facebook.android.FriendsList.java
com.facebook.android.GraphExplorer.java
com.facebook.android.IntentUriHandler.java
com.facebook.android.LoginButton.java
com.facebook.android.PermissionsDialog.java
com.facebook.android.Places.java
com.facebook.android.SessionEvents.java
com.facebook.android.SessionStore.java
com.facebook.android.TokenRefreshDialog.java
com.facebook.android.UpdateStatusResultDialog.java
com.facebook.android.UploadPhotoResultDialog.java
com.facebook.android.Util.java
com.facebook.android.Utility.java
com.googlecode.android.widgets.DateSlider.AlternativeDateSlider.java
com.googlecode.android.widgets.DateSlider.CustomDateSlider.java
com.googlecode.android.widgets.DateSlider.DateSlider.java
com.googlecode.android.widgets.DateSlider.DateTimeSlider.java
com.googlecode.android.widgets.DateSlider.DefaultDateSlider.java
com.googlecode.android.widgets.DateSlider.MonthYearDateSlider.java
com.googlecode.android.widgets.DateSlider.ScrollLayout.java
com.googlecode.android.widgets.DateSlider.TimeSlider.java
com.googlecode.android.widgets.DateSlider.TimeView.java
com.renaultsport.heartrate.ActivityMain.java
com.renaultsport.heartrate.ActivityRecord.java
com.renaultsport.heartrate.ActivitySplash.java
com.renaultsport.heartrate.ActivityUpload.java
com.renaultsport.heartrate.ActivityVideoReader.java
com.renaultsport.heartrate.ActivityWarning.java
com.renaultsport.heartrate.MainApplication.java
com.renaultsport.heartrate.utils.CaptureCamera.java
com.renaultsport.heartrate.utils.ClientThread.java
com.renaultsport.heartrate.utils.Constants.java
com.renaultsport.heartrate.utils.RunEncoder.java
team.stride.tabs.TabGroupActivity.java