• Hello,

    I've been playing around with BLE with Espruino Puck, and it's been quite pleasant so far. I've got a question and I can't find the answer- which means it's probably staring me in the face.

    I'm experimenting/learning and used NRF.setServices to create a characteristic and I can read the value from it using Nordic nRF connect on my phone. I can also use NRF.updateServices to update the value, and if I use notify the value automatically updates on the phone screen. Yay: Bluetooth success!

    My question: Suppose (hypothetically) I turned the puck into a BLE thermometer. This might be used really infrequently, so I don't want to use setInterval with updateServices as this would keep taking temperature readings which were never used, and use more power than necessary.

    I only want to do the temperature reading (or whatever power hungry thing) when the data is requested by the phone.

    I want something like an 'onRead' function for the characteristic, much as there is onWrite. So my code would be something like

    onRead : function() { return doReallyPowerHungryThingThatReturnsAValu­e() }

    I can't find out how to do such a thing. Is this possible or is it a limitation of BLE/The Nordic SDK? (I can imagine some sort of timing restriction in BLE or something - what if my function took a long time to execute.).

    I'm grateful for any teachings!

    Thanks,

    Colin

    Example code:
    
    NRF.setServices({
      "047b0001-37e3-457d-3763-2a9b2d24cbb2" : {
        "047b0002-37e3-457d-3763-2a9b2d24cbb2": {
          maxLen : 10,
          value : "Unknown",
          readable : true,
          writable : true,
          notify : true,
          onWrite : function(evt) {
            var text = String.fromCharCode.apply(String, evt.data);
            console.log("Message received: ",text);
          },
        }
      }
    }, { uart : false });
    
    function updateTemperature() {
      NRF.updateServices({
        "047b0001-37e3-457d-3763-2a9b2d24cbb2" : {
          "047b0002-37e3-457d-3763-2a9b2d24cbb2" : {
            value : E.getTemperature().toString(),
            notify : true,
          }
        }
      });
    }
    
About

Avatar for ColinP @ColinP started