Subscribed unsubscribe Subscribe Subscribe

TI-Nspire & BLE / Tiny BLE-Connection-Listener / LaunchPad と Grove BLE

Tiny BLE-Connection-Listener

Some changes have been made to the previous script.

f:id:ti-nspire:20161101035813p:plain:h315

.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 UUIDs, which will be stored in a variable "uuids", in a Calculator page.
require "bleCentral"

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[j+3] = "  name: " ..(myPeripheral:getName()  or "none")
      MESSAGES[j+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")
      j = 6
      for k, v in pairs(advertisementData) do
         MESSAGES[j] = "  ad "..(tostring(k) or "none")..": "..(tostring(v) or "none")
         if tostring(k):find("uuid") then
            var.store("uuids", v)
         end 
         j = j + 1 
      end
      MESSAGES[j]   = "  isConnectable: "..(tostring(isConnectable) or "none")
      MESSAGES[j+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[j+2] = "Connector Callback"
   MESSAGES[j+3] = "  name: " ..(peripheral:getName()  or "none")
   MESSAGES[j+4] = "  state: "..(peripheral:getState() or "none")
   MESSAGES[j+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
Remove all ads