Home TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 31



Scripting Tutorial - Lesson 31: BLE - Create your own TI-Nspire Remote


Supplement: Working with Scripts on the iPad

Lesson 31: BLE - BLE - Create your own TI-Nspire Remote

Lesson 32: BLE - Measuring Temperature with the Vernier Go Wireless Temp (UPDATED to include Vernier GoLink Support)

Lesson 33: BLE - Measuring Heart Rate

Lesson 34: BLE - Measuring Temperature with the TI Sensor Tag

Lesson 35: BLE - Build Your Own Weather Station with the TI Sensor Tag

Lesson 36: BLE - Exploring Movement and Position with the TI Sensor Tag

Lesson 37: Lua, LaunchPads and BLE: Making Music via BLE

Lesson 38: Lua, LaunchPads and BLE: Real world data at your fingertips: Light, Ultrasonic Motion and more...

Lesson 39: Lua, LaunchPads and BLE: Build your own BLE Robot for under $USD40


Download supporting files for this tutorial

Download this page in PDF format

Texas Instruments TI-Nspire Scripting Support Page


In the first lesson of this sequence, you were introduced to the fundamentals of BLE scripting using Ti-Nspire/Lua. You learned how to scan for BLE devices, and to connect to certain of these.

In this lesson, you will connect to a specific peripheral and read data (in this case, simple key presses) from this. We will begin with the TI Sensor Tag 2.0 - one of the most affordable and powerful BLE devices around. If you don't yet have one, then perhaps it is time to explore further!


Hearing SensorTag Key Presses

Top of Page

The TI SensorTag comes with what are termed "Simple Keys" - we will refer to these as "left" and "right" keys. The SensorTag 2.0 (or CC2650 SensorTag) has only two buttons, found on the sides. Facing the two strip windows, the left side button also serves to turn the device on and off, but this actually requires a 3-second keypress. The right side button will switch to iBeacon mode after 6 seconds, and reset the device after 10.

All SensorTags use the same broadcast signal (UUID) and transmit the same information when the left and right buttons are pressed. It is a simple thing to take this information and define actions for these events, turning your Tag into a remote control for your script. This has been done with the Lua Objects Gallery scripts.


Interactive Script Exploration

This page has been enhanced using JavaScript so that we can focus easily upon the various elements of our script.

  • Click the 'init' button to see the initial variable definitions in the script window below for our SensorTag keypress script.
  • To see the on.resize and on.paint functions, click 'Layout'.
  • For menu, keyboard and mouse controls, click 'Menu'.
  • For BLE functions common to many scripts, click 'BLE General'.
  • For BLE functions specific to this script, click 'BLE Specific'.
  • To see the entire script, click 'Full Script'.

    Grabbing and Using the BLE Data

    We now focus upon the critical BLE functions for our script, first looking at General BLE Functions. Here we trace the path of the BLE functionality throughout the script.

    These first steps review the previous tutorial.

    • require 'bleCentral': This occurs immediately that the script is run and tests the validity of the platform and device.

    • ble.addStateListener(listenerCallback): This is placed in on.resize, setting up the BLE listener. It calls...

    • listenerCallback(state, scriptError): Checks the state (BLE.ON, BLE.OFF, etc) and may act upon errors.

    • peripheralOn(): If all conditions are met, then the user may initiate this command. As described previously, this calls...

    • bleCentral.startScanning(callbackScan): Scanning begins, calling...

    • callbackScan(peripheral): Carrying the name of a found peripheral, if this name is found then...

    • peripheral:connect(callbackConnect): This brings us up to the stage that we reached in the previous tutorial.

    The next steps are new.

    • callbackConnect(peripheral, event): Carrying the found peripheral and the event associated with this, we are ready to connect, and...

    • peripheral:discoverServices(callbackServices): All conditions being met, it is time to discover which services are available.

    • callbackServices(peripheral): Services are composed of characteristics. This function runs through found services looking for these.

    • service:discoverCharacteristics(callbackCharacteristics): Multiple characteristics will often be discovered, and each of these will lead to a callback test.


    Note again that these are general BLE functions - they are not specific to the SensorTag and so almost everything that has gone before (apart from a few initial variables) may be reused for other BLE devices.


    platform.apilevel = '2.5'

    screen = platform.window
    w = screen:width()
    h = screen:height()

    pcall(function () require 'bleCentral' end)

    require "color"
    local bleState = ''
    local bleStatus = 'Stand by'
    local peripheralName = ''
    local myPeripheral = nil
    local characteristicsFound = 0

    -- SensorTag keyPress Variables

    local keysRead = 'FFE1'

    local keyPress = nil

    local nameList = {'Tag', 'LaunchPad', 'HMSoft', 'BT05', 'CC41'}




    BLE Functions Specific to the Simple Keys of the SensorTag

    Time to see where the real action happens!

    • callbackCharacteristics(service): You will have seen from the final command above that each found service generates a callback. These are now checked against known UUID values, and also use characteristic:setNotify(true) to send a message to the peripheral to confirm connection. characteristic:setValueUpdateListener(callbackCharacteristic) then initiates...

    • callbackCharacteristic(characteristic) : Checking again that you are talking to the right device, the data from that device may now be configured to draw from it the required information - in this case, key press data. The SensorTag key presses issue 8-bit unsigned data, and so the command ble.unpack('u8', value) to take this apart and read the results - in this case, a '2' value for the left key and '1' for the right key.

      The applications of this simple method are broad - these simple results might be used, for example to simulate pressing tab keys, enter keys or escapeKey actions in a script. In this way, the SensorTag behaves like a remote control unit for the script. Why not grab the Lua Objects Gallery document from tutorial 29 and have a play with the various tools provided there. Study the scripts - these are simplified even further than the one developed here, by scanning and connecting automatically. All the user needs to do is to turn on the SensorTag, open the page and wait for connection. Then start controlling the action!


    If you have a TI SensorTag and Nspire iPad App, you may test this entire script by copying and pasting from this web page into the TI-Nspire Lua Script Editor.


    Back to Top


    Home TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 31