Humanities Home page   Office of Digital Humanities
Back     BYU LiveCode Lessons Gateway

Mobile Device Sensors Exercise


Using the tools Livecode provides for reading mobile device sensors, construct a simple app that displays the readings from a mobile phone such as an iPhone.

See the lesson on Mobile Device Sensors for a more complete description of how to access sensors using LiveCode.


  1. Create a stack and name it something like "sensorDemo". Make the size appropriate for fitting on an iPhone screen, about 320 X 260. It should look something like the figure at right. Save it to your disk before continuing.
  2. Name the field "sensorReport".
  3. The four radio buttons should be grouped into a group named "sensorGrp". Be sure that the group's radioBehavior property is set to true.

  4. We want the stack to stay in portrait orientation on the mobile device so we'll script the stack like this:
    on preOpenStack
        if the environment is "mobile" then
            mobileSetAllowedOrientations "portrait,portrait upside down"
        end if
    end preOpenStack
  5. The group containing the radio buttons will be scripted to turn on only the selected sensor, turning the other ones off so we're only focusing on one at a time. So script the group like this:
    on mouseUp
        stopAllSensors -- clear the current sensor tracking setting
        put the hilitedButtonName of me into tSensor
        mobileStartTrackingSensor tSensor, false
    end mouseUp
    on stopAllSensors
        repeat with x = 1 to the number of btns in me
            put the short name of btn x of me into tSensor
            mobileStopTrackingSensor tSensor
        end repeat
    end stopAllSensors
  6. Scripting the card. First we're going to write some simple housekeeping handlers. It should be obvious what they are doing:
    on preOpenCard
        send "stopAllSensors" to group "sensorGrp"
    end preOpenCard
    on initCard
        put empty into fld "sensorReport"
        set the hilitedButtonName of group "sensorGrp" to empty
    end initCard
  7. Now let's add a handler to the card script to handle the locationChanged message, which receives data from the location sensors when that sensor is turned on:
    on locationChanged pLat, pLong, pAlt
        put "Your location has changed: " & return & \
              "latitude: " & pLat & return & "longitude: " & pLong & return & \
              "altitude: " & pAlt into fld "sensorReport"
    end locationChanged
  8. Now write handlers to report data from the heading, acceleration, and rotation rate sensors. These handlers are described on the Monitoring Mobile Device Sensors page.

  9. Write a handler to capture and report error messages that might be received from the sensors.

  10. Set up the standalone application settings so that this app can be tested on actual devices. The following screen shots show what settings need to be selected. Note that the specific desired sensors need to be enabled on the devices for these commands above to work:



  11. Test your stack on a simulator or actual device. Observe what information the various sensors return.

Back     BYU LiveCode Lessons Gateway
Maintained by Devin Asay.
Copyright © 2005 Brigham Young University.
This page last updated on April 03, 2020 09:45:32.