华为ai开发套件_探索华为健康套件:数据控制器

华为ai开发套件

Hello everyone 🙌 , in this article we’re going to take a look at the Huawei Health Kit features so we can easily put these features to our apps.

大家好,🙌,在本文中,我们将了解华为Health Kit的功能,以便我们可以轻松地将这些功能应用于我们的应用程序。

Health app industry have shown an important increase in recent years.The software and healthcare industry are working together to find the latest technology products for the needs of patients and families.Huawei offers high technology service for the health and fitness app ecosystem with HealthKit and HiHealthKit service.Huawei and third-party partner capabilities track and customize all aspects of better living.

近年来,健康应用行业呈现出重要的增长态势,软件和医疗行业正在共同努力寻找满足患者和家庭需求的最新技术产品。华为通过HealthKit和HiHealthKit为健康和健身应用生态系统提供高科技服务华为和第三方合作伙伴功能可跟踪和自定义更好生活的方方面面。

  • Data storage : Provides a platform for users to store their fitness and health data.

    数据存储 :为用户提供一个平台来存储其健身和健康数据。

  • Data openness : Provides a wide range of APIs for writing and reading speed, positioning, blood sugar level, and other data.

    数据开放性 :提供各种API,用于读写速度,位置,血糖水平和其他数据。

  • Data interaction : Enables you and your users to agree on data access, guaranteeing proper use and security of personal data.

    数据交互 :使您和您的用户可以就数据访问达成协议,从而保证个人数据的正确使用和安全。

HealthKit:

HealthKit:

HiHealth:

HiHealth:

We will examine the differences between HealthKit and HiHealth in our next articles.

在下一篇文章中,我们将研究HealthKit和HiHealth之间的区别。

I created a sample app which I’ve open-sourced on GitHub for enthusiasts who want to use this service. 🎉

我创建了一个示例应用程序,该应用程序已在GitHub上开源,以供希望使用此服务的爱好者使用。 🎉

设置健康套件 (Setting up the Health Kit)

You’ll need to do setup before you can use the Health Kit.You can follow the official documentation on how to prepare app and enable Health Kit on App Gallery Connect.

您需要先进行设置,然后才能使用Health Kit。您可以按照官方文档中的有关如何准备应用程序和在App Gallery Connect上启用Health Kit的说明进行操作。

Also you can follow this blog post to integrate your apps with Huawei HMS Core:

您也可以按照此博客文章将您的应用程序与华为HMS Core集成:

I’ll show you how to implement Data Controller features in this article for your android projects.Let’s get started!

我将向您展示如何实现数据控制器功能 在本文中为您的android项目。让我们开始吧!

Add the required dependencies to the build.gradle file under app folder.We need some dependencies.

将所需的依赖项添加到app文件夹下的build.gradle文件中。我们需要一些依赖项。

Add the required permissions to the AndroidManifest.xml file under app/src/main folder.

将所需的权限添加到app / src / main文件夹下的AndroidManifest.xml文件。

You need to give some permissions after applying for HealthKit in service cards area.The applied permissions will be displayed on the user authorization page.

服务卡中申请HealthKit后需要授予一些权限 应用的权限将显示在用户授权页面上。

Image for post

Let’s continue by coding Data Controller functionalities in our project.

让我们继续在项目中编码Data Controller功能。

Image for post

使用Data Controller之前登录 (Sign-in before using Data Controller)

Firstly, we need to configure Huawei ID sign in.After that, we will start sign intent while using startActivityForResult method.We added also some scopes to apply for. The following only shows an example. You need to add scopes according to your specific needs.

首先,我们需要配置华为ID登录,然后使用startActivityForResult方法启动登录意图,并添加了一些适用范围。 下面仅显示一个示例。 您需要根据您的特定需求添加范围。

  • HEALTHKIT_STEP_BOTH → View and save step counts in HUAWEI Health Kit.

    HEALTHKIT_STEP_BOTH →在HUAWEI Health Kit中查看并保存步数。

  • HEALTHKIT_HEIGHTWEIGHT_BOTH → View and save height and weight in HUAWEI Health Kit.

    HEALTHKIT_HEIGHTWEIGHT_BOTH →在HUAWEI Health Kit中查看并保存身高和体重。

  • HEALTHKIT_HEARTRATE_BOTH → View and save the heart rate data in HUAWEI Health Kit.

    HEALTHKIT_HEARTRATE_BOTH →在HUAWEI Health Kit中查看并保存心率数据。

For details about the mapping between scopes and permissions, see Scope in the API reference.After successfully sign process , we can move to crud and other operations. We are able to call ten methods in DataController to perform operations on the fitness and health data.The methods include:

有关作用域和权限之间的映射的详细信息,请参阅API参考中的作用域 。成功签名过程之后,我们可以移至crud和其他操作。 我们可以在DataController中调用十个方法来对健身和健康数据执行操作,这些方法包括:

  • insert: inserts data.

    insert:插入数据。
  • delete: deletes data.

    delete:删除数据。
  • update: updates data.

    更新:更新数据。
  • read: reads data.

    读取:读取数据。
  • readTodaySummation: queries the summarized data of the current day.

    readTodaySummation:查询当天的汇总数据。
  • readTodaySummationFromDevice: queries the summarized data of the local device of the current day.

    readTodaySummationFromDevice:查询当天本地设备的汇总数据。
  • registerModifyDataMonitor: registers a listener for data updates.

    registerModifyDataMonitor:注册用于数据更新的侦听器。
  • unregisterModifyDataMonitor: unregisters a listener for data updates.

    unregisterModifyDataMonitor:注销用于数据更新的侦听器。
  • syncAll: syncs data between the device and cloud.

    syncAll:在设备和云之间同步数据。
  • clearAll: clears data of the app from the device and cloud.

    clearAll:从设备和云中清除应用程序的数据。

We need to create the data controller object before starting operations.

在开始操作之前,我们需要创建数据控制器对象。

插入资料 (Inserting Data)

We build a DataCollector object to insert body weight.

我们构建一个DataCollector对象以插入体重。

We will create a sampling dataset based on the data collector.

我们将基于数据收集器创建一个采样数据集。

We will create the start time, end time, and weight value for a DT_INSTANTANEOUS_BODY_WEIGHT sampling point.The weight value must be in float format.

我们将为DT_INSTANTANEOUS_BODY_WEIGHT采样点创建开始时间,结束时间和权重值。权重值必须为浮点格式。

And in this time, we need to call the data controller to insert the sampling dataset into the Health platform.

这次,我们需要调用数据控制器,将采样数据集插入Health平台。

If you examine the showSampleData function, you can see how the added data was reached.

如果检查showSampleData函数,则可以看到如何到达添加的数据。

删除资料 (Deleting Data)

We will build a DataCollector object for data deletion.

我们将构建一个DataCollector对象以删除数据。

After creating data collector object, we need to build the time range for the deletion: start time and end time.

创建数据收集器对象后,我们需要构建删除的时间范围:开始时间和结束时间。

We build a parameter object as the conditions for the deletion while using DeleteOptions.So, the data in this time interval will be deleted.Also note that: Only historical data that has been inserted by the current app can be deleted from the Health platform.

我们使用DeleteOptions构建一个参数对象作为删除的条件。因此,此时间间隔内的数据将被删除。还请注意:仅可从Health平台删除当前应用程序插入的历史数据。

And as last step, we use the specified condition deletion object to call the data controller to delete the sampling dataset.

最后一步,我们使用指定的条件删除对象来调用数据控制器以删除采样数据集。

Calling the data controller to delete the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data deletion is successful or not.

调用数据控制器删除采样数据集是异步操作。 因此,需要注册一个侦听器以监视数据删除是否成功。

更新数据 (Updating Data)

We will build a DataCollector object for data update.

我们将构建一个DataCollector对象用于数据更新。

We will create a sampling dataset based on the data collector.

我们将基于数据收集器创建一个采样数据集。

We will create the start time, end time, and weight value for a DT_INSTANTANEOUS_BODY_WEIGHT sampling point.The weight value must be in float format.

我们将为DT_INSTANTANEOUS_BODY_WEIGHT采样点创建开始时间,结束时间和权重值。权重值必须为浮点格式。

We build a parameter object as the conditions for the update while using UpdateOptions.So, the data in this time interval will be updated.

我们使用UpdateOptions建立一个参数对象作为更新的条件,因此,此时间间隔内的数据将被更新。

And as last step, we use the specified parameter object for the update to call the data controller to modify the sampling dataset.

最后一步,我们使用指定的参数对象进行更新,以调用数据控制器修改采样数据集。

Calling the data controller to modify the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data update is successful or not.

调用数据控制器修改采样数据集是异步操作。 因此,需要注册一个侦听器以监视数据更新是否成功。

If you examine the showSampleData function, you can see how the updated data was reached.

如果检查showSampleData函数,则可以查看如何获取更新的数据。

读取数据 (Reading Data)

We will build a DataCollector object for data query.

我们将构建一个DataCollector对象用于数据查询。

After creating data collector object, we need to build the time range for the data query: start time and end time.

创建数据收集器对象后,我们需要构建数据查询的时间范围:开始时间和结束时间。

We build a parameter object as the conditions for the data query while using ReadOptions.So, the data in this time interval will be read.

我们使用ReadOptions建立一个参数对象作为数据查询的条件,因此将读取此时间间隔内的数据。

And as last step, we use the specified condition query object to call the data controller to query the sampling dataset.

最后一步,我们使用指定的条件查询对象来调用数据控制器以查询采样数据集。

Calling the data controller to query the sampling dataset is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data query is successful or not.

调用数据控制器查询采样数据集是一个异步操作。 因此,需要注册一个侦听器以监视数据查询是否成功。

If you examine the showSampleData function, you can see how the read data was reached.

如果检查showSampleData函数,则可以看到如何达到读取的数据。

读取当日数据 (Reading Data of the Current Day)

We use the specified data type (DT_INSTANTANEOUS_BODY_WEIGHT) to call the data controller to query the summary data of this data type of the current day.

我们使用指定的数据类型(DT_INSTANTANEOUS_BODY_WEIGHT)调用数据控制器以查询当天该数据类型的摘要数据。

Calling the data controller to query the summary data of the current day is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data query is successful or not.

调用数据控制器查询当天的摘要数据是异步操作。 因此,需要注册一个侦听器以监视数据查询是否成功。

If you examine the showSampleData function, you can see how the read today data was reached.

如果检查showSampleData函数,则可以看到如何达到今天读取的数据。

在当天的本地设备上读取用户的数据 (Reading Data of the of the User on the Local Device of the Current Day)

We call the DataController to query the statistical value of the DT_INSTANTANEOUS_BODY_WEIGHT data type of the current day.

我们调用DataController来查询当日DT_INSTANTANEOUS_BODY_WEIGHT数据类型的统计值。

The query time range starts from 00:00:00 of the day and ends at the system timestamp when the API is called. Calling this API will query all data points with the start time or end time being in the specified time range. The sum value of the queried data points will be returned.

查询时间范围从当天的00:00:00开始,并在调用API时以系统时间戳记结束。 调用此API将查询开始时间或结束时间在指定时间范围内的所有数据点。 将返回查询的数据点的总和。

Calling the data controller to query the summary data of the current day is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the data query is successful or not.

调用数据控制器查询当天的摘要数据是异步操作。 因此,需要注册一个侦听器以监视数据查询是否成功。

If you examine the showSampleData function, you can see how the read today local device data was reached.

如果检查showSampleData函数,则可以看到如何达到今天读取的本地设备数据。

注册侦听器以进行更新 (Registering a Listener for Updates)

We will build a DataCollector object for data updates.

我们将构建一个DataCollector对象用于数据更新。

  • Basic metabolic rate per day (unit: kcal): DataType.DT_INSTANTANEOUS_CALORIES_BMR

    每天的基本代谢率(单位:大卡):DataType.DT_INSTANTANEANE_CALORIES_BMR
  • Body fat rate: DataType.DT_INSTANTANEOUS_BODY_FAT_RATE

    体内脂肪率:DataType.DT_INSTANTANEOUS_BODY_FAT_RATE
  • Height (unit: meter): DataType.DT_INSTANTANEOUS_HEIGHT

    高度(单位:米):DataType.DT_INSTANTANEOUS_HEIGHT
  • Water taken over a single drink (unit: liter): DataType.DT_INSTANTANEOUS_HYDRATE

    一份饮料中摄取的水(单位:升):DataType.DT_INSTANTANEOUS_HYDRATE
  • Nutrient intake over a meal: DataType.DT_INSTANTANEOUS_NUTRITION_FACTS

    一顿饭的营养摄入量:DataType.DT_INSTANTANEOUS_NUTRITION_FACTS
  • Weight (unit: kg): DataType.DT_INSTANTANEOUS_BODY_WEIGHT

    重量(单位:千克):DataType.DT_INSTANTANEOUS_BODY_WEIGHT

We will build a listening object for data changes that will be use from PendingIntent.

我们将为PendingIntent中将使用的数据更改构建一个侦听对象。

Let’s create our pendingIntent object.We will set this DataRegisterReceiver class as parameter on intent.

让我们创建未决对象,将这个DataRegisterReceiver类设置为intent的参数。

After this, we build a parameter object for registering a listener for data changes.

此后,我们将建立一个参数对象,用于注册用于数据更改的侦听器。

And as last step, we use the specified parameter object for registering a listener for data changes to call the data controller to add the listener.

最后一步,我们使用指定的参数对象注册用于数据更改的侦听器,以调用数据控制器来添加侦听器。

Calling the data controller to register a listener is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the registration is successful or not.

调用数据控制器以注册侦听器是异步操作。 因此,需要注册一个侦听器以监视注册是否成功。

We can call the following code to insert a data entry to trigger the created listener.

我们可以调用以下代码来插入数据条目以触发创建的侦听器。

注销更新的侦听器 (Unregistering a Listener for Updates)

We will use the specified parameter object for unregistering a listener for data changes to call the data controller to cancel the listener.

我们将使用指定的参数对象为数据更改注销侦听器,以调用数据控制器取消侦听器。

Calling the data controller to unregister a listener is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the unregistration is successful or not.

调用数据控制器以取消注册侦听器是异步操作。 因此,需要注册一个侦听器以监视注销是否成功。

在设备和云之间同步用户的健康和健康数据 (Syncing the User’s Fitness and Health Data Between the Device and Cloud)

We can call the syncAll method of DataController to sync data.

我们可以调用DataController的syncAll方法来同步数据。

Calling the data controller to sync data between the device and cloud is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the syncing is successful or not.

调用数据控制器在设备和云之间同步数据是异步操作。 因此,需要注册一个侦听器以监视同步是否成功。

从设备和云中清除用户的健康和健康数据 (Clearing the User’s Fitness and Health Data from the Device and Cloud)

We can call the clearAll method of the DataController to delete data inserted by the current app from the device and cloud.

我们可以调用DataController的clearAll方法从设备和云中删除当前应用插入的数据。

Calling the data controller to clear user data from the device and cloud is an asynchronous operation. Therefore, a listener needs to be registered to monitor whether the clearance is successful or not.

调用数据控制器从设备和云中清除用户数据是异步操作。 因此,需要注册一个侦听器以监视清除是否成功。

翻译自: https://medium.com/huawei-developers/exploring-the-huawei-health-kit-data-controller-35f45e3d4fef

华为ai开发套件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值