Find nearby beacons

Posted on
  • Hello there,
    I want my Puck to scan for beacons every 5 minutes(for example). I want to know about NRF.findDevices and NRF.setScan(what's the difference). Also I want to advertise the id of the beacon when is found. Can I scan continuously or the battery will be dead soon? Thanks in advance! (Code would be much appreciated).

  • Hi,

    So, just to be clear - you'd like to scan for iBeacons, and then have the Puck itself advertise the ID of any beacon that was found?

    Scanning itself uses quite a lot of power (12mA compared to 0.02mA normally), but the good news is you don't have to do it for long (maybe 2 seconds).

    • NRF.findDevices automatically scans for a certain length of time and aggregates all the beacons that are found - unless you're somewhere that's got 100s of beacons within range then this is the one you want.
    • NRF.setScan scans continuously and calls a function every time an advertising packet is received.

    Let me know if I'm right about the kind of thing you want above and I'll post up some code.

  • I have 3 iBeacons(4m distance from each other) and the Puck. I want the beacon id to be advertised whenever is found. So, I want continuous scanning.
    I also have a name for every beacon(kitchen, bedroom, etc..) Can I advertise that too?

  • It's unlikely there will be space in the advertising packet for a name and the UUID - you could do one or the other though.

    I want continuous scanning.

    You said originally you wanted to scan only every 5 minutes? I'm going to do that as an example, because if you scan continuously your battery will only last 10-15 hours.

  • Okay,
    Every 5 minutes then. Thanks Gordon!

  • Here you go.

    It scans every 5 minutes for 2 seconds, then sets its own manufacturer data to Espruino's ID (0x590) with the found device's UUID - otherwise it advertises normally.

    function advertiseBeacon(dev) {
      var uuid = new Uint8Array(dev.manufacturerData,2,16);
      NRF.setAdvertising({},{
        manufacturer:0x590, // espruino's manufacturer
        manufacturerData:uuid
      });
    }
    
    function doScan() {  
      NRF.findDevices(function(devs) {
        var found = undefined;
        devs.forEach(function(dev) {
          if (dev.manufacturer==76)
            found = dev;
        });
        
        if (found) advertiseBeacon(found); // advertise one we found
        else NRF.setAdvertising({},{}); // don't advertise any data
      }, 2000); // scan for 2 seconds  
    };
    
    setInterval(doScan, 5*60000); // 5 minutes
    
  • I wanted to say Mac Address of the beacon instead of UUID. Sorry for that.
    So, how to check and advertise the mac address? thanks --this is my final question by the way :)

  • dev.id contains the mac address as a string, so just change:

    var uuid = new Uint8Array(dev.manufacturerData,2,16);
    

    to

    var uuid = dev.id .substr(0,17).split(":").map(x=>parseInt­(x,16))
    
  • Can I get the major of the iBeacon as well?

  • Yes, but I think you could probably figure out how to do that yourself. It'll be in dev.manufacturerData like the UUID was.

  • I would be very thankful if you could you give me the line with iBeacon major value.

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

Find nearby beacons

Posted by Avatar for Pzr0 @Pzr0

Actions