BLE BluetoothGattExecutor持续读写呼叫,CPU使用率高吗?

时间:2017-06-22 18:41:50

标签: android android-bluetooth

您好我正在从BLE设备读取心率并在应用程序中显示。但是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);
}

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为心率服务(0x180D)是基于通知的服务,您已订阅它: -

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

您需要通过在应用设计中的适当逻辑点传递false来取消订阅