Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Bluetooth Low Energy Heart Game Example - reinterpret_cast?

Bluetooth Low Energy Heart Game Example - reinterpret_cast?

Scheduled Pinned Locked Moved Solved General and Desktop
bluetooth low ecastingc++
4 Posts 3 Posters 533 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TUStudi
    wrote on last edited by aha_1980
    #1

    Hello,

    in the bluetooth low energy heart game example provided by qt there is this piece of code:

    void DeviceHandler::updateHeartRateValue(const QLowEnergyCharacteristic &c, const QByteArray &value)
    {
        // ignore any other characteristic change -> shouldn't really happen though
        if (c.uuid() != QBluetoothUuid(QBluetoothUuid::HeartRateMeasurement))
            return;
    
        auto data = reinterpret_cast<const quint8 *>(value.constData());
        quint8 flags = *data;
    
        //Heart Rate
        int hrvalue = 0;
        if (flags & 0x1) // HR 16 bit? otherwise 8 bit
            hrvalue = static_cast<int>(qFromLittleEndian<quint16>(data[1]));
        else
            hrvalue = static_cast<int>(data[1]);
    
        addMeasurement(hrvalue);
    }
    
    

    So after the characteristics have changed (new values are available) this slot ist called. Can anyone explain to me, what this code exactly does? (Noobfriendly for a C++ beginner? :) What would be the output if I just would print the value without making this reinterpret_cast and so on

    Thank you!

    Gojir4G 1 Reply Last reply
    0
    • T TUStudi

      Hello,

      in the bluetooth low energy heart game example provided by qt there is this piece of code:

      void DeviceHandler::updateHeartRateValue(const QLowEnergyCharacteristic &c, const QByteArray &value)
      {
          // ignore any other characteristic change -> shouldn't really happen though
          if (c.uuid() != QBluetoothUuid(QBluetoothUuid::HeartRateMeasurement))
              return;
      
          auto data = reinterpret_cast<const quint8 *>(value.constData());
          quint8 flags = *data;
      
          //Heart Rate
          int hrvalue = 0;
          if (flags & 0x1) // HR 16 bit? otherwise 8 bit
              hrvalue = static_cast<int>(qFromLittleEndian<quint16>(data[1]));
          else
              hrvalue = static_cast<int>(data[1]);
      
          addMeasurement(hrvalue);
      }
      
      

      So after the characteristics have changed (new values are available) this slot ist called. Can anyone explain to me, what this code exactly does? (Noobfriendly for a C++ beginner? :) What would be the output if I just would print the value without making this reinterpret_cast and so on

      Thank you!

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by Gojir4
      #2

      Hi @TUStudi,

      QByteArray::constData() returns a const char* value, which should be converted to const unsigned char* or const quint8 * (which is the same) in this case to interpret the heart rate data correctly.

      Printing constData() without casting could truncate data displayed as a const char* is considered to be a string and first '\0' (0x00) will be interpreted as the end of the string in this case, which is not the case with an unsigned char* array.

      I'm not an expert of BLE but I guess const QByteArray &value is used as a generic container here and is interpreted (casted) differently according to the case.

      Hope it helps

      aha_1980A 1 Reply Last reply
      2
      • Gojir4G Gojir4

        Hi @TUStudi,

        QByteArray::constData() returns a const char* value, which should be converted to const unsigned char* or const quint8 * (which is the same) in this case to interpret the heart rate data correctly.

        Printing constData() without casting could truncate data displayed as a const char* is considered to be a string and first '\0' (0x00) will be interpreted as the end of the string in this case, which is not the case with an unsigned char* array.

        I'm not an expert of BLE but I guess const QByteArray &value is used as a generic container here and is interpreted (casted) differently according to the case.

        Hope it helps

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by aha_1980
        #3

        @Gojir4 is mostly right, but the null termination is not the problem.

        The problem is, that char is most often signed, so doing arithmetic oprations like bit shifting might lead to wrong results.

        Hence the cast to an unsigned array.

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        1
        • T Offline
          T Offline
          TUStudi
          wrote on last edited by
          #4

          Thank you both very much.

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved