• Hi,

    im totally new with Puck.js and Javascript at all but I'm trying to advertise a own private Characteristic with 2 Services.

    One service to controll the LEDs from the Puck from a external device and the other service for button state from Puck.

    First Service is writable from external device, the second one is just readable but with notifications.

    
    var timeout;
    function flag() {
      if (timeout) clearTimeout();
      LED1.set();
      timeout = setTimeout(function() {
        timeout = undefined;
        LED2.set();
      },2000);
    }
    
    //setWatch(flag, BTN, {repeat:true});
    
    // New code to define services
    NRF.setServices({
      "3e440001-f5bb-357d-719d-179272e4d4d9": {
        "3e440002-f5bb-357d-719d-179272e4d4d9": {
          value : [0],
          maxLen : 1,
          writable : true,
          onWrite : function(evt) {
            // When the characteristic is written, raise flag
            flag();
          }
        }
      }
    }, { uart : false });
    // Change the name that's advertised
    NRF.setAdvertising({}, {name:"BTN"});
    console.log("CONTROL BUTTON");
    

    That code works, if I'm writing into the service the LED1 rises. But i want to check what value is transmitted. But I cant find any documentation about NRF.setServices so I dont know how to read out the value.

    How do I make the second service with button state?

    A bit off topic question: As I saw the Puck only can connect with one device at once, thats ok but later I want that the code is execute during boot, is it still possible to access from Espruino IDE then?

    EDIT: Okay, I found that code:
    digitalWrite([LED3,LED2,LED1], evt.data[0]); which reads the value and works fine. But how to do the second task?

    Also: I need to restart iPhone (use BLE Scanner) after changing the Services. Otherwise it will not displayed. Very irritating. Is there a workaround?

  • Update:

    Im done with second characteristic and it works. But with notify it stocks.

    Here is the Code:

    NRF.setServices({
      "b0d0cdff-56fa-4bee-b611-874c82f0c313": {
       "9ed52c1b-73e4-47a9-b335-8e12cfa22fdc": {
          value : 0,
          writable : false,
          notify:true
        }
      }
    }, { uart : false });
    

    And the update code:

    NRF.updateServices({
      "b0d0cdff-56fa-4bee-b611-874c82f0c313" : {
        "9ed52c1b-73e4-47a9-b335-8e12cfa22fdc" : {
          value : 1,
          notify: true
        }
      }
    });
    

    The state switch and I can set Notify into the BLE App but no notification received.

  • Fri 2019.07.19

    Hi @user101905, there are many links to assist that I just posted in a separate user thread:

    http://forum.espruino.com/conversations/336276/

     

    'But I cant find any documentation about NRF.setServices'

    This is a good starting point:

    https://www.espruino.com/Reference#l_NRF_setServices

  • Ok, so it looks like your first issue (getting the data in a write notification) is sorted?

    How do I make the second service with button state?

    You just add an extra element to the object. For example:

    NRF.setServices({
      "3e440001-f5bb-357d-719d-179272e4d4d9": {
        "3e440002-f5bb-357d-719d-179272e4d4d9": {
          value : [0],
          maxLen : 1,
          writable : true,
          onWrite : function(evt) {      }
        },
        "3e440003-f5bb-357d-719d-179272e4d4d9": {
          value : [0],
          maxLen : 1,
          writable : true,
          onWrite : function(evt) {  }
        }
      }
    }, { uart : false });
    

    What you're doing with updateServices looks fine. All I'd say is it's far more efficient to use the same basic UUID and to change the second group of 4 digits as you did in the first example.

    I just tried this code and it works fine:

    NRF.setServices({
     "3e440001-f5bb-357d-719d-179272e4d4d9": {
       "3e440003-f5bb-357d-719d-179272e4d4d9": {
          value : 0,
          writable : false,
          notify:true
        }
      }
    });
    
    var n=1;
    setWatch(function() {
      NRF.updateServices({
        "3e440001-f5bb-357d-719d-179272e4d4d9": {
          "3e440003-f5bb-357d-719d-179272e4d4d9": {
            value : n++,
            notify: true
          }
        }
      });
    }, BTN1, {repeat:true,edge:"rising"});
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Puck Advertising Private Characteristic and r/w

Posted by Avatar for user101905 @user101905

Actions