Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MAX14720 MAX30205 USBDevice
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 = "621a00e3-b093-46bf-aadc-abe4c648c569"; // heart rate 00134 public final static String BLE_MAXIM_HSP_COMMAND_CHARACTERISTIC = "36e55e37-6b5b-420b-9107-0d34a0e8675a"; // command 00135 00136 /** 00137 * Subscribe to all of the HSP characteristics that we care about 00138 */ 00139 private void subscribeCharacteristics() { 00140 for (List<BluetoothGattCharacteristic> chars : mGattCharacteristics) { 00141 for (BluetoothGattCharacteristic cha : chars) { 00142 if (cha.getService().getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_SERVICE)) == 0) { 00143 if (cha.getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_TEMPERATURE_TOP_CHARACTERISTIC)) == 0) { 00144 mBluetoothLeService.Subscribe(cha.getService().getUuid(), cha.getUuid()); 00145 } 00146 if (cha.getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_TEMPERATURE_BOTTOM_CHARACTERISTIC)) == 0) { 00147 mBluetoothLeService.Subscribe(cha.getService().getUuid(), cha.getUuid()); 00148 } 00149 if (cha.getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_ACCEL_CHARACTERISTIC)) == 0) { 00150 mBluetoothLeService.Subscribe(cha.getService().getUuid(), cha.getUuid()); 00151 } 00152 if (cha.getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_HEARTRATE_CHARACTERISTIC)) == 0) { 00153 mBluetoothLeService.Subscribe(cha.getService().getUuid(), cha.getUuid()); 00154 } 00155 if (cha.getUuid().compareTo(UUID.fromString(BLE_MAXIM_HSP_PRESSURE_CHARACTERISTIC)) == 0) { 00156 mBluetoothLeService.Subscribe(cha.getService().getUuid(), cha.getUuid()); 00157 } 00158 } 00159 } 00160 } 00161 } 00162 00163 /** 00164 * Called from the activity framework when the activity is created 00165 */ 00166 @Override 00167 protected void onCreate(Bundle savedInstanceState) { 00168 super.onCreate(savedInstanceState); 00169 setContentView(R.layout.activity_sensor); 00170 00171 // bind the misc TextView fields 00172 mConnectionState = (TextView) findViewById(R.id.connection_state); 00173 mBtnTest = (Button) findViewById(R.id.btnTest); 00174 mTextTempTop = (TextView) findViewById(R.id.textTempTop); 00175 mTextTempTopRaw = (TextView) findViewById(R.id.textTempTopRaw); 00176 mTextTempBottom = (TextView) findViewById(R.id.textTempBottom); 00177 mTextTempBottomRaw = (TextView) findViewById(R.id.textTempBottomRaw); 00178 mTextLIS2DH_X = (TextView) findViewById(R.id.textLIS2DH_X); 00179 mTextLIS2DH_Y = (TextView) findViewById(R.id.textLIS2DH_Y); 00180 mTextLIS2DH_Z = (TextView) findViewById(R.id.textLIS2DH_Z); 00181 mTextLIS2DHRaw = (TextView) findViewById(R.id.textLIS2DHRaw); 00182 mTextPressure = (TextView) findViewById(R.id.textPressure); 00183 mTextTemperature = (TextView) findViewById(R.id.textTemperature); 00184 mTextPressureRaw = (TextView) findViewById(R.id.textPressureRaw); 00185 mTextHeartRate = (TextView) findViewById(R.id.textHeartRate); 00186 mTextHeartRateRaw = (TextView) findViewById(R.id.textHeartRateRaw); 00187 mBtnSubscribe = (Button) findViewById(R.id.btnSubscribe); 00188 mClickStartBtn = (TextView) findViewById(R.id.clickStartBtn); 00189 mClickStartBtn2 = (TextView) findViewById(R.id.clickStartBtn2); 00190 mAccelMissionStopped = (TextView) findViewById(R.id.textView7); 00191 mECGMissionStopped = (TextView) findViewById(R.id.textView8); 00192 /// accelerometer 00193 lineGraphingAccelerometer = (LineGraphing) findViewById(R.id.lineGraphingAccelerometer); 00194 /// pressure 00195 lineGraphingPressure = (LineGraphing) findViewById(R.id.lineGraphingPressure); 00196 /// temperature 1 00197 lineGraphingTemperature1 = (LineGraphing) findViewById(R.id.lineGraphingTemperature1); 00198 /// temperature 2 00199 lineGraphingTemperature2 = (LineGraphing) findViewById(R.id.lineGraphingTemperature2); 00200 /// heartrate 00201 lineGraphingHeartrate = (LineGraphing) findViewById(R.id.lineGraphingHeartrate); 00202 00203 mBtnSubscribe.setOnClickListener(new View.OnClickListener() { 00204 @Override 00205 public void onClick(View view) { 00206 //mBluetoothLeService.connect(mDeviceAddress); 00207 } 00208 }); 00209 00210 mClickStartBtn2.setOnClickListener(new View.OnClickListener() { 00211 @Override 00212 public void onClick(View view) { 00213 missionClick(); 00214 } 00215 }); 00216 00217 final Intent intent = getIntent(); 00218 final BluetoothDevice device = intent.getParcelableExtra(EXTRA_DEVICE); 00219 mDeviceName = device.getName(); 00220 mDeviceAddress = device.getAddress(); 00221 ((TextView) findViewById(R.id.device_address)).setText(mDeviceAddress); 00222 00223 //getSupportActionBar().setTitle(mDeviceName); 00224 //getSupportActionBar().setDisplayHomeAsUpEnabled(true); 00225 00226 final Intent gattServiceIntent = new Intent(this, BluetoothLeService.class); 00227 bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE); 00228 00229 // setup the accelerometer linegraph collections 00230 linePointCollectionX = new LinePointCollection(Color.RED, 20); 00231 linePointCollectionY = new LinePointCollection(Color.GREEN, 20); 00232 linePointCollectionZ = new LinePointCollection(Color.BLUE, 20); 00233 lineGraphingAccelerometer.addPointCollection(linePointCollectionX); 00234 lineGraphingAccelerometer.addPointCollection(linePointCollectionY); 00235 lineGraphingAccelerometer.addPointCollection(linePointCollectionZ); 00236 00237 // setup the pressure linegraph collections 00238 linePointCollectionPressure = new LinePointCollection(Color.BLACK, 20); 00239 lineGraphingPressure.addPointCollection(linePointCollectionPressure); 00240 00241 // setup the temperature 1 linegraph collections 00242 linePointCollectionTemperature1 = new LinePointCollection(Color.BLACK, 20); 00243 lineGraphingTemperature1.addPointCollection(linePointCollectionTemperature1); 00244 00245 // setup the temperature 2 linegraph collections 00246 linePointCollectionTemperature1 = new LinePointCollection(Color.BLACK, 20); 00247 lineGraphingTemperature1.addPointCollection(linePointCollectionTemperature1); 00248 00249 linePointCollectionTemperature2 = new LinePointCollection(Color.BLACK, 20); 00250 lineGraphingTemperature2.addPointCollection(linePointCollectionTemperature2); 00251 00252 linePointCollectionHeartrate = new LinePointCollection(Color.BLACK, 20); 00253 lineGraphingHeartrate.addPointCollection(linePointCollectionHeartrate); 00254 00255 00256 00257 } 00258 00259 /** 00260 * Called from the activity framework when the activity is paused 00261 */ 00262 @Override 00263 protected void onPause() { 00264 super.onPause(); 00265 unregisterReceiver(mGattUpdateReceiver); 00266 } 00267 00268 /** 00269 * Called from the activity framework when the activity is resuming 00270 */ 00271 @Override 00272 protected void onResume() { 00273 super.onResume(); 00274 registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter()); 00275 if (mBluetoothLeService != null) { 00276 final boolean result = mBluetoothLeService.connect(mDeviceAddress); 00277 } 00278 } 00279 00280 /** 00281 * Define an intent filter of actions to respond to 00282 * @return Returns the intent filter that is created 00283 */ 00284 private static IntentFilter makeGattUpdateIntentFilter() { 00285 final IntentFilter intentFilter = new IntentFilter(); 00286 intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED); 00287 intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED); 00288 intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED); 00289 intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE); 00290 intentFilter.addAction(BluetoothLeService.ACTION_CHARACTERISTIC_WRITE); 00291 return intentFilter; 00292 } 00293 00294 // Code to manage Service lifecycle. 00295 private final ServiceConnection mServiceConnection = new ServiceConnection() { 00296 @Override 00297 public void onServiceConnected(final ComponentName componentName, final IBinder service) { 00298 mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService(); 00299 if (!mBluetoothLeService.initialize()) { 00300 finish(); 00301 } 00302 // Automatically connects to the device upon successful start-up initialization. 00303 mBluetoothLeService.connect(mDeviceAddress); 00304 } 00305 00306 @Override 00307 public void onServiceDisconnected(final ComponentName componentName) { 00308 mBluetoothLeService = null; 00309 //finish(); 00310 } 00311 }; 00312 00313 /** 00314 * Handles various events fired by the Service. 00315 */ 00316 private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { 00317 @Override 00318 public void onReceive(final Context context, final Intent intent) { 00319 final String action = intent.getAction(); 00320 if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { 00321 mConnected = true; 00322 updateConnectionState(R.string.connected); 00323 invalidateOptionsMenu(); 00324 } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { 00325 mConnected = false; 00326 updateConnectionState(R.string.disconnected); 00327 invalidateOptionsMenu(); 00328 finish(); // get out of this activity and back to the parent 00329 } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { 00330 // Show all the supported services and characteristics on the user interface. 00331 displayGattServices(mBluetoothLeService.getSupportedGattServices()); 00332 subscribeCharacteristics(); 00333 } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { 00334 final String noData = getString(R.string.no_data); 00335 final String uuid = intent.getStringExtra(BluetoothLeService.EXTRA_UUID_CHAR); 00336 final byte[] dataArr = intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA_RAW); 00337 if (uuid != null) { 00338 UpdateUI(UUID.fromString(uuid), dataArr); 00339 } 00340 } else if (BluetoothLeService.ACTION_CHARACTERISTIC_WRITE.equals(action)) { 00341 final String uuid = intent.getStringExtra(BluetoothLeService.EXTRA_UUID_CHAR); 00342 if (uuid.equalsIgnoreCase(BLE_MAXIM_HSP_COMMAND_CHARACTERISTIC)) { 00343 EnableMissionButton(true); 00344 } 00345 } 00346 } 00347 }; 00348 00349 /** 00350 * Enable or disable the Start Mission Button based on HSP state variable 00351 * @param state Enabled or disabled status 00352 */ 00353 private void EnableMissionButton(boolean state) { 00354 isMissionButtonEnabled = state; 00355 if (state) { 00356 mClickStartBtn2.setBackgroundColor(Color.BLACK); 00357 } else { 00358 mClickStartBtn2.setBackgroundColor(Color.LTGRAY); 00359 } 00360 } 00361 00362 /** 00363 * Update the connection status 00364 * @param resourceId Resource id that is bound to the status test view field 00365 */ 00366 private void updateConnectionState(final int resourceId) { 00367 runOnUiThread(new Runnable() { 00368 @Override 00369 public void run() { 00370 final int colourId; 00371 00372 switch (resourceId) { 00373 case R.string.connected: 00374 colourId = android.R.color.holo_green_dark; 00375 break; 00376 case R.string.disconnected: 00377 colourId = android.R.color.holo_red_dark; 00378 break; 00379 default: 00380 colourId = android.R.color.black; 00381 break; 00382 } 00383 00384 mConnectionState.setText(resourceId); 00385 mConnectionState.setTextColor(getResources().getColor(colourId)); 00386 } 00387 }); 00388 } 00389 00390 /** 00391 * Convert the temperature from celsius to fahrenheit 00392 * @param celsius temperature to convert 00393 * @return converted temperature in fahrenheit 00394 */ 00395 private float ToFahrenheit(float celsius) { 00396 return celsius * 9 / 5 + 32; 00397 } 00398 00399 /** 00400 * Convert the incoming byte data array into a temperature 00401 * @param data Data array containing two elements 00402 * @return temperature as a float in celcius 00403 */ 00404 private float ToTemperature(byte[] data) { 00405 float value; 00406 value = (float) data[1] + (float) data[0] / 256.0f; 00407 return value; 00408 } 00409 00410 /** 00411 * Convert the portion of the byte array into a float 00412 * @param data Array to convert, a part of this array is converted based on a offset 00413 * @param offset Offset into the array to extract the float 00414 * @return Returns the float that was converted 00415 */ 00416 private float PartialArrayToFloat(byte[] data, int offset) { 00417 short value = (short) ((short) (data[offset + 1] << 8) + (short) data[offset]); 00418 return (float) value; 00419 } 00420 00421 /** 00422 * Calculate the heart rate from the ECG RtoR device 00423 * @param data Incoming data to convert 00424 * @return Returns a float representing the heart rate 00425 */ 00426 private float CalculateHeartRate(byte[] data) { 00427 float t = 8.0f; 00428 float value = 0; 00429 float RtoR = (float) ((int) data[1] << 8) + (float) data[0]; 00430 float fmStr = (float) ((int) data[3] << 8) + (float) data[2]; 00431 if (fmStr == 0.0f) t = 7.813f; 00432 if (RtoR > 0.0f) { 00433 value = 60000.0f / (RtoR * t); 00434 } 00435 return value; 00436 } 00437 00438 /** 00439 * Update the UI for a given characteristic uuid using data 00440 * This will update all of the text view values for the given characteristic 00441 * It also will graph the incoming data 00442 * @param characteristic Characteristic to update with given data 00443 * @param data Data used to update the UI 00444 */ 00445 private void UpdateUI(UUID characteristic, byte[] data) { 00446 float x = 0; 00447 float y = 0; 00448 float z = 0; 00449 float value = 0; 00450 String str = ""; 00451 str = bytesToHex(data); 00452 // temperature top 00453 if (characteristic.compareTo(UUID.fromString(BLE_MAXIM_HSP_TEMPERATURE_TOP_CHARACTERISTIC)) == 0) { 00454 mTextTempTopRaw.setText(str); 00455 value = ToFahrenheit(ToTemperature(data)); 00456 mTextTempTop.setText(String.format("%.1f", value) + "F"); 00457 linePointCollectionTemperature1.addpoint(value); 00458 lineGraphingTemperature1.plotPointCollection(); 00459 } 00460 // temperature bottom 00461 if (characteristic.compareTo(UUID.fromString(BLE_MAXIM_HSP_TEMPERATURE_BOTTOM_CHARACTERISTIC)) == 0) { 00462 mTextTempBottomRaw.setText(str); 00463 value = ToFahrenheit(ToTemperature(data)); 00464 mTextTempBottom.setText(String.format("%.1f", value) + "F"); 00465 linePointCollectionTemperature2.addpoint(value); 00466 lineGraphingTemperature2.plotPointCollection(); 00467 } 00468 // acceleration 00469 if (characteristic.compareTo(UUID.fromString(BLE_MAXIM_HSP_ACCEL_CHARACTERISTIC)) == 0) { 00470 mTextLIS2DHRaw.setText(str); 00471 x = PartialArrayToFloat(data, 0); 00472 y = PartialArrayToFloat(data, 2); 00473 z = PartialArrayToFloat(data, 4); 00474 mTextLIS2DH_X.setText(String.format("X: %.1f", x)); 00475 mTextLIS2DH_Y.setText(String.format("Y: %.1f", y)); 00476 mTextLIS2DH_Z.setText(String.format("Z: %.1f", z)); 00477 linePointCollectionX.addpoint(x); 00478 linePointCollectionY.addpoint(y); 00479 linePointCollectionZ.addpoint(z); 00480 lineGraphingAccelerometer.plotPointCollection(); 00481 } 00482 // pressure 00483 if (characteristic.compareTo(UUID.fromString(BLE_MAXIM_HSP_PRESSURE_CHARACTERISTIC)) == 0) { 00484 String pressureStr = ""; 00485 int n = 0; 00486 int bits; 00487 byte[] arr = {data[0], data[1], data[2], data[3]}; 00488 ByteBuffer bb = ByteBuffer.wrap(arr); 00489 bb.order(ByteOrder.LITTLE_ENDIAN); 00490 bits = bb.getInt(); 00491 float temperature = Float.intBitsToFloat(bits); 00492 n = 4; 00493 //bits = data[n] | data[n+1]<<8 | data[n+2]<<16 | data[n+3]<<24; 00494 byte[] arr2 = {data[4], data[5], data[6], data[7]}; 00495 ByteBuffer bb2 = ByteBuffer.wrap(arr2); 00496 bb2.order(ByteOrder.LITTLE_ENDIAN); 00497 bits = bb2.getInt(); 00498 float pressure = Float.intBitsToFloat(bits); 00499 mTextPressure.setText(String.format("%.1f", pressure) + "Pa"); 00500 linePointCollectionPressure.addpoint(pressure); 00501 lineGraphingPressure.plotPointCollection(); 00502 } 00503 // heartrate 00504 if (characteristic.compareTo(UUID.fromString(BLE_MAXIM_HSP_HEARTRATE_CHARACTERISTIC)) == 0) { 00505 mTextHeartRateRaw.setText(str); 00506 value = CalculateHeartRate(data); 00507 mTextHeartRate.setText(String.format("%.1f", value)); 00508 linePointCollectionHeartrate.addpoint(value); 00509 lineGraphingHeartrate.plotPointCollection(); 00510 } 00511 } 00512 00513 /// array of hex ascii values used for hex byte to string conversion 00514 final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); 00515 00516 /** 00517 * Convert a series of bytes within a given array into a string 00518 * @param bytes Arbitrary array of bytes to convert 00519 * @return String is returned containing the bytes as hex ascii values 00520 */ 00521 public static String bytesToHex(byte[] bytes) { 00522 char[] hexChars = new char[bytes.length * 2]; 00523 for (int j = 0; j < bytes.length; j++) { 00524 int v = bytes[j] & 0xFF; 00525 hexChars[j * 2] = hexArray[v >>> 4]; 00526 hexChars[j * 2 + 1] = hexArray[v & 0x0F]; 00527 } 00528 return new String(hexChars); 00529 } 00530 00531 /** 00532 * Demonstrates how to iterate through the supported GATT Services/Characteristics. 00533 * In this sample, we populate the data structure that is bound to the ExpandableListView 00534 * on the UI. 00535 * @param gattServices List of services to parse and display 00536 */ 00537 private void displayGattServices(final List<BluetoothGattService> gattServices) { 00538 if (gattServices == null) return; 00539 00540 String uuid = null; 00541 final String unknownServiceString = getResources().getString(R.string.unknown_service); 00542 final String unknownCharaString = getResources().getString(R.string.unknown_characteristic); 00543 final List<Map<String, String>> gattServiceData = new ArrayList<>(); 00544 final List<List<Map<String, String>>> gattCharacteristicData = new ArrayList<>(); 00545 mGattCharacteristics = new ArrayList<>(); 00546 00547 // Loops through available GATT Services. 00548 for (final BluetoothGattService gattService : gattServices) { 00549 final Map<String, String> currentServiceData = new HashMap<>(); 00550 uuid = gattService.getUuid().toString(); 00551 currentServiceData.put(LIST_NAME, GattAttributeLookupTable.getAttributeName(uuid, unknownServiceString)); 00552 currentServiceData.put(LIST_UUID, uuid); 00553 gattServiceData.add(currentServiceData); 00554 00555 final List<Map<String, String>> gattCharacteristicGroupData = new ArrayList<>(); 00556 final List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); 00557 final List<BluetoothGattCharacteristic> charas = new ArrayList<>(); 00558 00559 // Loops through available Characteristics. 00560 for (final BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { 00561 charas.add(gattCharacteristic); 00562 final Map<String, String> currentCharaData = new HashMap<>(); 00563 uuid = gattCharacteristic.getUuid().toString(); 00564 currentCharaData.put(LIST_NAME, GattAttributeLookupTable.getAttributeName(uuid, unknownCharaString)); 00565 currentCharaData.put(LIST_UUID, uuid); 00566 gattCharacteristicGroupData.add(currentCharaData); 00567 } 00568 00569 mGattCharacteristics.add(charas); 00570 gattCharacteristicData.add(gattCharacteristicGroupData); 00571 } 00572 } 00573 00574 /** 00575 * Given a TextView, output a string plus units concatenated 00576 * @param textView View to set 00577 * @param value String representing the value 00578 * @param units String representing the units 00579 */ 00580 private void SetSensorText(TextView textView, String value, String units) { 00581 textView.setText(value + units); 00582 } 00583 00584 /** 00585 * Create the menu for the options 00586 * @param menu the menu to set the options for 00587 * @return true if created 00588 */ 00589 @Override 00590 public boolean onCreateOptionsMenu(final Menu menu) { 00591 getMenuInflater().inflate(R.menu.gatt_services, menu); 00592 if (mConnected) { 00593 menu.findItem(R.id.menu_connect).setVisible(false); 00594 menu.findItem(R.id.menu_disconnect).setVisible(true); 00595 } else { 00596 menu.findItem(R.id.menu_connect).setVisible(true); 00597 menu.findItem(R.id.menu_disconnect).setVisible(false); 00598 } 00599 return true; 00600 } 00601 00602 /** 00603 * Called when the user selects a menu item 00604 * @param item Selected menu item 00605 * @return returns true if the selection was handled 00606 */ 00607 @Override 00608 public boolean onOptionsItemSelected(final MenuItem item) { 00609 switch (item.getItemId()) { 00610 case R.id.menu_connect: 00611 mBluetoothLeService.connect(mDeviceAddress); 00612 return true; 00613 case R.id.menu_disconnect: 00614 mBluetoothLeService.disconnect(); 00615 return true; 00616 case android.R.id.home: 00617 mBluetoothLeService.disconnect(); 00618 return true; 00619 } 00620 return super.onOptionsItemSelected(item); 00621 } 00622 00623 /** 00624 * Mission button clicked 00625 * @param v The handle of the view clicked 00626 */ 00627 public void onClick(View v) { 00628 missionClick(); 00629 } 00630 00631 /** 00632 * Start or stop the mission 00633 */ 00634 private void missionClick() { 00635 byte[] data = new byte[1]; 00636 //if (isMissionButtonEnabled == false) return; 00637 00638 if (mClickStartBtn2.getText().toString().equalsIgnoreCase("Stop Mission") == true) { 00639 // stop 00640 data[0] = 0x00; 00641 mBluetoothLeService.writeCharacteristic(UUID.fromString(BLE_MAXIM_HSP_SERVICE), UUID.fromString(BLE_MAXIM_HSP_COMMAND_CHARACTERISTIC), data); 00642 mClickStartBtn2.setText("Start Mission"); 00643 EnableMissionButton(false); 00644 } else { 00645 // start 00646 data[0] = 0x01; 00647 mBluetoothLeService.writeCharacteristic(UUID.fromString(BLE_MAXIM_HSP_SERVICE), UUID.fromString(BLE_MAXIM_HSP_COMMAND_CHARACTERISTIC), data); 00648 mClickStartBtn2.setText("Stop Mission"); 00649 EnableMissionButton(false); 00650 } 00651 } 00652 00653 }
Generated on Thu Jul 28 2022 18:07:15 by
1.7.2