Skip to main content

bluetooth.startadvertise(advert, minAdInterval, maxAdInterval, scanResponse)

Causes the imp004m to begin transmitting GAP advertisements

Availability

Device (from impOS™ 38)

Parameters

Name Type Description
advert String or blob The advertising data, up to 31 bytes in length
minAdInterval Integer The minimum advertising interval in milliseconds (ms)
maxAdInterval Integer The maximum advertising interval in milliseconds (ms)
scanResponse String or blob Optional data the be returned in response to an external scan

Returns

Nothing

Description

This method causes the imp004m to begin broadcasting Generic Access Profile (GAP) advertisements. It may be called as many times as you need to update the data broadcast and/or the data with which the imp004m responds to a scan. Advertising continues while the data is being updated.

The value passed into advert comprises up to 31 bytes of advertising data. It is the application’s responsibility to ensure the data meets the Bluetooth advertising and scan response data specification — this method will broadcast whatever data you pass in.

The parameters minAdInterval and maxAdInterval respectively set the minimum and maximum advertising interval. The values provided must lie in the range 20ms to 10,240ms, and minAdInterval must not be greater than maxAdInterval.

The data passed into scanResponse comprises up to 31 bytes of response data. It is the application’s responsibility to ensure the data meets the Bluetooth advertising and scan response data specification — this method will broadcast whatever data you pass in.

bluetooth.startadvertise() automatically sets up the advertisement type according to the following rules:

  • ADV_IND if bluetooth.onconnect() has a current connection function registered, or
  • ADV_SCAN_IND if a scanRespsonse is set, or
  • ADV_NONCONN_IND otherwise

Example Code

The following code sets the imp004m up as a Bluetooth transmitter operating to the Apple iBeacon specification, which uses a 31-byte advertisement of a pre-set format: after the iBeacon preamble data, bytes 9 through 25 contain the iBeacon’s ‘proximity’ UUID; bytes 26 and 27 a major value; bytes 28 and 29 a minor value; and byte 30 a calibrated RSSI value. All iBeacons in the same site typically use the same UUID, with the major and minor values used to identify specific beacons, eg. major for floor number, minor for beacon on a given floor.

local iBeacon = "\x02\x01\x1A\x1A\xFF\x4C\x00\x02\x15\x92\x77\x83\x0A\xB2\xEB\x49\x0F\xA1\xDD\x7F\xE3\x8C\x49\x2E\xDE\x00\x00\x00\x00\xC5\x00";

local uuid = "";
for (local i = 0 ; i < iBeacon.len() ; i++) {
  uuid = uuid + format("%02x", iBeacon[i]);
}

server.log("Advertising the following iBeacon:");
server.log("UUID: " + uuid.slice(18,26) + "-" + uuid.slice(26,30) + "-" +
  uuid.slice(30,34) + "-" + uuid.slice(34,38) + "-" + uuid.slice(38,50));
server.log("Major: " + uuid.slice(50,54));
server.log("Minor: " + uuid.slice(54,58));

bt.startadvertise(iBeacon, 100, 100);