首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从BLE设备读取值

从BLE设备读取值
EN

Stack Overflow用户
提问于 2017-06-05 20:06:37
回答 1查看 1.3K关注 0票数 0

我拥有一个极地H10胸带,它的运作与蓝宝石低能量,并提供心率和心率变异性。

我想用Android应用程序读出这些值。由于官方BLE教程中的帮助,我能够连接到设备。现在的问题是从设备中读出心率和心率变异性值。每当设备上有一个新值可用时,我都要读出这个值(至少每秒钟有一个新值)。

我发现了以下代码:

代码语言:javascript
运行
复制
private static double extractHeartRate(
            BluetoothGattCharacteristic characteristic) {

        int flag = characteristic.getProperties();
        Log.d(TAG, "Heart rate flag: " + flag);
        int format = -1;
        // Heart rate bit number format
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        return heartRate;
    }



private static Integer[] extractBeatToBeatInterval(
            BluetoothGattCharacteristic characteristic) {

        int flag = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
        int format = -1;
        int energy = -1;
        int offset = 1; // This depends on hear rate value format and if there is energy data
        int rr_count = 0;

        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
            offset = 3;
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
            offset = 2;
        }
        if ((flag & 0x08) != 0) {
            // calories present
            energy = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, offset);
            offset += 2;
            Log.d(TAG, "Received energy: {}"+ energy);
        }
        if ((flag & 0x16) != 0){
            // RR stuff.
            Log.d(TAG, "RR stuff found at offset: "+ offset);
            Log.d(TAG, "RR length: "+ (characteristic.getValue()).length);
            rr_count = ((characteristic.getValue()).length - offset) / 2;
            Log.d(TAG, "RR length: "+ (characteristic.getValue()).length);
            Log.d(TAG, "rr_count: "+ rr_count);
            if (rr_count > 0) {
                Integer[] mRr_values = new Integer[rr_count];
                for (int i = 0; i < rr_count; i++) {
                    mRr_values[i] = characteristic.getIntValue(
                            BluetoothGattCharacteristic.FORMAT_UINT16, offset);
                    offset += 2;
                    Log.d(TAG, "Received RR: " + mRr_values[i]);
                }
                return mRr_values;
            }
        }
        Log.d(TAG, "No RR data on this update: ");
        return null;
    }

假设我与设备有连接,如何使用它提取心率和r-r间期(节拍到节拍间隔)?如果有人能做个简短的例子,我会很高兴的。此外,我想使用一个服务,以便它在后台运行,我可以做其他工作,它消耗更少的电池。但是这个服务应该有最高优先级,这样它就不会被杀死。

extractBeatToBeatInterval()方法中,有一个带有描述的int offset = 1;

这取决于听觉速率值的格式和是否有能量数据。

我不明白这件事的意义。我应该使用什么偏移值?

其次,如果连接中断,我希望得到一个通知,以便我能够处理它(例如显示一条消息)。我怎么能这么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-15 09:28:03

在这件事上我可以帮你几件事。对于连接中断,您可以这样处理

代码语言:javascript
运行
复制
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothGatt.STATE_CONNECTED) {
                Log.e(Constants.TAGGattConnectThread, "Connected to the GATT Server " + status + " " +newState);
                Log.e(Constants.TAGGattConnectThread, "Attempting to discover services");
                bluetoothGatt.discoverServices();
            }
            else if(newState == BluetoothGatt.STATE_DISCONNECTED)
            {
                Log.e(Constants.TAGGattConnectThread,"Band disconnected");
               //Write you disconnect handling code here
            }

您还可以在连接到关贸总协定时将“AutoConnect”选项设置为“True”。

代码语言:javascript
运行
复制
bluetoothGatt = bluetoothDevice.connectGatt(context, true, bluetoothGattCallback);

对于创建服务,您可以创建一个Sticky服务,除非强制停止,否则该服务将重新启动自身。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44376839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档