2017-06-22 52 views
0

嗨我正在閱讀從BLE設備的心率和顯示在應用程序。但BLE BluetoothGattExecutor在我的應用程序中連續讀取和寫入呼叫,如果我在後臺連續運行,CPU使用率很高。BLE BluetoothGattExecutor讀寫連續不斷,CPU使用率高?

@Override 
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { 
    super.onDescriptorWrite(gatt, descriptor, status); 
    //Log.i("BluetoothGattExecutor","onDescriptorWrite"+ ","+status); 
    System.out.println("BLE gender " + HeartRhythmApplication.mGender); 
    BluetoothGattService RxService = gatt.getService(UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb")); 
    List<BluetoothGattCharacteristic> characteristics = RxService.getCharacteristics(); 

    // BluetoothGattService RxService = gatt.getService(UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb")); 
    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(UUID.fromString("0000fff2-0000-1000-8000-00805f9b34fb")); 
    BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb")); 

    if (mGender != null) { 
     gender = Integer.parseInt(mGender); 
    } else { 
     gender = 0; 
    } 
    if (mAge != null) { 
     age = Integer.parseInt(mAge); 
    } else { 
     age = 40; 
    } 

    if (mHeight != null) { 
     height = Integer.parseInt(mHeight); 
    } else { 
     height = 150; 
    } 
    if (mWeight != null) { 
     weight = Integer.parseInt(

    // byte[] dateTime = getDateTimeByteArray(); 
    /* byte[] byteArray = new byte[10]; 
    byteArray[0] = (byte) 104; 
    byteArray[1] = (byte) 32; 
    byteArray[2] = (byte) 4; 
    byteArray[3] = (byte) 0; 
    byteArray[4] = (byte) 183; 
    byteArray[5] = (byte) 156; 
    byteArray[6] = (byte) 146; 
    byteArray[7] = (byte) 85; 
    byteArray[8] = (byte) 198; 
    byteArray[9] = (byte) 22;*/ 

    //byte[] byteArray = getDateTimeByteArray(); 

    //68 04 04 00 B2 3C 00 1C 7A 16 
    byte[] byteArray = new byte[10]; 
    byteArray[0] = (byte) 104; 
    byteArray[1] = (byte) 4; 
    byteArray[2] = (byte) 4; 
    byteArray[3] = (byte) 0; 
    byteArray[4] = (byte) height; 
    byteArray[5] = (byte) weight; 
    byteArray[6] = (byte) gender; 
    byteArray[7] = (byte) age; 
    byteArray[8] = (byte) 122; 
    byteArray[9] = (byte) 22; 

    /* byte[] byteArray = new byte[10]; 
    byteArray[0] = (byte) 104; 
    byteArray[1] = (byte) 4; 
    byteArray[2] = (byte) 4; 
    byteArray[3] = (byte) 0; 
    byteArray[4] = (byte) 178; 
    byteArray[5] = (byte) 60; 
    byteArray[6] = (byte) 0; 
    byteArray[7] = (byte) 28; 
    byteArray[8] = (byte) 122; 
    byteArray[9] = (byte) 22;*/ 


    /* byte[] byteArray = new byte[7]; 
    byteArray[0] = (byte) 104; 
    byteArray[1] = (byte) 6; 
    byteArray[2] = (byte) 1; 
    byteArray[3] = (byte) 0; 
    byteArray[4] = (byte) 0; 
    byteArray[5] = (byte) 111; 
    byteArray[6] = (byte) 22;*/ 
    RxChar.setValue(byteArray); 
    gatt.writeCharacteristic(RxChar); 

    gatt.setCharacteristicNotification(RxChar, true); 
    gatt.setCharacteristicNotification(TxChar, true); 

    currentAction = null; 
    execute(gatt); 
} 

private static byte[] getDateTimeByteArray() { 
    int sum = 0; 
    byte[] dateTime = new byte[10]; 
    dateTime[0] = 104; 
    dateTime[1] = 32; 
    dateTime[2] = 04; 
    dateTime[3] = 0; 

    sum = sum + 104 + 32 + 04 + 0; 
    long currentSeconds = (System.currentTimeMillis()/1000l) + 19800; 

    char[] hexCurrentSecondsChar = Long.toHexString(currentSeconds).toCharArray(); 
    String blaString = hexCurrentSecondsChar[6] + "" + hexCurrentSecondsChar[7]; 
    sum = sum + Integer.parseInt(blaString, 16); 
    dateTime[4] = (byte) (Integer.parseInt(blaString, 16)); 

    String b2aString = hexCurrentSecondsChar[4] + "" + hexCurrentSecondsChar[5]; 
    dateTime[5] = (byte) (Integer.parseInt(b2aString, 16)); 
    sum = sum + Integer.parseInt(b2aString, 16); 

    String b3aString = hexCurrentSecondsChar[2] + "" + hexCurrentSecondsChar[3]; 
    dateTime[6] = (byte) (Integer.parseInt(b3aString, 16)); 
    sum = sum + Integer.parseInt(b3aString, 16); 

    String b4aString = hexCurrentSecondsChar[0] + "" + hexCurrentSecondsChar[1]; 
    dateTime[7] = (byte) (Integer.parseInt(b4aString, 16)); 
    sum = sum + Integer.parseInt(b4aString, 16); 

    dateTime[8] = (byte) (sum % 256); 
    dateTime[9] = 22; 
    return dateTime; 
} 

@Override 
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { 
    super.onCharacteristicWrite(gatt, characteristic, status); 
    // Log.i("isDisconnected","onCharacteristicWrite"+ ","+isDisconnected); 
    BluetoothGattService RxService = gatt.getService(UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb")); 
    List<BluetoothGattCharacteristic> characteristics = RxService.getCharacteristics(); 
    // BluetoothGattService RxService = gatt.getService(UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb")); 
    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(UUID.fromString("0000fff2-0000-1000-8000-00805f9b34fb")); 
    BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb")); 

    if(!isTimeUpdated){ 
     //Log.i("isTimeUpdated:", ""+ isTimeUpdated); 
     isTimeUpdated = true; 
     byte[] byteArray = getDateTimeByteArray(); 
     RxChar.setValue(byteArray); 
     gatt.writeCharacteristic(RxChar); 
    }else if(isDisconnected) { 
     isDisconnected = false; 
     //Log.i("isDisconnected",""+ ","+isDisconnected); 
     byte[] byteArray = antiLostByteArray(); 
     RxChar.setValue(byteArray); 
     gatt.writeCharacteristic(RxChar); 
    }else{ 
     byte[] byteArray = new byte[7]; 
     byteArray[0] = (byte) 104; 
     byteArray[1] = (byte) 6; 
     byteArray[2] = (byte) 1; 
     byteArray[3] = (byte) 0; 
     byteArray[4] = (byte) 0; 
     byteArray[5] = (byte) 111; 
     byteArray[6] = (byte) 22; 
     RxChar.setValue(byteArray); 
     gatt.writeCharacteristic(RxChar); 
    } 

    gatt.setCharacteristicNotification(RxChar, true); 
    gatt.setCharacteristicNotification(TxChar, true); 

    currentAction = null; 
    execute(gatt); 
} 
+0

什麼是BluetoothGattExecutor?您的代碼是基於[此示例項目](http://www.java2s.com/Open-Source/Android_Free_Code/Sensor/heart/com_sample_hrvBluetoothGattExecutor_java.htm)? –

+0

@KevinKrumwiede是這個代碼只有 –

+0

@KevinKrumwiede如果我使用相同的代碼意味着心率不顯示。 –

回答

0

發生這種情況是因爲心率服務(0x180D)是一個基於通知服務和你已經訂閱了它: -

gatt.setCharacteristicNotification(RxChar, true); 
gatt.setCharacteristicNotification(TxChar, true); 

你需要通過傳遞false在適當的邏輯點退訂您應用程序設計

+0

你能告訴邏輯嗎? –

+0

除非您取消訂閱,否則傳感器不會知道您對HR數據不再感興趣,並將繼續用傳感器數據轟擊您 – Zakir

+0

我想在後臺連續閱讀HR ..您能否告訴邏輯? –