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 设备发送带有指示的测量数据。
  6. A&D 设备识别到 CCCD 设置为“2”并在连接后 5 秒内同步时间和日期后,发送带指示的数据。
  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);
            }

问题是这段代码不返回任何数据!

4

2 回答 2

3

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

于 2015-07-24T11:49:07.170 回答
1

你有没有例子,我用相同的设备尝试这个,我无法获得你尝试的信息

public String response() {
if (mConnected) {
    mBluetoothLeService.readCharacteristic(characteristica);
    byte response[] = characteristica.getValue();
    String respuesta = ReadBytes(response);
    mBluetoothLeService.disconnect();
    return respuesta;
} else {
    return null;
}
}
于 2015-11-06T04:07:25.703 回答