从健康跟踪器获取数据

huangapple go评论61阅读模式
英文:

Get Data From Fitness Tracker

问题

我正在使用Kotlin开发移动应用,需要从健康追踪器获取数据(例如心率)。但我找不到通用的API来完成这个任务。

我知道一些健康追踪器,如小米手环和华为手表,有官方API,但我需要一种通用的方法。

英文:

I'm developing mobile app on Kotlin and I need to get data (ex. Heart Rate) from Fitness Tracker. But I don't find universal API for this task.

Also I know, that some fitness tracker like Mi Band, Huawei Watch has a official API, but I need a universal method.

答案1

得分: 2

你的问题不够清晰。但是,要回答你所问的任何问题,你必须使用蓝牙库来连接设备(健身追踪器)。

蓝牙设备,如健身追踪器,通过服务传输数据。每个蓝牙设备都有其自己的服务集。每个服务都有多个特征。

  1. 一旦连接到设备,使用蓝牙库获取设备支持的服务列表。

每个服务都有一个UUID。你可以根据UUID来确定需要哪个服务。蓝牙有一个官方文档可供参考:https://btprodspecificationrefs.blob.core.windows.net/assigned-numbers/Assigned%20Number%20Types/Assigned_Numbers.pdf

例如,如果你需要“心率”数据,那么你需要具有UUID“0x180d”的服务。

  1. 一旦找到你需要的服务(在本例中是“0x180d”),获取该服务中存在的特征列表。并在此链接上获取该服务的官方文档:https://www.bluetooth.com/specifications/specs/

打开链接,搜索所需的服务(在我们的示例中是“心率服务”,即“HRS”)。仔细阅读文档,了解如何提取数据。文档中列出了它支持的特征列表以及数据的格式。检查你的设备支持哪组特征,并相应地操作。

对于心率,你要寻找的特征是“0x2a37”。同样,你可以参考上面的链接来获取相关信息。

  1. 一旦获得所需的特征,如果需要读取数据或随时间监听数据,请阅读特征的文档。

例如,心率特征“0x2a37”仅支持监听数据。你将获得一个整数列表,你需要将其解析为可读格式。参考以下答案来完成此操作:https://stackoverflow.com/a/65458794/12555686

就这样,你需要在每次需要某种活动的数据时遵循这些步骤。

还有一件事,许多健身追踪器(如MiBand、Firebolt等)使用自定义服务。例如,步数数据必须通过“Physical Activity Monitor Service”(PAMS)传输,但MiBand没有此服务。它通过自定义服务“0xfee0”和特征“0x0007”传输有关步数的数据。因此,这将是一项棘手的工作。

你可以参考一些GitHub存储库来解决这个问题。截止目前,我只知道一些MiBand的GitHub存储库。但我认为其他品牌也必须遵循类似的模式。

以下是一些项目链接:

  1. https://github.com/creotiv/MiBand2/tree/master(python)
  2. https://github.com/dkhmelenko/miband-android/tree/master(kotlin)
  3. https://github.com/simranss/my_fit(flutter)(不仅适用于MiBand,还适用于所有健身追踪器)

希望这有所帮助。

英文:

Your question is not clear. But to answer whatever you have asked, you must use a bluetooth library to connect to the device (fitness tracker).

Bluetooth devices such as fitness trackers communicate data through services. Every bluetooth device has its own set of services. Every service has multiple characteristics.

  1. Once you connect to the device, use the bluetooth library to get the list of services, that the device supports.

Every service has a UUID. You can check which service you need according to the UUID. Bluetooth has an official document for this purpose. You can refer to this: https://btprodspecificationrefs.blob.core.windows.net/assigned-numbers/Assigned%20Number%20Types/Assigned_Numbers.pdf

For example, if you need "heart rate" data, then you need the service with the UUID "0x180d".

  1. Once you find the service you wanted (in this example "0x180d"), get the list of characteristics present in that service. And get the official documentation for that service on this link: https://www.bluetooth.com/specifications/specs/

Go on the link, and search for the desired service (In our example, "heart rate service" i.e. "HRS". Read the documentation carefully on how to extract data. The documentation has the list of characteristics it supports and the format, the data is present in. Check what set of characteristics your device is supporting and work accordingly.

For heart rate, the characteristic you are looking for is "0x2a37". Again you can refer to the above links for this purpose.

  1. Once you have the desired characteristic, read the documentation of the characteristic, if you have to read the data or listen to the data over time.

For example, the heart rate characteristic "0x2a37", only supports listening to the data. The data you will get is a list of integers. You need to parse it into readable format. Refer to this answer for that purpose: https://stackoverflow.com/a/65458794/12555686

And there you have it, you need to follow these steps every time you need data for a certain activity.

One more thing, a lot of fitness trackers (MiBand, Firebolt, etc..) use custom services. For example, the steps data has to be communicated through the "Physical Activity Monitor Service" (PAMS), but MiBand doesn't have this service. It communicates the data regarding steps through its custom service "0xfee0" with the characteristic "0x0007". So that is going to be a tricky job.

You can refer to some GitHub repositories for this purpose. As of now, I know only some GitHub repositories and they are for MiBand. But I assume that other brands must also follow a similar pattern.

Here are the links to some projects:

  1. https://github.com/creotiv/MiBand2/tree/master (python)
  2. https://github.com/dkhmelenko/miband-android/tree/master (kotlin)
  3. https://github.com/simranss/my_fit (flutter) (this is not just for MiBand, but for every fitness tracker)

Hope, this helped

huangapple
  • 本文由 发表于 2023年6月9日 06:16:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定