136 lines
4.0 KiB
Lua
136 lines
4.0 KiB
Lua
-- DESCRIPTION: Test BLE scanning and connections
|
|
|
|
local screen = "devices"
|
|
local lastScreen = nil
|
|
local devices = {}
|
|
local selected = 1
|
|
local message = "Press Scan to find nearby devices"
|
|
local connectedDevice = nil
|
|
|
|
local function refreshMode()
|
|
local mode = screen == lastScreen and REFRESH_FAST or REFRESH_HALF
|
|
lastScreen = screen
|
|
return mode
|
|
end
|
|
|
|
local function deviceName(device)
|
|
return device.name ~= "" and device.name or "(unknown)"
|
|
end
|
|
|
|
local function drawDevices()
|
|
gui.clear()
|
|
gui.drawCenteredText(FONT_UI_12, 28, "BLE Devices", COLOR_BLACK, STYLE_BOLD)
|
|
|
|
local visible = math.max(1, math.floor((gui.height() - 130) / 38))
|
|
local first = math.max(1, math.min(selected - math.floor(visible / 2), #devices - visible + 1))
|
|
local last = math.min(#devices, first + visible - 1)
|
|
local y = 72
|
|
for index = first, last do
|
|
local prefix = index == selected and "> " or " "
|
|
gui.drawText(FONT_UI_12, 36, y, prefix .. deviceName(devices[index]), COLOR_BLACK,
|
|
index == selected and STYLE_BOLD or STYLE_NORMAL)
|
|
y = y + 38
|
|
end
|
|
|
|
if devices[selected] then
|
|
local device = devices[selected]
|
|
if message ~= "" then gui.drawCenteredText(FONT_UI_10, gui.height() - 108, message) end
|
|
gui.drawCenteredText(FONT_UI_10, gui.height() - 82,
|
|
device.address .. " " .. device.rssi .. " dBm", COLOR_BLACK, STYLE_NORMAL)
|
|
else
|
|
gui.drawCenteredText(FONT_UI_10, math.floor(gui.height() / 2), message)
|
|
end
|
|
|
|
gui.drawButtonHints("Back", #devices > 0 and "Connect" or "", "", #devices > 0 and "Refresh" or "Scan")
|
|
gui.refresh(refreshMode())
|
|
end
|
|
|
|
local function drawStatus(text)
|
|
gui.clear()
|
|
gui.drawCenteredText(FONT_UI_12, math.floor(gui.height() / 2), text, COLOR_BLACK, STYLE_BOLD)
|
|
gui.refresh(refreshMode())
|
|
end
|
|
|
|
local function drawConnected()
|
|
gui.clear()
|
|
gui.drawCenteredText(FONT_UI_12, 44, "Connected", COLOR_BLACK, STYLE_BOLD)
|
|
gui.drawCenteredText(FONT_UI_12, 100, deviceName(connectedDevice))
|
|
gui.drawCenteredText(FONT_UI_10, 134, connectedDevice.address)
|
|
gui.drawButtonHints("Back", "Disconnect", "", "")
|
|
gui.refresh(refreshMode())
|
|
end
|
|
|
|
local function render()
|
|
if screen == "devices" then
|
|
drawDevices()
|
|
elseif screen == "connected" then
|
|
drawConnected()
|
|
end
|
|
end
|
|
|
|
local function scan()
|
|
screen = "scanning"
|
|
drawStatus("Scanning...")
|
|
|
|
devices = {}
|
|
collectgarbage("collect")
|
|
local results, err = ble.scan(3000)
|
|
devices = results or {}
|
|
selected = 1
|
|
message = results and (#devices .. " devices found") or (err or "Scan failed")
|
|
screen = "devices"
|
|
render()
|
|
end
|
|
|
|
local function connect()
|
|
local device = devices[selected]
|
|
if not device then return end
|
|
|
|
screen = "connecting"
|
|
drawStatus("Connecting...")
|
|
if ble.connect(device.address) then
|
|
connectedDevice = device
|
|
screen = "connected"
|
|
else
|
|
message = "Could not connect to " .. deviceName(device)
|
|
screen = "devices"
|
|
end
|
|
render()
|
|
end
|
|
|
|
local function disconnect()
|
|
ble.disconnect()
|
|
connectedDevice = nil
|
|
message = "Disconnected"
|
|
screen = "devices"
|
|
render()
|
|
end
|
|
|
|
function init()
|
|
if not ble.init("BLETest") then message = "BLE initialization failed" end
|
|
render()
|
|
end
|
|
|
|
function on_button(name, event)
|
|
if event ~= "down" then return end
|
|
|
|
if name == "up" and screen == "devices" and #devices > 0 then
|
|
selected = selected == 1 and #devices or selected - 1
|
|
render()
|
|
elseif name == "down" and screen == "devices" and #devices > 0 then
|
|
selected = selected == #devices and 1 or selected + 1
|
|
render()
|
|
elseif name == "right" and screen == "devices" then
|
|
scan()
|
|
elseif name == "confirm" and screen == "devices" then
|
|
connect()
|
|
elseif name == "confirm" and screen == "connected" then
|
|
disconnect()
|
|
elseif name == "back" and screen == "connected" then
|
|
disconnect()
|
|
elseif name == "back" then
|
|
ble.deinit()
|
|
sys.exit()
|
|
end
|
|
end
|