ble

BLEではscanにのみ対応しておりペアリングは非対応となります。

ble.on()

bleを開始します。

ble.on()

ble.off()

bleを停止します。
停止した段階でLuaからの占有は解放されます。

ble.scanStart(callback, options)

bleのスキャンを無期限で開始します。

Options

Key Type Default 意味
active boolean true trueでアクティブスキャンを意味します。
interval number 0x10 スキャンインターバルを指定します。2バイトの数値です。
window number 0x10 スッキャンインターバルを指定します。2バイトの数値です。
phy1m boolean true trueで通常のBLEパケットを探索・受信します。
phyCoded boolean true trueでBLE5のCoded PHYのパケットを探索・受信します。
duplicate boolean true falseで同じmacaddressから来た信号を覚えてcallbackを呼ばないようにします。

callback(peripheral)

引数で指定するcallback関数では受信したadvertisementをtableで(objectで)受け取ります。

Options

Key Type 意味
address string macaddressを意味します
rssi number 受信時の電波強度
advData string 受け取ったパケット

Example

以下の例は起動と同時にパッシブスキャンを開始し可能な限りすべての信号を受信するようにします。

受信した信号はシリアルコンソールに表示します。


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

function onFind(peripheral)
  os.log("found\n addr: " .. tohex(peripheral.address) .. "\n rssi:" .. tostring(peripheral.rssi) .. "\n data:" .. tohex(peripheral.advData));
end

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

ret = ble.scanStart(onFind, {
  active=false,
  interval=16,
  window=16,
  phy1m=true,
  phyCoded=true,
  duplicate=true
});
os.log("scan started ret=" .. tostring(ret));