Android 从可编程设备读取值

Android 从可编程设备读取值,android,bluetooth-lowenergy,android-bluetooth,Android,Bluetooth Lowenergy,Android Bluetooth,我有一个Polar H10胸带,它以blueooth低能量运行,提供心率和心率变异性 我想用Android应用程序读出这些值。由于中的帮助,我能够连接到设备。现在的问题是从设备中读取心率和心率变异性值。每次设备上有一个新值可用时,我都想读出这个值(并且至少每秒钟有一个新值) 我发现了以下几段代码: private static double extractHeartRate( BluetoothGattCharacteristic characteristic) {

我有一个Polar H10胸带,它以blueooth低能量运行,提供心率和心率变异性

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

我发现了以下几段代码:

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;
    }
private静态双提取心率(
蓝牙(特征){
int flag=characteristic.getProperties();
Log.d(标签“心率标志:+标志”);
int格式=-1;
//心率位数格式
如果((标志&0x01)!=0){
格式=BluetoothGattCharacteristic.format_UINT16;
Log.d(标签“心率格式UINT16”);
}否则{
格式=BluetoothGattCharacteristic.format_UINT8;
Log.d(标签“心率格式UINT8”);
}
最终整数心率=characteristic.getIntValue(格式1);
Log.d(TAG,String.format(“接收到的心率:%d”,心率));
返回心率;
}
私有静态整数[]ExtractBeatBeatInterval(
蓝牙(特征){
int flag=characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8,0);
int格式=-1;
int能量=-1;
int offset=1;//这取决于收听率值格式以及是否有能量数据
int rr_计数=0;
如果((标志&0x01)!=0){
格式=BluetoothGattCharacteristic.format_UINT16;
Log.d(标签“心率格式UINT16”);
偏移量=3;
}否则{
格式=BluetoothGattCharacteristic.format_UINT8;
Log.d(标签“心率格式UINT8”);
偏移量=2;
}
如果((标志&0x08)!=0){
//卡路里含量
能量=特征.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16,偏移量);
偏移量+=2;
Log.d(标签,“接收能量:{}”+能量);
}
如果((标志&0x16)!=0){
//RR的东西。
Log.d(标记“在偏移量处找到的RR文件:”+offset);
Log.d(标记,“RR长度:”+(characteristic.getValue()).length);
rr_计数=((characteristic.getValue()).length-offset)/2;
Log.d(标记,“RR长度:”+(characteristic.getValue()).length);
Log.d(标记“rr_计数:”+rr_计数);
如果(rr_计数>0){
整数[]mRr_值=新整数[rr_计数];
对于(int i=0;i
假设我已连接到设备,如何使用它提取心率和r-r间期(逐拍间期)?如果有人能举个简单的例子,我会很高兴的。另外,我想使用一个服务,使它在后台运行,我可以做其他工作,它消耗更少的电池。但是服务应该具有最高优先级,这样它就不会被杀死

extractBeatToBeatInterval()
方法中有一个
int offset=1和描述

这取决于收听率值格式以及是否有能量数据

我不明白这个提议的意思。我应该使用什么偏移值


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

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

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
            }
您还可以在连接Gatt时将“自动连接”选项设置为“真”

bluetoothGatt = bluetoothDevice.connectGatt(context, true, bluetoothGattCallback);

为了创建服务,您可以创建一个粘性服务,除非强制停止,否则该服务将自动重新启动