微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

蓝牙低功耗重量测量特征时间戳

如何解决蓝牙低功耗重量测量特征时间戳

我没有连接设备时正在缓冲数据,因此我需要实现时间戳记,以便知道何时测量了什么。

幸运的是,体重测量特性包括时间戳。

不幸的是,由于它不是普通数据类型,而且肯定不是单个字节,因此目前尚不清楚如何将数据写入程序包。

https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.weight_measurement.xml

我正在使用Adafruit的bluefruit arduino库,因此我尝试仅忽略该模式并在SI权重之后编写一个unix时间戳,但是毫不奇怪的是,该模式似乎不允许这样做,因此当出现以下情况时我看不到时间戳我收到通知(但是我仍然看到正确的体重读数)

这是date_time特征的链接,显然是它期望的格式 https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.date_time.xml

我尝试在nRF52 SDK中进行一些查找,以便查看是否可以通过其API更好地处理此问题,但是学习曲线有些陡峭,我只需要完成最后一部分即可使设备正常工作。

更新:

对于其他有此问题的人,解决方案原来是我使用的通知方法与adafruit示例中编写的方式相同

wmc.notify(notification,sizeof(notification))

因为我正在索引N x 7的缓冲数据数组,但是notification是指向我要提供的1 x 7数组中第一个元素的指针,所以sizeof总是返回4(大小我假设的地址长度),而不是最初写入的数组长度7

解决方法

weight_scale服务具有两个强制性特征:

<Characteristic type="org.bluetooth.characteristic.weight_scale_feature" name="Weight Scale Feature">
  <Requirement>Mandatory</Requirement>
<Characteristic type="org.bluetooth.characteristic.weight_measurement" name="Weight Measurement">
  <Requirement>Mandatory</Requirement>

weight_measurement特性(uuid =“ 2A9D”)中,第一个字节是标志。需要在<Bit index="1" size="1" name="Time stamp present">1的地方说会有一个“时间戳”字段。该“时间戳”字段将为:

<Field name="Year"> <Format>uint16</Format> = 2 bytes
<Field name="Month"> <Format>uint8</Format> = 1 byte
<Field name="Day"> <Format>uint8</Format> = 1 byte
<Field name="Hours"> <Format>uint8</Format> = 1 byte
<Field name="Minutes"> <Format>uint8</Format> = 1 byte
<Field name="Seconds"> <Format>uint8</Format> = 1 byte

这使“时间戳记”字段的宽度变为7个字节。

给出一个可行的示例,说明如何创建完整的数据包(如果您需要重量(以千克为单位))和一个时间戳,它需要10个字节长:

<Field name="Flags"> <Format>8bit</Format>  = 1 byte
<Field name="Weight - SI "> <Format>uint16</Format> = 2 bytes
<Field name="Time Stamp"> = 7 bytes

我使用Python计算数据包的值:

import struct

flags = 0b00000010 # Include time. SI units
weight_raw = 38.1 #  Weight of 38.1 Kg
weight = int((weight_raw/5)*1000) # Formula from XML
year = 1972
month = 12
day = 11
hour = 23
minute = 22
second = 8

packet = struct.pack('<BHHBBBBB',flags,weight,year,month,day,hour,minute,second)
print(packet)

会给一个10字节长的数据包:

b'\x02\xc4\x1d\xb4\x07\x0c\x0b\x17\x16\x08'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。