Android - BLE蓝牙回调会导致更多的CPU使用率

时间:2017-09-01 07:50:43

标签: android bluetooth-lowenergy android-bluetooth

我正在编写一个BLE应用程序来读取K2健身手镯中的HR和计步器数据。但目前,我正面临一些性能问题。

BLE读写会导致更多CPU使用,所以请任何人说明如何正确读写特性。

以下是蓝牙回拨的代码

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            broadcastUpdate(intentAction);
            Log.d(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.d(TAG, "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            Log.d(TAG, "Disconnected from GATT server.");
            broadcastUpdate(intentAction);
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        super.onDescriptorWrite(gatt, descriptor, status);
        try {
            writeDescriptor(gatt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCharacteristicWrite(final BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);
        try {
            writeCharacteristic(gatt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }

    @Override
    public void onCharacteristicChanged(final BluetoothGatt gatt,
                                        final BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
    }
};

//描述符方法

private void writeDescriptor(BluetoothGatt gatt) throws Exception {

        BluetoothGattService RxService = gatt.getService(UUID_BLE_SERVICE);
        BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(UUID_RX_SERVICE);
        BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(UUID_TX_SERVICE);


        gender = 0;
        age = 40;
        height = 150;
        weight = 80;

        //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;

        RxChar.setValue(byteArray);
        gatt.writeCharacteristic(RxChar);

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

    }

写入特征的方法

public void writeCharacteristic(final BluetoothGatt gatt) throws Exception {
    BluetoothGattService RxService = gatt.getService(UUID_BLE_SERVICE);
    BluetoothGattCharacteristic RxChar = RxService.getCharacteristic(UUID_RX_SERVICE);
    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);
}

请任何人指导我解决这个性能问题。

0 个答案:

没有答案