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.

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.

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));