Advertise Mag reading when button pushed

Posted on
  • Is there a tutorial, that I can't find, that shows code to advertise the magnetometer values in a characteristic when the button is pushed?

    Thank you.

  • There isn't, but try:

    setWatch(function() {
      var m = Puck.mag();
      NRF.setAdvertising({
        0x1809 : [m.x>>8,m.x,m.y>>8,m.y,m.z>>8,m.z]
      });
    }, BTN, {repeat:true,debounce:50,edge:"rising"})­;
    

    That should pack the magnetometer data into 6 bytes that you could then read. Hope that helps!

  • Thank you for the code sample, I will put it into place tonight.

  • Hi Gordon,

    On Puck we have: [-21, -5126,5,1289, -3,-747]
    On RPi3 we have: [235, 250,5, 9,253, 21]

    How can we obtain the advertised mag values ?

  • In JavaScript?

    It seems I got the endianness wrong, so you can't just do: new Int16Array((new Uint8Array([235, 250,5, 9,253, 21])).buffer)

    Instead you need:

    var a = [235, 250,5, 9,253, 21];
    var d = [(a[0]<<8)|a[1], (a[2]<<8)|a[3], (a[4]<<8)|a[5]];
    for (var i=0;i<3;i++) if (d[i]&32768) d[i]-=65536;
    // results in d
    

    I guess you're using Java in Android - that code should be pretty easy to convert.

    There are other ways of converting numbers from byte arrays (utilities in various different languages), but the code above is pretty universal.

  • Hi Gordon,

    Thank you for your answer. Yes, one way of getting the data on Android is:

    int value = new BigInteger(new byte[]{a[i], a[i + 1]}).intValue();
    
  • Hi @Gordon

    How did you test the advertising? I'm trying to test it using a WiPy and the nRF connect app, but I don't see any data.

  • Personally I use the NRF Connect app to scan for advertising data.

    You're trying to scan for advertising data that's coming from a Puck.js?

    Are you disconnected from the Puck.js? Puck.js (like most BLE devices) will only advertise when there is no device with an active BLE connection to it.

  • Yes, I'm trying to scan for advertising data that's coming from my Puck.js.

    I indeed had a Bluetooth connection open (for the IDE). Thank you!

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

Advertise Mag reading when button pushed

Posted by Avatar for Terrence @Terrence

Actions