You are on page 1of 1

This function gets called when things start up.

It creates a timer
that fires every 10 seconds, and also attempts to open the sensor,
which is represented by the TempeSensor class.

The onLayout() method is called when your app starts, and sets
up the layout of your app’s view. Because you’re only drawing
directly on the screen in the onUpdate() function, as opposed to
displaying UI elements whose contents are refreshed, you don’t
have any layout to perform. The dc argument is short for “draw‐
ing context,” which is an object that’s provided to your app for
all drawing operations. You want to put something on the
screen? You go through the dc.

The onShow() method is called when the app comes back into
the foreground. This restarts the timer (it will be stopped in
onHide()). This reveals a key part of power management. The
device runtime will notify your app when the user sends it to
the background. It does this by calling the onHide() method.
When the user returns to the app later, the device calls your
onShow() method.

This function clears the screen, puts a white background on


there, and adds some text with the current temperature read
from the sensor.

This stops the timer when the app gets sent to the background.
See the preceding discussion for the onShow() method.

Example 4-2. Source of the TempeSensor.mc file


using Toybox.Ant as Ant;
using Toybox.System as System;
using Toybox.Time as Time;

class TempeSensor extends Ant.GenericChannel


{

const DEVICE_TYPE = 25; // The ANT+ device type


const PERIOD = 65535; // How often we expect new data

hidden var chanAssign; // The channel assigned by the radio

var data; // Stores the data received from the


// sensor

Read Data from a Sensor | 45

You might also like