Skip to main content

bluetooth.setscanparams(active, interval, window)

Configures the current or next GAP scan

Availability

Device
Only available on the imp004m and imp006 (impOS 42)

Parameters

Name Type Description
active Bool Specifies whether the scan is active (true) or passive (false)
interval Integer The scan interval in milliseconds (ms)
window Integer The scan duty cycle in milliseconds (ms)

Returns

Nothing

Description

This method sets the parameters of any current or subsequent GAP scan.

Scans may be active or passive — choose which of these you require by passing true or false into the active parameter. If scanning is active, the imp will send a scan request when it receives a scannable advert (see bluetooth.startadvertise()) that includes a scan response. Otherwise it will scan passively: it will not respond to scan requests. Passive scans are typically used for beacon-sensing applications, whereas active scans are used to detect devices to which you might wish to connect.

Note that active and passive are types of scan — it does not indicate state, ie. if a scan is active, that does not indicate whether it is actually taking place.

If you don’t call bluetooth.setscanparams(), the imp will scan passively with a 10ms interval and a 100 per cent duty cycle window (ie. 10ms). These are also the default values for the optional parameters interval and window if you do not include them in your bluetooth.setscanparams() call.

The interval and window values must both be in the range 3ms to 10,240ms, and the value passed into window must not be greater than that passed into interval. For power-sensitive applications, this allows scanning to take place at a low duty cycle with the radio and imp idle. For most applications, however, the default values are sufficient.

Example Code

server.log("Scanning...");

// Configure imp004m for passive scanning; 100ms interval and window
bt.setscanparams(false, 100, 100);

// Begin scanning
bt.startscan(function(data) {
    if ("type" in data) {
        if (data.type == 3) {
            // Look out for ADV_NONCONN_IND responses
            local payload = "";

            // Convert payload to hex string
            for (local i = 0 ; i < data.data.len() ; i++) {
                payload = payload + format("%02x", data.data[i]);
            }

            // Is it an iBeacon? Get the first 9 hex pairs
            local iBeaconPrefix = payload.slice(0, 18);

            if (iBeaconPrefix == "02011a1aff4c000215") {
                // This is a beacon so record it
                local beacon = {};
                beacon.uuid <- payload.slice(18, 50);
                beacon.majorString <- payload.slice(50, 54);
                beacon.majorValue <- (hti(beacon.majorString.slice(0, 2)) * 256 + hti(beacon.majorString.slice(2)));
                beacon.minorString <- payload.slice(54, 58);
                beacon.minorValue <- (hti(beacon.minorString.slice(0, 2)) * 256 + hti(beacon.minorString.slice(2)));

                if (beacons.len() > 0) {
                    local got = false;
                    foreach (aBeacon in beacons) {
                        if (aBeacon.uuid == beacon.uuid && aBeacon.majorValue == beacon.majorValue 
                            && aBeacon.minorValue == beacon.minorValue) {
                            got = true;
                            break;
                        }
                    }

                    if (!got) {
                        beacons.append(beacon);
                        local uuid = beacon.uuid.slice(0,8) + "-" + beacon.uuid.slice(8,12) + "-" 
                          + beacon.uuid.slice(12,16) + "-" + beacon.uuid.slice(16,20) + "-" 
                          + beacon.uuid.slice(20);
                        server.log("Beacon found: " + uuid + " (Major: " + beacon.majorValue 
                          + " Minor: " + beacon.minorValue + ")");
                    }
                } else {
                    beacons.append(beacon);
                    local uuid = beacon.uuid.slice(0,8) + "-" + beacon.uuid.slice(8,12) + "-" 
                      + beacon.uuid.slice(12,16) + "-" + beacon.uuid.slice(16,20) + "-" 
                      + beacon.uuid.slice(20);
                    server.log("Beacon found: " + uuid + " (Major: " + beacon.majorValue 
                      + " Minor: " + beacon.minorValue + ")");
                }
            }
        }
    }
});