如何使用Swift和Core蓝牙从iOS中的Polar H10获得心率测量的值?

问题描述 投票:0回答:1

我将Polar H10用作心率监测器,我想使用Swift和Core蓝牙获得iOS的心率测量特性中的值。

以下是CBPeripheralDelegate的适当回调方法中的代码:

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

    print("didUpdateValueFor")

    if let error = error {
        print("error:", error)
    }

    guard let value = characteristic.value else {
        return
    }

    print("value:", value)

    guard let stringValue = String(data: value, encoding: .utf8) else {
        return
    }

    print("string value:", stringValue)

}

Heart Rate Measurement的官方蓝牙网页显示了此未格式化的信息:

必填8位,注意:心率测量值字段的格式取决于标志字段的位0。 C1 uint8 org.bluetooth.unit.period.beats_per_minute注意:心率测量值字段的格式取决于“标志”字段的位0。 C2 uint16 org.bluetooth.unit.period.beats_per_minute能源消耗字段的存在取决于“标志”字段的第3位。 C3 uint16 org.bluetooth.unit.energy.joule C4 uint16 org.bluetooth.unit.time.second分辨率为1/1024秒上表中的字段按从LSO到MSO的顺序排列。 LSO =最低有效八位位组,MSO =最高有效八位位组。

我不知道文档的含义。特别是我不明白C1,C2,C3和C4是指什么。

character.value是数据类型。我该如何处理该数据对象?如何获得所需的价值?我首先要获得RR值。

[类似的帖子,但是它们对我没有帮助,因为他们使用不同的工具和语言,或者没有很好的答案。

更新:

我自从发现Heart Rate Measurement的页面是一个xml文件。使用可以显示该文件的应用程序打开该文件后,我可以从该文件中获取所需的信息。我第一次看到它是在Safari中。它不能很好地显示xml文件。这是文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright 2011 Bluetooth SIG, Inc. All rights reserved. -->
<Characteristic xsi:noNamespaceSchemaLocation="http://schemas.bluetooth.org/Documents/characteristic.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="org.bluetooth.characteristic.heart_rate_measurement" uuid="2A37" name="Heart Rate Measurement">
    <InformativeText>
    </InformativeText>
    <Value>
        <Field name="Flags">
            <Requirement>Mandatory</Requirement>
            <Format>8bit</Format>

            <BitField>
                <Bit index="0" size="1" name="Heart Rate Value Format bit">
                    <Enumerations>
                        <Enumeration key="0" value="Heart Rate Value Format is set to UINT8. Units: beats per minute (bpm)" requires="C1" />
                        <Enumeration key="1" value="Heart Rate Value Format is set to UINT16. Units: beats per minute (bpm)" requires="C2" />
                    </Enumerations>
                </Bit>
                <Bit index="1" size="2" name="Sensor Contact Status bits">
                    <Enumerations>
                        <Enumeration key="0" value="Sensor Contact feature is not supported in the current connection" />
                        <Enumeration key="1" value="Sensor Contact feature is not supported in the current connection" />
                        <Enumeration key="2" value="Sensor Contact feature is supported, but contact is not detected" />
                        <Enumeration key="3" value="Sensor Contact feature is supported and contact is detected" />
                    </Enumerations>
                </Bit>

                <Bit index="3" size="1" name="Energy Expended Status bit">
                    <Enumerations>
                        <Enumeration key="0" value="Energy Expended field is not present" />
                        <Enumeration key="1" value="Energy Expended field is present. Units: kilo Joules" requires="C3"/>
                    </Enumerations>
                </Bit>
                <Bit index="4" size="1" name="RR-Interval bit">
                    <Enumerations>
                        <Enumeration key="0" value="RR-Interval values are not present." />
                        <Enumeration key="1" value="One or more RR-Interval values are present." requires="C4"/>
                        </Enumerations>
                </Bit>
                <ReservedForFutureUse index="5" size="3"></ReservedForFutureUse>
                </BitField>
        </Field>
        <Field name="Heart Rate Measurement Value (uint8)">
              <InformativeText>
                Note: The format of the Heart Rate Measurement Value field is dependent upon bit 0 of the Flags field.
              </InformativeText>
            <Requirement>C1</Requirement>
            <Format>uint8</Format>
            <Unit>org.bluetooth.unit.period.beats_per_minute</Unit>

        </Field>    

         <Field name="Heart Rate Measurement Value (uint16)">
              <InformativeText>
                Note: The format of the Heart Rate Measurement Value field is dependent upon bit 0 of the Flags field.
              </InformativeText>
            <Requirement>C2</Requirement>
            <Format>uint16</Format>
            <Unit>org.bluetooth.unit.period.beats_per_minute</Unit>

        </Field>       

        <Field name="Energy Expended">
            <InformativeText>The presence of the Energy Expended field is dependent upon bit 3 of the Flags field.</InformativeText>
            <Requirement>C3</Requirement>
            <Format>uint16</Format>
            <Unit>org.bluetooth.unit.energy.joule</Unit>

        </Field>
        <Field name="RR-Interval">
            <InformativeText>
               <!-- The presence of the RR-Interval field is dependent upon bit 4 of the Flags field. 
                <p>The RR-Interval value represents the time between two R-Wave detections.</p> 

                <p>Because several RR-Intervals may be measured between transmissions of the HEART RATE MEASUREMENT characteristic, 
                multiple RR-Interval sub-fields may be present in the characteristic. The number of RR-Interval sub-fields present 
                is determined by a combination of the overall length of the characteristic and whether or not the characteristic contains 
                the Energy Expended field.</p>

                <p>Where there are multiple RR-Interval values transmitted in the HEART RATE MEASUREMENT characteristic, the field uses the following format:</p>
                <p>RR-Interval Value 0 (LSO...MSO), RR-Interval Value 1 (LSO...MSO), RR-Interval Value 2 (LSO...MSO), RR-Interval Value n (LSO...MSO).</p>
                <p>Where the RR-Interval Value 0 is older than the RR-Interval Value 1.</p>
                <p>RR-Interval Value 0 is transmitted first followed by the newer measurements.</p>-->

            </InformativeText>
            <Requirement>C4</Requirement>
            <Format>uint16</Format>
            <Unit>org.bluetooth.unit.time.second</Unit>
            <Description>Resolution of 1/1024 second</Description>



        </Field>
    </Value>
   <Note> <p>The fields in the above table are in the order of LSO to MSO. Where LSO = Least Significant Octet and MSO = Most Significant Octet.</p>
   </Note>
</Characteristic>
ios swift bluetooth core-bluetooth gatt
1个回答
1
投票

首先,您需要将特征值数据转换为UInt8数组,然后需要检查您的心率传感器是否在该索引处提供值。我在我的项目中使用以下代码,并且工作正常。希望它也会对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.