title: Android Bluetooth LE - Read float characteristic
body: <p>I am trying to read the float characteristic of a connected Bluetooth LE device(Genuino 101). For testing purposes, the device provides a FloatCharacteristic with the hardcoded Value '55.3'. While I am able to receive a string that somehow resembles the float, I cannot read an actual float value.<br>
  Here's the code fragment that handles a string:</p>
  
  <pre><code>// For all other profiles, writes the data formatted in HEX.
          final byte[] data = characteristic.getValue();
          if (data != null &amp;&amp; data.length &gt; 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());
          }
  </code></pre>
  
  <p>this is copied straight from the <a href="https://developer.android.com/samples/BluetoothLeGatt/index.html" rel="nofollow noreferrer">https://developer.android.com/samples/BluetoothLeGatt/index.html</a> BLE demo project from the android developer homepage.
  The intent is then handled by this snippet:</p>
  
  <pre><code>private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          System.out.println("Broadcast received");
          final String action = intent.getAction();
          if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
  
          } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
  
          } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
  
          } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
             displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
          }
      }
  };
  
  private void displayData(String data) {
      if (data != null) {
          System.out.println("Data Received: " + data);
      }
  }
  </code></pre>
  
  <p>Which leads to the output</p>
  
  <pre><code>I/System.out: Data Received: 33]B 
  I/System.out: 33 33 5D 42
  </code></pre>
  
  <p>So, set aside the swapped endianness, this is the correct hex value for 55.3f.<br>
  However, if I try to use characteristic.getFloatValue(), I only get garbage. Here's how I tried figuring out how to receive an actual float:</p>
  
  <pre><code>final byte[] data = characteristic.getValue();
          if (data != null &amp;&amp; data.length &gt; 0) {
              for (int i = 0; i&lt; 333; i++) {
                  try {
                      final float fData = characteristic.getFloatValue(BluetoothGattCharacteristic.FORMAT_FLOAT, i);
                      System.out.println("Offset = "+ i + ". Data that gets sent: " + fData + "/Data that we would expect: " + 55.3f);
                  } catch (Exception e) {
                      System.out.println("Exception at offset " + i);
                  }
              }
          }
  </code></pre>
  
  <p>the output is always</p>
  
  <pre><code>I/System.out: Offset = 0. Data that gets sent: Infinity/Data that we would expect: 55.3
  I/System.out: Exception at offset 1
  I/System.out: Exception at offset 2
  ...
  </code></pre>
  
  <p>What is my mistake here? Also, I'm not sure how the Offset parameter should be understood. Is it the offset in bit, in byte? Counting from MSB, from LSB?
  Also the documentation of getFloatValue() claims "returns float - Cached value of the characteristic at a given offset or null if the requested offset exceeds the value size. ". But the above snippet grossly exceeds the maximum size of any gatt characteristic, yet instead of returning 'null', the method call throws exceptions.
  So what's the correct way to get a float here?</p>