Back to project page rsmonitor-heartrate.
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.
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; } }