如何接收存储在BLE设备中的心率?

时间:2019-05-02 13:47:02

标签: android bluetooth-lowenergy xiaomi heartrate

嗨,我是android ble编程的新手。最近,我建立了一个心率监测应用程序,可以从Mi波段3接收实时心率。但是,Mi波段3每分钟记录一次心率(根据Mi Fit应用程序设置),并且数据存储在咪乐队。我想知道如何在Mi乐队的Android应用程序中接收心率存储。

下面是我的代码,可以测量实时心率。您可以在https://github.com/lmarceau/heart-rate-monitor-ble中找到有关源代码的更多信息。

在BluetoothGattCallback中:

private final BluetoothGattCallback gattClientCallback = new 
BluetoothGattCallback() {
...
@Override
    public void onDescriptorWrite(BluetoothGatt gatt, 
BluetoothGattDescriptor descriptor,
                                  int status){
        BluetoothGattCharacteristic characteristic =
                gatt.getService(HEART_RATE_SERVICE_UUID)

.getCharacteristic(HEART_RATE_CONTROL_POINT_CHAR_UUID);

        /* Write to Heart Rate Control Point Characteristic to get data 
streaming */
        characteristic.setValue(new byte[]{1, 1});
        gatt.writeCharacteristic(characteristic);
    }

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

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

在setHeartRateNotification中:

   public void setHeartRateNotification(BluetoothGatt gatt,
                                     boolean enabled) {
    BluetoothGattCharacteristic characteristic =
            gatt.getService(HEART_RATE_SERVICE_UUID)
                    .getCharacteristic(HEART_RATE_MEASUREMENT_CHAR_UUID);

    /* Enable notification  on the heart rate measurement characteristic 
    */
    gatt.setCharacteristicNotification(characteristic, enabled);

    BluetoothGattDescriptor descriptor =

    characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID);

    descriptor.setValue(
            BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

    gatt.writeDescriptor(descriptor);
}

此代码只能测量实时心率。如何获得已经测量的心律并记录在Mi频段3中?

0 个答案:

没有答案