Sending Floats over BLE

Posted on
  • [Related post](http://forum.espruino.com/conversations/­306536/

    In my case my Puck is notifying a .js app of a change of a float32 value.
    I tried using print() but this converts a float32 into a ascii byte-string. This is sort of OK but rather messy for the app to have to convert back to a Float, not to mention potential loss of precision.

    Is there a way to send the float in its natural 4-byte form so that converting back to float32 can be easily done by the app?

    Thx.
    Paul

  • Yes, there are 2 easyish ways - Float32Array and DataView.

    DataView is the nicest though:

    var arr = new Uint8Array(4);
    d = new DataView(arr.buffer) ;
    d.setFloat32(0,1234);
    print(arr) // it contains [68, 154, 64, 0]
    

    You can pack lots of different types of data (double,float,int,short,byte,etc) with different endianness, all in the same array. Check out the DataView reference for the various methods: http://www.espruino.com/Reference#DataVi­ew

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Sending Floats over BLE

Posted by Avatar for paul_tanner @paul_tanner

Actions