安卓蓝牙获取心率测量结果

3
我想获取"A&D UA-651BLE"设备的HRM值。根据该设备的数据表,需要执行以下操作来获取HRM值:
  1. 将应用设置为配对模式以开始扫描。
  2. 按照每个说明手册启动A&D BLE设备的配对。
  3. 在配对模式下,应用程序应将时间和日期以及任何其他设备设置设为A&D BLE设备。配对成功后,A&D BLE设备屏幕上显示“End”。
  4. 进行一次测量并完成测量,然后A&D BLE设备启动带广告的BLE连接。应用程序开始扫描,间隔合适,以便尽快捕获A&D BLE设备的广告。
  5. 在初始连接或配对时,应用程序将“2”设置为CCCD(客户端特性配置描述符),以便A&D BLE设备使用Indication发送测量数据。
  6. 在A&D设备识别到CCCD设置为“2”以及在连接后5秒内同步时间和日期之后,使用Indication发送数据。
  7. 如果超时设置了CCCD和时间和日期,则A&D BLE设备不会发送数据,并将数据存储在内存中。在下次成功连接时,A&D BLE设备可以发送存储的数据。
这是我的服务代码:
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }

        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

        // This is specific to Heart Rate Measurement.
        if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                    UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(descriptor);
        }
    }

这是读取数据的方法:

final byte[] data = characteristic.getValue();
            if (data != null && data.length > 0) {
                final StringBuilder stringBuilder = new StringBuilder(data.length);
                for(byte byteChar : data)
                    stringBuilder.append(String.format("%02X ", byteChar));
                Log.e("HRM value",stringBuilder.toString());
                dataComposition.put(characteristic.getUuid().toString(),stringBuilder.toString());
                intent.putExtra(EXTRA_DATA,dataComposition);
            }

这段代码的问题在于它没有返回任何数据!!
2个回答

3

有一个与此完全相符的Android开源项目示例,最简单的选择是克隆android-BluetoothLeGatt代码,构建并将其与您自己的代码进行比较。如果您无法发现差异/问题,只需部署两个应用程序并逐步执行两组代码即可。拥有一些已知的工作代码也有助于排除HRM未正常运行的可能性。


谢谢提供这个链接,我看了一下这个项目,问题是我正在使用的设备需要写操作,而这个项目并没有显示我们可以写入值和描述符。 - user4844052

1

你有例子吗?我尝试使用相同的设备,但无法获取信息。我试过:

public String response() {
if (mConnected) {
    mBluetoothLeService.readCharacteristic(characteristica);
    byte response[] = characteristica.getValue();
    String respuesta = ReadBytes(response);
    mBluetoothLeService.disconnect();
    return respuesta;
} else {
    return null;
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接