setAdvertisting serviceData data length max?

Posted on
  • Trying to advertise last button press epoch time. Whenever I do something like the following:

    var buttonEpoch = 0;
    
    function updateAdvertising() {
        data = totpObj.getOTP("");
        NRF.setAdvertising({0xFFFF : [buttonEpoch]});
    }
    
    setWatch(function() {
        var epoch = Math.round(new Date().getTime() / 1000.0);
        buttonEpoch=epoch;
        updateAdvertising();
    }, BTN, { edge:"rising", repeat:true, debounce:50 });
    

    Seems like the serviceData is only 2 bytes. Is this a known limitation of using advertising for data?

    I am able to put large integers into the manufacturing data but i'm using that now and dont have enough room.

  • So actually I figured it out that I was running int he max_size of a single advertistment packet.

    I instead tried to split it up into 2 advertistment packets but it was not working right. Is the below code something that should work?

    NRF.setAdvertising([{0xFFFF : [buttonEpoch]},{0x180F:[batteryLevel]}])­;
    
  • the code you have should work, but you're supplying an array with one element, so it'll only be sending one byte (0..255) worth of data.

    You can do:

    NRF.setAdvertising({0xFFFF : [buttonEpoch>>24,buttonEpoch>>16,buttonE­poch>>8,buttonEpoch]});
    

    which will transmit 4 bytes that can be reconstructed into a 32 bit int. Normally you'd have ... &255 there but it's sort of implicit since only the bottom 8 bits of each element is used anyway.

    If you want something that looks a bit nicer you can use DataView instead of all the bit shifting too.

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

setAdvertisting serviceData data length max?

Posted by Avatar for Enclavet @Enclavet

Actions