Wake, send BLE a few times and sleep?

Can I just clarify something regarding BLE notifications. Does the data only get sent each time you use a setValue?

I have a small test program that picks a random number every 2 seconds and uses setValue to store that in a BLE characteristic with type NOTIFY. On the same service, I have added temperature and humidity, also as NOTIFY. Using nRF Connect, I can see the regular changing of the random number. However I don’t see a value for temperature or humidity appear until it next does the reading, even though the serial console says a value was set before I did a connect from the app.

What I eventually want to do is have a controller wake up, take some sensor readings, set this in BLE data, run for 5 seconds in the hope the gateway picks it up, but not care if the gateway misses it for some reason and go back to sleep. Is NOTIFY and setting values before I turn BLE on, the best way to achieve this?

Also, is there a way for a device to read it’s own BLE address? I can see how it can read addresses from scan results but I’m struggling to find it’s own.

If you mean calling BLE.on() then I’d rather turn it on (and also call BLE.begin()) before setting the value.

I’d try it with this BLE.address() :wink:

I managed to get some time to look at this again. I’m not sure why I asked about BLE.address(), I thought I had not got it worked, looked at your suggestion, went to write it into my code and it was already there! Owning children and sitting in the same chair all day working from home is not the best for the brain.

I think some of it was me trying to get the concepts of how BLE works. I wanted the sensor to wake up from sleep and broadcast a load of data for a bit, hope the central device noticed at some point and then sleep. That is not quite how BLE works.

As you need to have the devices connected to send characteristics, I think the best method for the peripheral device is to:

  • Wake
  • Take the sensor readings
  • Turn BLE on
  • When connected (the central device should find this with a scan and connect), use setValue on all the characteristics with the values I have just read
  • Go back into deep sleep
  • Unless a minimum time has passed (30 seconds) and deep sleep again.

The central device will scan and connect to the first device it can see offering the correct service UUID, get the values from the characteristics and then send them on with a straight forward Particle.publish.

With a 15 minute deep sleep and only three sensor devices, I can’t see any reason why any should be missed.

Sound reasonable?