Make function return variable from within nested "IF" routines


In the code for the device side of BLE112 on GitHub, there’s this function I’d like to modify slightly to get just the “beacon” variable (a table) as the final value reported from the function in a way that I can store such outcome into a global variable (let’s say: myBeacons<-{}). How can I do that? Where can I assign the “return” statement of the function to achieve that?

function discover_mode(active = false) {

// Setup the scanning in passive mode
ble112.gap_set_scan_parameters(75, 50, active ? 1 : 0);

// start scanning for peripherals

// Handle incoming scan responses here
ble112.on("gap_scan_response", function(event) {
    // Parse the advertising packet
    foreach (advdata in {
        if (advdata.type == BLE_GAP_AD_TYPES.GAP_AD_TYPE_MANUFACTURER_DATA) {
            // This is an ibeacon
            if (, 4) == "\x4c\x00\x02\x15") {
                // This is an iBeacon
                local uuid =, 20);
                uuid = parse_uuid(uuid);
                if (uuid in uuids) {

                    local major =, 22);
                    major = (major[0] << 8) + (major[1]);

                    local minor =, 24);
                    minor = (minor[0] << 8) + (minor[1]);

                    local power =[24];
                    local beaconid = format("%s:%d:%d", uuid, major, minor)
                    if (!(beaconid in scans)) {
                        scans[beaconid] <- {};
                        scans[beaconid].rssi <- {};
                        scans[beaconid].rssi[address] <- {};
                        scans[beaconid].rssi[address].time <- time();
                        scans[beaconid].rssi[address].samples <- 0;
                        scans[beaconid].rssi[address].rssi <- 0.0;
                    local beacon = scans[beaconid];
                    beacon.uuid <- uuid;
                    beacon.major <- major;
                    beacon.minor <- minor;
                    beacon.time <- time();
                    beacon.rssi[address].rssi += event.payload.rssi;
                    beacon.rssi[address].rssi /= 2;


Thanks in advance,




Generally, if you want something to happen in squirrel based on an asynchronous event (eg in this ble event handler) you’d register a function with the class and just call it - or call it in an imp.wakeup(0, xxx) callback if you wanted to ensure that higher priority tasks could execute before the callback.

If you want to, eg, add this received beacon to a queue that a periodic task could deal with, then you’d have something like:

processingQueue <- []; // starts empty

…then in the callback, after you’ve built the “beacon” object:


…and then you can process this queue periodically like this:

function processQueue() {
  while(processingQueue.len() > 0) {
    local beacon = processingQueue.remove(0); // pull the oldest item in the queue
    // process it

  imp.wakeup(10, processQueue()); // do this again in 10 seconds
processQueue(); // kick off queue processing

Is that what you were looking for?

1 Like


I’ve not tried this approach.

But if this will allow me to use device.on()/agent.send() interactions then I’ll use it.

I have to declare and write these as standalone functions/variable, right? That is, outside the “discover_mode” function?

Thanks for your help!



I mean, you can just do agent.send(“data”, beacon) right from the handler if you want - I presumed you were wanting to do some local pre-processing before sending to the agent.

You could declare them at a global level, or you can also wrap stuff in a Application() class, which is the approach taken by a lot of our example code.

1 Like


Thanks for this Hugo!

The approaches really helped me out :slight_smile: