2017-06-05 99 views
0

我擁有一個Polar H10胸帶,它能夠以低能量藍牙運行並提供心率和心率變異性。從BLE設備中讀取數值

我想用Android應用程序讀出這些值。由於official BLE tutorial的幫助,我可以連接到設備。現在的問題是從設備讀出心率和心率變異性值。每當設備上有新的值時,我想讀出這個值(並且至少每秒有一個新的值)。

我發現下面的代碼片段:

private static double extractHeartRate(
      BluetoothGattCharacteristic characteristic) { 

     int flag = characteristic.getProperties(); 
     Log.d(TAG, "Heart rate flag: " + flag); 
     int format = -1; 
     // Heart rate bit number format 
     if ((flag & 0x01) != 0) { 
      format = BluetoothGattCharacteristic.FORMAT_UINT16; 
      Log.d(TAG, "Heart rate format UINT16."); 
     } else { 
      format = BluetoothGattCharacteristic.FORMAT_UINT8; 
      Log.d(TAG, "Heart rate format UINT8."); 
     } 
     final int heartRate = characteristic.getIntValue(format, 1); 
     Log.d(TAG, String.format("Received heart rate: %d", heartRate)); 
     return heartRate; 
    } 



private static Integer[] extractBeatToBeatInterval(
      BluetoothGattCharacteristic characteristic) { 

     int flag = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0); 
     int format = -1; 
     int energy = -1; 
     int offset = 1; // This depends on hear rate value format and if there is energy data 
     int rr_count = 0; 

     if ((flag & 0x01) != 0) { 
      format = BluetoothGattCharacteristic.FORMAT_UINT16; 
      Log.d(TAG, "Heart rate format UINT16."); 
      offset = 3; 
     } else { 
      format = BluetoothGattCharacteristic.FORMAT_UINT8; 
      Log.d(TAG, "Heart rate format UINT8."); 
      offset = 2; 
     } 
     if ((flag & 0x08) != 0) { 
      // calories present 
      energy = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, offset); 
      offset += 2; 
      Log.d(TAG, "Received energy: {}"+ energy); 
     } 
     if ((flag & 0x16) != 0){ 
      // RR stuff. 
      Log.d(TAG, "RR stuff found at offset: "+ offset); 
      Log.d(TAG, "RR length: "+ (characteristic.getValue()).length); 
      rr_count = ((characteristic.getValue()).length - offset)/2; 
      Log.d(TAG, "RR length: "+ (characteristic.getValue()).length); 
      Log.d(TAG, "rr_count: "+ rr_count); 
      if (rr_count > 0) { 
       Integer[] mRr_values = new Integer[rr_count]; 
       for (int i = 0; i < rr_count; i++) { 
        mRr_values[i] = characteristic.getIntValue(
          BluetoothGattCharacteristic.FORMAT_UINT16, offset); 
        offset += 2; 
        Log.d(TAG, "Received RR: " + mRr_values[i]); 
       } 
       return mRr_values; 
      } 
     } 
     Log.d(TAG, "No RR data on this update: "); 
     return null; 
    } 

我如何使用它來提取的心臟速率和R-R間隔(節拍跳動間隔)假設我有設備的連接?如果有人能做一個簡單的例子,我會很高興。另外,我想使用一種服務,以便它能夠在後臺運行,並且我可以做其他工作,並且消耗更少的電量。但該服務應該具有最高的優先級,以便它不會被殺死。

extractBeatToBeatInterval()方法有一個int offset = 1;與描述

這取決於聽到率值格式,如果有能源數據

我不明白這個offest的意義。我應該使用什麼抵消值?其次,如果連接中斷,我希望得到一個通知,以便我可以處理它(例如顯示一條消息)。我怎樣才能做到這一點?

回答

0

我可以幫你解決這個問題。 對於連接中斷,你可以處理像這樣

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
      if (newState == BluetoothGatt.STATE_CONNECTED) { 
       Log.e(Constants.TAGGattConnectThread, "Connected to the GATT Server " + status + " " +newState); 
       Log.e(Constants.TAGGattConnectThread, "Attempting to discover services"); 
       bluetoothGatt.discoverServices(); 
      } 
      else if(newState == BluetoothGatt.STATE_DISCONNECTED) 
      { 
       Log.e(Constants.TAGGattConnectThread,"Band disconnected"); 
       //Write you disconnect handling code here 
      } 

你也可以設置「自動連接選項爲‘True’,而連接關貿總協定

bluetoothGatt = bluetoothDevice.connectGatt(context, true, bluetoothGattCallback); 

有關創建服務,您可以創建一個Sticky服務,除非強制停止,否則它將自動重啓。