BLE

Regarding Bluetooth Low Energy (BLE) functionality, this system supports scanning only. Pairing with other devices is not supported.


ble.on()

Starts the BLE module.

ble.on()

ble.off()

Stops the BLE module.
Once stopped, the system resources occupied by the BLE module for Lua are released.


ble.scanStart(callback, options)

Starts a BLE scan indefinitely.

Options

Key Type Default Description
active boolean true Set to true for Active Scanning.
interval number 0x10 Specifies the scan interval (2-byte numeric value).
window number 0x10 Specifies the scan window (2-byte numeric value).
phy1m boolean true Set to true to scan/receive standard BLE packets.
phyCoded boolean true Set to true to scan/receive BLE5 Coded PHY packets.
duplicate boolean true Set to false to filter out duplicate signals from the same MAC address.

Note: behavior of duplicate=false

When you set duplicate=false, signals from a MAC address that has already been received are not notified again. However, this filter is only effective until the scan is stopped or until the internal buffer becomes full.

If you keep scanning without ever stopping, the internal buffer will eventually overflow. When that happens the filter is reset, and signals from devices that were already received will start being received (notified) again.

For this reason, when using duplicate=false, design your code on the assumption that it must be paired with logic that periodically stops and restarts the scan. Restarting the scan resets the filter state and avoids unexpected re-notifications caused by buffer overflow.

callback(peripheral)

The callback function receives a table (object) containing the data of the detected advertisement.

Peripheral Object Properties:

Key Type Description
address string The MAC address of the detected device.
rssi number Received Signal Strength Indicator (signal strength).
advData string The raw advertisement packet data.
isScanResp boolean true when the packet is a scan response, false when it is an advertisement. (OS7.1.0 and later)

Example

The following example demonstrates how to start a passive scan immediately upon startup and receive all available signals.

The received signal details will be output to the serial console.

-- Helper function to convert string to hex format
local function tohex(data)
  local hex = {}
  for i = 1, #data do
    hex[#hex+1] = string.format("%02x", data:byte(i))
  end
  return table.concat(hex, "")
end

-- Callback function triggered when a device is found
function onFind(peripheral)
  os.log("found\n addr: " .. tohex(peripheral.address) .. 
         "\n rssi:" .. tostring(peripheral.rssi) .. 
         "\n data:" .. tohex(peripheral.advData));
end

-- Initialize BLE
local ret = ble.on();
os.log("ble on ret=" .. tostring(ret));

-- Start scanning with specific options
ret = ble.scanStart(onFind, {
  active=false,    -- Passive scan
  interval=16,
  window=16,
  phy1m=true,
  phyCoded=true,
  duplicate=true   -- Do not filter duplicates
});

os.log("scan started ret=" .. tostring(ret));