Some changes have been made to the previous script.
.lua
-- Tiny BLE-Connection-Listener for TI-Nspire -- Tap the right half of the screen to start scanning to connect. -- Tap the left half of the screen to disconnect. -- You can view UUID's, which will be stored in a variable "uuids", in a Calculator page. require "bleCentral" require "color" local myPeripheral = nil local MESSAGES = {} function on.resize() W, H = platform.window:width(), platform.window:height() fontSize = W/30 lineSpace = H/15.5 leftMargin = W/50 refreshMenu() platform.window:invalidate() end function on.construction() var.store("uuids", "none") ble.addStateListener(listenerCallback) end function on.paint(gc) gc:setFont("sansserif", "r", fontSize) for i, v in ipairs(MESSAGES) do gc:drawString(v, leftMargin, (i - 1) * lineSpace) end end ------------------------------ -- Define listener callback -- ------------------------------ function listenerCallback(state) MESSAGES[1] = "Listener Callback" MESSAGES[2] = " state: "..(state or "none") platform.window:invalidate() end ---------------------------- -- Start or Stop scanning -- ---------------------------- function peripheralOn() bleCentral.startScanning(scannerCallback) platform.window:invalidate() end function peripheralOff() bleCentral.stopScanning() if myPeripheral then myPeripheral:disconnect() MESSAGES[i+3] = " name: " ..(myPeripheral:getName() or "none") MESSAGES[i+4] = " state: "..(myPeripheral:getState() or "none") end platform.window:invalidate() end ----------------------------- -- Define scanner callback -- ----------------------------- function scannerCallback(peripheral, advertisementData, isConnectable, RSSI) if peripheral then peripheral:connect(connectorCallback) MESSAGES[3] = "Scanner Callback" MESSAGES[4] = " name: " ..(peripheral:getName() or "none") MESSAGES[5] = " state: "..(peripheral:getState() or "none") i = 6 for k, v in pairs(advertisementData) do MESSAGES[i] = " ad "..(tostring(k) or "none")..": "..(tostring(v) or "none") if tostring(k):find("uuid") then var.store("uuids", v) end i = i + 1 end MESSAGES[i] = " isConnectable: "..(tostring(isConnectable) or "none") MESSAGES[i+1] = " RSSI: " ..(RSSI or "none").." dBm" end platform.window:invalidate() end ------------------------------- -- Define connector callback -- ------------------------------- function connectorCallback(peripheral, event) if peripheral then bleCentral.stopScanning() myPeripheral = peripheral end MESSAGES[i+2] = "Connector Callback" MESSAGES[i+3] = " name: " ..(peripheral:getName() or "none") MESSAGES[i+4] = " state: " ..(peripheral:getState() or "none") MESSAGES[i+5] = " event: " ..(event or "none") platform.window:invalidate() end ------------------ -- Menu & Mouse -- ------------------ function refreshMenu() Menu = { {"Controls", {"Scan and Connect", function() peripheralOn() end}, {"Disconnect" , function() peripheralOff() end}, }, } toolpalette.register(Menu) end function on.mouseUp(x, _) if W/2 < x then peripheralOn() end if x < W/2 then peripheralOff() end end