Timer

The Timer module schedules tasks to be executed periodically on the board. Unlike using a Timer or Handler from the Android side, the MetaWear timer is only used with MetaWear commands and solely exists on they board.

import com.mbientlab.metawear.module.Timer;

final Timer timer = board.getModule(Timer.class);

Scheduling a Task

Use scheduleAsync to schedule tasks to be run in the future. Typically this is used with the forced data producer’s read function to have the board periodically request data from that producer.

import com.mbientlab.metawear.module.Gpio;

public Task<ScheduledTask> scheduleRead(final ForcedDataProducer producer) {
    // send a read command to the dadta producer every 30 seconds, start immediately
    return timer.scheduleAsync(30000, false, new CodeBlock() {
        producer.read();
    });
}

Task Management

After scheduling a task, use the created ScheduledTask object to start, stop, and remove the task. Furthermore, all ScheduledTask objects have a unique numerical value that is used to retieve a previously created task at any time with lookupScheduledTask.

ScheduledTask mwTask;

// lookup a task with id = 0
if ((mwTask = timer.lookupScheduledTask((byte) 0) != null) {
    // start the task
    mwTask.start();

    // stop the task
    mwTask.stop();

    // remove the task, id 0 no longer valid id
    mwTask.remove();
}