Darien Figueroa / Mbed OS Final_Program

Dependencies:   USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SensorActivity.java Source File

SensorActivity.java

00001 /*******************************************************************************
00002  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00003  * <p>
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  * <p>
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  * <p>
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  * <p>
00022  * Except as contained in this notice, the name of Maxim Integrated
00023  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024  * Products, Inc. Branding Policy.
00025  * <p>
00026  * The mere transfer of this software does not imply any licenses
00027  * of trade secrets, proprietary technology, copyrights, patents,
00028  * trademarks, maskwork rights, or any other form of intellectual
00029  * property whatsoever. Maxim Integrated Products, Inc. retains all
00030  * ownership rights.
00031  * ******************************************************************************
00032  */
00033 package com.example.android.bluetoothlegatt;
00034 
00035 import android.app.Activity;
00036 import android.bluetooth.BluetoothDevice;
00037 import android.bluetooth.BluetoothGattCharacteristic;
00038 import android.bluetooth.BluetoothGattService;
00039 import android.content.BroadcastReceiver;
00040 import android.content.ComponentName;
00041 import android.content.Context;
00042 import android.content.Intent;
00043 import android.content.IntentFilter;
00044 import android.content.ServiceConnection;
00045 import android.graphics.Color;
00046 import android.os.Bundle;
00047 import android.os.IBinder;
00048 import android.view.Menu;
00049 import android.view.MenuItem;
00050 import android.view.View;
00051 import android.widget.Button;
00052 import android.widget.TextView;
00053 
00054 import java.nio.ByteBuffer;
00055 import java.nio.ByteOrder;
00056 import java.util.ArrayList;
00057 import java.util.HashMap;
00058 import java.util.List;
00059 import java.util.Map;
00060 import java.util.UUID;
00061 
00062 import graphing.LineGraphing;
00063 import graphing.LinePointCollection;
00064 
00065 public class SensorActivity extends Activity {
00066     /// define intent strings used the pass in data from other activities and services
00067     public static final String EXTRA_DEVICE = "extra_device";
00068     private static final String LIST_NAME = "NAME";
00069     private static final String LIST_UUID = "UUID";
00070 
00071     /// controls
00072     protected TextView mConnectionState;
00073     protected Button mBtnTest;
00074     protected TextView mTextTempTop;
00075     protected TextView mTextTempTopRaw;
00076     protected TextView mTextTempBottom;
00077     protected TextView mTextTempBottomRaw;
00078     protected TextView mTextLIS2DH_X;
00079     protected TextView mTextLIS2DH_Y;
00080     protected TextView mTextLIS2DH_Z;
00081     protected TextView mTextLIS2DHRaw;
00082     protected TextView mTextPressure;
00083     protected TextView mTextTemperature;
00084     protected TextView mTextPressureRaw;
00085     protected TextView mTextHeartRate;
00086     protected TextView mTextHeartRateRaw;
00087     protected Button mBtnSubscribe;
00088     protected TextView mClickStartBtn;
00089     protected TextView mClickStartBtn2;
00090     protected TextView mAccelMissionStopped;
00091     protected TextView mECGMissionStopped;
00092 
00093     /// accelerometer
00094     protected LineGraphing lineGraphingAccelerometer;
00095     LinePointCollection linePointCollectionX;
00096     LinePointCollection linePointCollectionY;
00097     LinePointCollection linePointCollectionZ;
00098 
00099     /// pressure
00100     protected LineGraphing lineGraphingPressure;
00101     LinePointCollection linePointCollectionPressure;
00102 
00103     /// temperature 1
00104     protected LineGraphing lineGraphingTemperature1;
00105     LinePointCollection linePointCollectionTemperature1;
00106 
00107     /// temperature 2
00108     protected LineGraphing lineGraphingTemperature2;
00109     LinePointCollection linePointCollectionTemperature2;
00110 
00111     /// heartrate
00112     protected LineGraphing lineGraphingHeartrate;
00113     LinePointCollection linePointCollectionHeartrate;
00114 
00115     /// define variables of values that we care about
00116     private String mDeviceName;
00117     private String mDeviceAddress;
00118     private boolean mConnected = false;
00119     private String mExportString;
00120     private BluetoothGattCharacteristic mNotifyCharacteristic;
00121     private BluetoothLeService mBluetoothLeService;
00122     private List<List<BluetoothGattCharacteristic>> mGattCharacteristics = new ArrayList<>();
00123 
00124     /// bool used to determine if we should enable the mission start button
00125     boolean isMissionButtonEnabled = true;
00126 
00127     /// define the UUIDs of the HSP BLE service and characteristics
00128     public final static String BLE_MAXIM_HSP_SERVICE = "5c6e40e8-3b7f-4286-a52f-daec46abe851"; // hsp service
00129     public final static String BLE_MAXIM_HSP_TEMPERATURE_TOP_CHARACTERISTIC = "3544531b-00c3-4342-9755-b56abe8e6c67"; // temp top
00130     public final static String BLE_MAXIM_HSP_TEMPERATURE_BOTTOM_CHARACTERISTIC = "3544531b-00c3-4342-9755-b56abe8e6a66"; // temp bottom
00131     public final static String BLE_MAXIM_HSP_ACCEL_CHARACTERISTIC = "e6c9da1a-8096-48bc-83a4-3fca383705af"; //accel
00132     public final static String BLE_MAXIM_HSP_PRESSURE_CHARACTERISTIC = "1d8a1932-da49-49ad-91d8-800832e7e940";  // pressure
00133     public final static String BLE_MAXIM_HSP_HEARTRATE_CHARACTERISTIC = "628e00e3-b093-46bf-aadc-abe4c648c569";  // heart rate
00134   //  OG  public final static String BLE_MAXIM_HSP_HEARTRATE_CHARACTERISTIC = "621a00e3-b093-46bf-aadc-abe4c648c569";  // heart rate
00135     public final static String BLE_MAXIM_HSP_COMMAND_CHARACTERISTIC = "36e55e37-6b5b-420b-9107-0d34a0e8675a"; // command
00136 
00137     /**
00138      * Subscribe to all of the HSP characteristics that we care about
00139      */
00140     private void subscribeCharacteristics() {
00141         for (List<BluetoothGattCharacteristic> chars : mGattCharacteristics) {
00142             for (BluetoothGattCharacteristic cha : chars) {
00143                 if (cha.getService().getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_SERVICE)) == 0) {
00144                     if (cha.getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_TEMPERATURE_TOP_CHARACTERISTIC)) == 0) {
00145                         mBluetoothLeService.Subscribe(cha.getService().getUuid(), cha.getUuid());
00146                     }
00147                     if (cha.getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_TEMPERATURE_BOTTOM_CHARACTERISTIC)) == 0) {
00148                         mBluetoothLeService.Subscribe(cha.getService().getUuid(), cha.getUuid());
00149                     }
00150                     if (cha.getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_ACCEL_CHARACTERISTIC)) == 0) {
00151                         mBluetoothLeService.Subscribe(cha.getService().getUuid(), cha.getUuid());
00152                     }
00153                     if (cha.getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_HEARTRATE_CHARACTERISTIC)) == 0) {
00154                         mBluetoothLeService.Subscribe(cha.getService().getUuid(), cha.getUuid());
00155                     }
00156                     if (cha.getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_PRESSURE_CHARACTERISTIC)) == 0) {
00157                         mBluetoothLeService.Subscribe(cha.getService().getUuid(), cha.getUuid());
00158                     }
00159                 }
00160             }
00161         }
00162     }
00163 
00164     /**
00165      * Called from the activity framework when the activity is created
00166      */
00167     @Override
00168     protected void onCreate(Bundle savedInstanceState) {
00169         super.onCreate(savedInstanceState);
00170         setContentView(R.layout.activity_sensor);
00171 
00172         // bind the misc TextView fields
00173         mConnectionState = (TextView) findViewById(R.id.connection_state);
00174         mBtnTest = (Button) findViewById(R.id.btnTest);
00175         mTextTempTop = (TextView) findViewById(R.id.textTempTop);
00176         mTextTempTopRaw = (TextView) findViewById(R.id.textTempTopRaw);
00177         mTextTempBottom = (TextView) findViewById(R.id.textTempBottom);
00178         mTextTempBottomRaw = (TextView) findViewById(R.id.textTempBottomRaw);
00179         mTextLIS2DH_X = (TextView) findViewById(R.id.textLIS2DH_X);
00180         mTextLIS2DH_Y = (TextView) findViewById(R.id.textLIS2DH_Y);
00181         mTextLIS2DH_Z = (TextView) findViewById(R.id.textLIS2DH_Z);
00182         mTextLIS2DHRaw = (TextView) findViewById(R.id.textLIS2DHRaw);
00183         mTextPressure = (TextView) findViewById(R.id.textPressure);
00184         mTextTemperature = (TextView) findViewById(R.id.textTemperature);
00185         mTextPressureRaw = (TextView) findViewById(R.id.textPressureRaw);
00186         mTextHeartRate = (TextView) findViewById(R.id.textHeartRate);
00187         mTextHeartRateRaw = (TextView) findViewById(R.id.textHeartRateRaw);
00188         mBtnSubscribe = (Button) findViewById(R.id.btnSubscribe);
00189         mClickStartBtn = (TextView) findViewById(R.id.clickStartBtn);
00190         mClickStartBtn2 = (TextView) findViewById(R.id.clickStartBtn2);
00191         mAccelMissionStopped = (TextView) findViewById(R.id.textView7);
00192         mECGMissionStopped = (TextView) findViewById(R.id.textView8);
00193         /// accelerometer
00194         lineGraphingAccelerometer = (LineGraphing) findViewById(R.id.lineGraphingAccelerometer);
00195         /// pressure
00196         lineGraphingPressure = (LineGraphing) findViewById(R.id.lineGraphingPressure);
00197         /// temperature 1
00198         lineGraphingTemperature1 = (LineGraphing) findViewById(R.id.lineGraphingTemperature1);
00199         /// temperature 2
00200         lineGraphingTemperature2 = (LineGraphing) findViewById(R.id.lineGraphingTemperature2);
00201         /// heartrate
00202         lineGraphingHeartrate = (LineGraphing) findViewById(R.id.lineGraphingHeartrate);
00203 
00204         mBtnSubscribe.setOnClickListener(new View.OnClickListener() {
00205             @Override
00206             public void onClick(View view) {
00207                 //mBluetoothLeService.connect(mDeviceAddress);
00208             }
00209         });
00210 
00211         mClickStartBtn2.setOnClickListener(new View.OnClickListener() {
00212             @Override
00213             public void onClick(View view) {
00214                 missionClick();
00215             }
00216         });
00217 
00218         final Intent intent = getIntent();
00219         final BluetoothDevice device = intent.getParcelableExtra(EXTRA_DEVICE);
00220         mDeviceName = device.getName();
00221         mDeviceAddress = device.getAddress();
00222         ((TextView) findViewById(R.id.device_address)).setText(mDeviceAddress);
00223 
00224         //getSupportActionBar().setTitle(mDeviceName);
00225         //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
00226 
00227         final Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
00228         bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
00229 
00230         // setup the accelerometer linegraph collections
00231         linePointCollectionX = new LinePointCollection(Color.RED, 20);
00232         linePointCollectionY = new LinePointCollection(Color.GREEN, 20);
00233         linePointCollectionZ = new LinePointCollection(Color.BLUE, 20);
00234         lineGraphingAccelerometer.addPointCollection(linePointCollectionX);
00235         lineGraphingAccelerometer.addPointCollection(linePointCollectionY);
00236         lineGraphingAccelerometer.addPointCollection(linePointCollectionZ);
00237 
00238         // setup the pressure linegraph collections
00239         linePointCollectionPressure = new LinePointCollection(Color.BLACK, 20);
00240         lineGraphingPressure.addPointCollection(linePointCollectionPressure);
00241 
00242         // setup the temperature 1 linegraph collections
00243         linePointCollectionTemperature1 = new LinePointCollection(Color.BLACK, 20);
00244         lineGraphingTemperature1.addPointCollection(linePointCollectionTemperature1);
00245 
00246         // setup the temperature 2 linegraph collections
00247         linePointCollectionTemperature1 = new LinePointCollection(Color.BLACK, 20);
00248         lineGraphingTemperature1.addPointCollection(linePointCollectionTemperature1);
00249 
00250         linePointCollectionTemperature2 = new LinePointCollection(Color.BLACK, 20);
00251         lineGraphingTemperature2.addPointCollection(linePointCollectionTemperature2);
00252 
00253         linePointCollectionHeartrate = new LinePointCollection(Color.BLACK, 20);
00254         lineGraphingHeartrate.addPointCollection(linePointCollectionHeartrate);
00255 
00256 
00257 
00258     }
00259 
00260     /**
00261      * Called from the activity framework when the activity is paused
00262      */
00263     @Override
00264     protected void onPause() {
00265         super.onPause();
00266         unregisterReceiver(mGattUpdateReceiver);
00267     }
00268 
00269     /**
00270      * Called from the activity framework when the activity is resuming
00271      */
00272     @Override
00273     protected void onResume() {
00274         super.onResume();
00275         registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
00276         if (mBluetoothLeService != null) {
00277             final boolean result = mBluetoothLeService.connect(mDeviceAddress);
00278         }
00279     }
00280 
00281     /**
00282      * Define an intent filter of actions to respond to
00283      * @return Returns the intent filter that is created
00284      */
00285     private static IntentFilter makeGattUpdateIntentFilter() {
00286         final IntentFilter intentFilter = new IntentFilter();
00287         intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
00288         intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
00289         intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
00290         intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
00291         intentFilter.addAction(BluetoothLeService.ACTION_CHARACTERISTIC_WRITE);
00292         return intentFilter;
00293     }
00294 
00295     // Code to manage Service lifecycle.
00296     private final ServiceConnection mServiceConnection = new ServiceConnection() {
00297         @Override
00298         public void onServiceConnected(final ComponentName componentName, final IBinder service) {
00299             mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
00300             if (!mBluetoothLeService.initialize()) {
00301                 finish();
00302             }
00303             // Automatically connects to the device upon successful start-up initialization.
00304             mBluetoothLeService.connect(mDeviceAddress);
00305         }
00306 
00307         @Override
00308         public void onServiceDisconnected(final ComponentName componentName) {
00309             mBluetoothLeService = null;
00310             //finish();
00311         }
00312     };
00313 
00314     /**
00315      * Handles various events fired by the Service.
00316      */
00317     private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
00318         @Override
00319         public void onReceive(final Context context, final Intent intent) {
00320             final String action = intent.getAction();
00321             if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
00322                 mConnected = true;
00323                 updateConnectionState(R.string.connected);
00324                 invalidateOptionsMenu();
00325             } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
00326                 mConnected = false;
00327                 updateConnectionState(R.string.disconnected);
00328                 invalidateOptionsMenu();
00329                 finish();   // get out of this activity and back to the parent
00330             } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
00331                 // Show all the supported services and characteristics on the user interface.
00332                 displayGattServices(mBluetoothLeService.getSupportedGattServices());
00333                 subscribeCharacteristics();
00334             } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
00335                 final String noData = getString(R.string.no_data);
00336                 final String uuid = intent.getStringExtra(BluetoothLeService.EXTRA_UUID_CHAR);
00337                 final byte[] dataArr = intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA_RAW);
00338                 if (uuid != null) {
00339                     UpdateUI(UUID.fromString(uuid), dataArr);
00340                 }
00341             } else if (BluetoothLeService.ACTION_CHARACTERISTIC_WRITE.equals(action)) {
00342                 final String uuid = intent.getStringExtra(BluetoothLeService.EXTRA_UUID_CHAR);
00343                 if (uuid.equalsIgnoreCase(BLE_MAXIM_HSP_COMMAND_CHARACTERISTIC)) {
00344                     EnableMissionButton(true);
00345                 }
00346             }
00347         }
00348     };
00349 
00350     /**
00351      * Enable or disable the Start Mission Button based on HSP state variable
00352      * @param state Enabled or disabled status
00353      */
00354     private void EnableMissionButton(boolean state) {
00355         isMissionButtonEnabled = state;
00356         if (state) {
00357             mClickStartBtn2.setBackgroundColor(Color.BLACK);
00358         } else {
00359             mClickStartBtn2.setBackgroundColor(Color.LTGRAY);
00360         }
00361     }
00362 
00363     /**
00364      * Update the connection status
00365      * @param resourceId Resource id that is bound to the status test view field
00366      */
00367     private void updateConnectionState(final int resourceId) {
00368         runOnUiThread(new Runnable() {
00369             @Override
00370             public void run() {
00371                 final int colourId;
00372 
00373                 switch (resourceId) {
00374                     case R.string.connected:
00375                         colourId = android.R.color.holo_green_dark;
00376                         break;
00377                     case R.string.disconnected:
00378                         colourId = android.R.color.holo_red_dark;
00379                         break;
00380                     default:
00381                         colourId = android.R.color.black;
00382                         break;
00383                 }
00384 
00385                 mConnectionState.setText(resourceId);
00386                 mConnectionState.setTextColor(getResources().getColor(colourId));
00387             }
00388         });
00389     }
00390 
00391     /**
00392      * Convert the temperature from celsius to fahrenheit
00393      * @param celsius temperature to convert
00394      * @return converted temperature in fahrenheit
00395      */
00396     private float ToFahrenheit(float celsius) {
00397         return celsius * 9 / 5 + 32;
00398     }
00399 
00400     /**
00401      * Convert the incoming byte data array into a temperature
00402      * @param data Data array containing two elements
00403      * @return temperature as a float in celcius
00404      */
00405     private float ToTemperature(byte[] data) {
00406         float value;
00407         value = (float) data[1] + (float) data[0] / 256.0f;
00408         return value;
00409     }
00410 
00411     /**
00412      * Convert the portion of the byte array into a float
00413      * @param data Array to convert, a part of this array is converted based on a offset
00414      * @param offset Offset into the array to extract the float
00415      * @return Returns the float that was converted
00416      */
00417     private float PartialArrayToFloat(byte[] data, int offset) {
00418         short value = (short) ((short) (data[offset + 1] << 8) + (short) data[offset]);
00419         return (float) value;
00420     }
00421 
00422     /**
00423      * Calculate the heart rate from the ECG RtoR device
00424      * @param data Incoming data to convert
00425      * @return Returns a float representing the heart rate
00426      */
00427     private float CalculateHeartRate(byte[] data) {
00428         float t = 8.0f;
00429         float value = 0;
00430         float RtoR = (float) ((int) data[1] << 8) + (float) data[0];
00431         float fmStr = (float) ((int) data[3] << 8) + (float) data[2];
00432         if (fmStr == 0.0f) t = 7.813f;
00433         if (RtoR > 0.0f) {
00434             value = 60000.0f / (RtoR * t);
00435         }
00436         return value;
00437     }
00438 
00439     /**
00440      * Update the UI for a given characteristic uuid using data
00441      * This will update all of the text view values for the given characteristic
00442      * It also will graph the incoming data
00443      * @param characteristic Characteristic to update with given data
00444      * @param data Data used to update the UI
00445      */
00446     private void UpdateUI(UUID characteristic, byte[] data) {
00447         float x = 0;
00448         float y = 0;
00449         float z = 0;
00450         float value = 0;
00451         String str = "";
00452         str = bytesToHex(data);
00453         // temperature top
00454         if (characteristic.compareTo(UUID.fromString(BLE_MAXIM_HSP_TEMPERATURE_TOP_CHARACTERISTIC)) == 0) {
00455             mTextTempTopRaw.setText(str);
00456             value = ToFahrenheit(ToTemperature(data));
00457             mTextTempTop.setText(String.format("%.1f", value) + "F");
00458             linePointCollectionTemperature1.addpoint(value);
00459             lineGraphingTemperature1.plotPointCollection();
00460         }
00461         // temperature bottom
00462         if (characteristic.compareTo(UUID.fromString(BLE_MAXIM_HSP_TEMPERATURE_BOTTOM_CHARACTERISTIC)) == 0) {
00463             mTextTempBottomRaw.setText(str);
00464             value = ToFahrenheit(ToTemperature(data));
00465             mTextTempBottom.setText(String.format("%.1f", value) + "F");
00466             linePointCollectionTemperature2.addpoint(value);
00467             lineGraphingTemperature2.plotPointCollection();
00468         }
00469         // acceleration
00470         if (characteristic.compareTo(UUID.fromString(BLE_MAXIM_HSP_ACCEL_CHARACTERISTIC)) == 0) {
00471             mTextLIS2DHRaw.setText(str);
00472             x = PartialArrayToFloat(data, 0);
00473             y = PartialArrayToFloat(data, 2);
00474             z = PartialArrayToFloat(data, 4);
00475             mTextLIS2DH_X.setText(String.format("X: %.1f", x));
00476             mTextLIS2DH_Y.setText(String.format("Y: %.1f", y));
00477             mTextLIS2DH_Z.setText(String.format("Z: %.1f", z));
00478             linePointCollectionX.addpoint(x);
00479             linePointCollectionY.addpoint(y);
00480             linePointCollectionZ.addpoint(z);
00481             lineGraphingAccelerometer.plotPointCollection();
00482         }
00483         // pressure
00484         if (characteristic.compareTo(UUID.fromString(BLE_MAXIM_HSP_PRESSURE_CHARACTERISTIC)) == 0) {
00485             String pressureStr = "";
00486             int n = 0;
00487             int bits;
00488             byte[] arr = {data[0], data[1], data[2], data[3]};
00489             ByteBuffer bb = ByteBuffer.wrap(arr);
00490             bb.order(ByteOrder.LITTLE_ENDIAN);
00491             bits = bb.getInt();
00492             float temperature = Float.intBitsToFloat(bits);
00493             n = 4;
00494             //bits = data[n] | data[n+1]<<8 | data[n+2]<<16 | data[n+3]<<24;
00495             byte[] arr2 = {data[4], data[5], data[6], data[7]};
00496             ByteBuffer bb2 = ByteBuffer.wrap(arr2);
00497             bb2.order(ByteOrder.LITTLE_ENDIAN);
00498             bits = bb2.getInt();
00499             float pressure = Float.intBitsToFloat(bits);
00500             mTextPressure.setText(String.format("%.1f", pressure) + "Pa");
00501             linePointCollectionPressure.addpoint(pressure);
00502             lineGraphingPressure.plotPointCollection();
00503         }
00504         // heartrate
00505         if (characteristic.compareTo(UUID.fromString(BLE_MAXIM_HSP_HEARTRATE_CHARACTERISTIC)) == 0) {
00506             mTextHeartRateRaw.setText(str);
00507             value = CalculateHeartRate(data);
00508             mTextHeartRate.setText(String.format("%.1f", value));
00509             linePointCollectionHeartrate.addpoint(value);
00510             lineGraphingHeartrate.plotPointCollection();
00511         }
00512     }
00513 
00514     /// array of hex ascii values used for hex byte to string conversion
00515     final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
00516 
00517     /**
00518      * Convert a series of bytes within a given array into a string
00519      * @param bytes Arbitrary array of bytes to convert
00520      * @return String is returned containing the bytes as hex ascii values
00521      */
00522     public static String bytesToHex(byte[] bytes) {
00523         char[] hexChars = new char[bytes.length * 2];
00524         for (int j = 0; j < bytes.length; j++) {
00525             int v = bytes[j] & 0xFF;
00526             hexChars[j * 2] = hexArray[v >>> 4];
00527             hexChars[j * 2 + 1] = hexArray[v & 0x0F];
00528         }
00529         return new String(hexChars);
00530     }
00531 
00532     /**
00533      *  Demonstrates how to iterate through the supported GATT Services/Characteristics.
00534      * In this sample, we populate the data structure that is bound to the ExpandableListView
00535      * on the UI.
00536      * @param gattServices List of services to parse and display
00537      */
00538     private void displayGattServices(final List<BluetoothGattService> gattServices) {
00539         if (gattServices == null) return;
00540 
00541         String uuid = null;
00542         final String unknownServiceString = getResources().getString(R.string.unknown_service);
00543         final String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
00544         final List<Map<String, String>> gattServiceData = new ArrayList<>();
00545         final List<List<Map<String, String>>> gattCharacteristicData = new ArrayList<>();
00546         mGattCharacteristics = new ArrayList<>();
00547 
00548         // Loops through available GATT Services.
00549         for (final BluetoothGattService gattService : gattServices) {
00550             final Map<String, String> currentServiceData = new HashMap<>();
00551             uuid = gattService.getUuid().toString();
00552             currentServiceData.put(LIST_NAME, GattAttributeLookupTable.getAttributeName(uuid, unknownServiceString));
00553             currentServiceData.put(LIST_UUID, uuid);
00554             gattServiceData.add(currentServiceData);
00555 
00556             final List<Map<String, String>> gattCharacteristicGroupData = new ArrayList<>();
00557             final List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
00558             final List<BluetoothGattCharacteristic> charas = new ArrayList<>();
00559 
00560             // Loops through available Characteristics.
00561             for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
00562                 charas.add(gattCharacteristic);
00563                 final Map<String, String> currentCharaData = new HashMap<>();
00564                 uuid = gattCharacteristic.getUuid().toString();
00565                 currentCharaData.put(LIST_NAME, GattAttributeLookupTable.getAttributeName(uuid, unknownCharaString));
00566                 currentCharaData.put(LIST_UUID, uuid);
00567                 gattCharacteristicGroupData.add(currentCharaData);
00568             }
00569 
00570             mGattCharacteristics.add(charas);
00571             gattCharacteristicData.add(gattCharacteristicGroupData);
00572         }
00573     }
00574 
00575     /**
00576      * Given a TextView, output a string plus units concatenated
00577      * @param textView View to set
00578      * @param value String representing the value
00579      * @param units String representing the units
00580      */
00581     private void SetSensorText(TextView textView, String value, String units) {
00582         textView.setText(value + units);
00583     }
00584 
00585     /**
00586      * Create the menu for the options
00587      * @param menu the menu to set the options for
00588      * @return true if created
00589      */
00590     @Override
00591     public boolean onCreateOptionsMenu(final Menu menu) {
00592         getMenuInflater().inflate(R.menu.gatt_services, menu);
00593         if (mConnected) {
00594             menu.findItem(R.id.menu_connect).setVisible(false);
00595             menu.findItem(R.id.menu_disconnect).setVisible(true);
00596         } else {
00597             menu.findItem(R.id.menu_connect).setVisible(true);
00598             menu.findItem(R.id.menu_disconnect).setVisible(false);
00599         }
00600         return true;
00601     }
00602 
00603     /**
00604      * Called when the user selects a menu item
00605      * @param item Selected menu item
00606      * @return returns true if the selection was handled
00607      */
00608     @Override
00609     public boolean onOptionsItemSelected(final MenuItem item) {
00610         switch (item.getItemId()) {
00611             case R.id.menu_connect:
00612                 mBluetoothLeService.connect(mDeviceAddress);
00613                 return true;
00614             case R.id.menu_disconnect:
00615                 mBluetoothLeService.disconnect();
00616                 return true;
00617             case android.R.id.home:
00618                 mBluetoothLeService.disconnect();
00619                 return true;
00620         }
00621         return super.onOptionsItemSelected(item);
00622     }
00623 
00624     /**
00625      * Mission button clicked
00626      * @param v The handle of the view clicked
00627      */
00628     public void onClick(View v) {
00629         missionClick();
00630     }
00631 
00632     /**
00633      * Start or stop the mission
00634      */
00635     private void missionClick() {
00636         byte[] data = new byte[1];
00637         //if (isMissionButtonEnabled == false) return;
00638 
00639         if (mClickStartBtn2.getText().toString().equalsIgnoreCase("Stop Mission") == true) {
00640             // stop
00641             data[0] = 0x00;
00642             mBluetoothLeService.writeCharacteristic(UUID.fromString(BLE_MAXIM_HSP_SERVICE), UUID.fromString(BLE_MAXIM_HSP_COMMAND_CHARACTERISTIC), data);
00643             mClickStartBtn2.setText("Start Mission");
00644             EnableMissionButton(false);
00645         } else {
00646             // start
00647             data[0] = 0x01;
00648             mBluetoothLeService.writeCharacteristic(UUID.fromString(BLE_MAXIM_HSP_SERVICE), UUID.fromString(BLE_MAXIM_HSP_COMMAND_CHARACTERISTIC), data);
00649             mClickStartBtn2.setText("Stop Mission");
00650             EnableMissionButton(false);
00651         }
00652     }
00653 
00654 }