Broadcast characteristic value

Posted on
  • I'd like to broadcast a characteristic value so it can be read from the advertising information without connecting.

    Here's an Arduino example increments a counter and broadcasts the value - BroadcastCount.ino. Using nRF Connect, I can see the characteristic value. (See attached image.)

    According to the docs, it looks NRF.setServices broadcast property might do this, but I can't get it to work on the Puck.js.

    // Demonstrate broadcasting a characteristic value
    // Use nRF Connect to see the counter value without connecting
    
    let count = 0;
    
    NRF.setServices({
        0xEEE0 : {
          0xEEE1 : {
            value: count,
            readable: true,
            notify: true,
            broadcast: true,
            description: 'Count'
          }
        }
      }, { advertise: [ 'EEE0' ] });
    
    function notify() {
      NRF.updateServices({
        0xEEE0 : {
          0xEEE1 : {
            value: count,
            broadcast: true,
            notify: true
          }
        }
      });
    }
    
    setInterval(function() {
      count++;
      notify();
    }, 1000);
    

    Should broadcast update the service info or do I need to manually build this into the advertising data?


    1 Attachment

    • microbit-broadcast.png
  • You just need to use NRF.setAdvertising. There's a quick run-down of BLE and which functions in Puck.js you use for it here that might be handy: http://www.espruino.com/About+Bluetooth+­LE

    There's an example here that uses Manufacturerdata advertising as well that might be interesting: http://www.espruino.com/Puck.js+Advertis­ing

  • @Gordon I like your nRF API. It's really easy to set the Service Data. This works great.

    // Demonstrate broadcasting a characteristic value
    // Use nRF Connect to see the counter value without connecting
    
    let count = 0;
    
    NRF.setServices({
        0xEEE0 : {
          0xEEE1 : {
            value: [0, 0],
            readable: true,
            notify: true,
            broadcast: true,
            description: 'Count'
          }
        }
      }, { advertise: [ 'EEE0' ] });
    
    function notify() {
      const buffer = new Uint16Array([count]).buffer;
      NRF.updateServices({
        0xEEE0 : {
          0xEEE1 : {
            value: buffer,
            broadcast: true,
            notify: true
          }
        }
      });
      // Set the service data 0x16 to the count value
      NRF.setAdvertising({
        0xEEE0: buffer
      });
    }
    
    setInterval(() => {
      count++;
      if (count > 0xFFFF) {
        count = 0;
      }
      notify();
    }, 1000);
    

    1 Attachment

    • IMG_3549.png
  • Great! The service data 0xCC00 looks a bit odd though.

    Do you have an up to date firmware on your device? I don't see that here when I run your code.

  • 0xCC00 is fine, I just waited 5 minutes before taking a screenshot.

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

Broadcast characteristic value

Posted by Avatar for don @don

Actions