Matt Thompson from Pebble G+ community asked a question that got me curious: Is there a way to buzz Pebble vibe at regular intervals in the background, while a regular watchface is displayed in foreground?
Besides running a normal app, Pebble has 2 ways to run code in the background: background worker and Wakeup API.
Background worker can truly run in the background, but has no access to UI (and vibes are considered UI) as well as other limitations. Besides you can have only one background worker, so for example if you’re running MisFit app and want to run another background app – you’re out of luck.
WakeUp API has the ability to act as a timer in the background and launch your app when timer countdown finished. Interesting thing is – if your app doesn’t have any UI (windows) – it exits right away, so from the user’s point of view – it didn’t even ran – then point in the watch interface remains the same (if you’re looking at a watchface, or at settings etc. – you remain at the same spot).
We can use this to wake the app, buzz the vibe, reschedule wakeup time and exit. User will just hear a buzz with no visual indication that something was launched. Here’s a basic code to achieve this:
static uint32_t const segments[] = {1000, 500, 1000, 500, 1000}; static void init(void) { wakeup_service_subscribe(NULL); wakeup_schedule(time(NULL) + 60, 0, false); vibes_cancel(); VibePattern pat = { .durations = segments, .num_segments = ARRAY_LENGTH(segments), }; vibes_enqueue_custom_pattern(pat); } int main(void) { init(); app_event_loop(); }
This is pretty straightforward. Line 01 declares an array for custom vibe pattern (3 one-second buzzes separated by half-a-second silence) Line 05 subscribes to WakeUp event. Ordinary you need to specify a callback function as a parameter, but our entire code runs in the Init
, so we don’t use it here. Line 06 schedules app wake-up in 60 seconds. Line 08 cancels any current vibes in case any are still running. Lines 10-13 prepare structure for custom vibe sequence and Line 14 runs the vibes.
That’s it. When you launch the app – it schedules its own wakeup, buzzes the vibe and exits immediately. You’re free to do what you want – set a watchface, run another app etc. When time comes – the app wakes up, buzzes the vibe, schedules next wakeup and exits without interfering with whatever user is doing. Etc. Etc. Etc.
Just remember that the only way to stop it is delete the app from the watch and wait for the current buzz sequence to finish.
Useful Links
- Demo project of “backgound” vibes at Github you can import it directly into CloudPebble
- Compiled binary of the demo you can install on your watch warning! it’s an extreme version that would buzz constantly without stop. Run at your own risk