如何从这个类中取出字节值?

时间:2016-11-16 08:35:48

标签: java android bluetooth-lowenergy

我想从这个类中取出字节值,以便我可以解析它,或者如果它可以解析它并获得第5和第6个字节值。

private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    // This is special handling for the Heart Rate Measurement profile.  Data parsing is
    // carried out as per profile specifications:
    // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        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));
            intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());




        } else {
            // For all other profiles, writes the data formatted in HEX.


        }

    }


    sendBroadcast(intent);

}

这是如何工作的,我踩到一个刻度,它发送给我这些字节:

00 00 00 00 02 02 00 00 00 00 00 00 00 00 00 00

存储在变量'数据'。我该如何处理数据'出去在另一个班级使用?

权重数据位于字节5和6上。如果转换字节5和6的十六进制值,则该示例中为' 0202'十进制为514(51.4千克)。

我需要将字节数据用于另一个类来获取kg数据。我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

您可以简单地获取这两个字节并在下面应用:

int result = ByteBuffer.wrap(bytes).getInt();

如果您想了解更多信息:HERE1HERE 2

相关问题