Pebble Watchface
Since I’ve had Pebble, I always wondered what is the API like and how difficult it is to do your own app or watchface. Well it turns out that it’s quiet easy especially when you have good documentation and bit of a time.
Install SDK and check connectivity
Installing Pebble SDK is very simple and it worked without any issues. On download page you can decide between new 3.0 SDK which is still in Beta version or 2.9 SDK, which is the one I picked. Pebble strongly recommends to rebuild and retest apps to all developers, but I doubt that mine simple watchface would need it at this point.
Installation of SDK for Linux is pretty much copy/past action. After that I wanted to check if everything works properly. First of all, you need to enable developer connection, so you can try out your app on the watch. There is also emulator available. In developer view on the phone app, there is server IP address, you will need it later.
To test connection, you can create “Hello World” app and push it to your watch. Just go to your project folder and create new Pebble project. Then you can export your server IP address to env variables so you don’t have to type it all the time. After that go to just created Pebble project and buil&install.
Sometimes I got this error and all I need to do is wake up phone.
Design watchface
With every watchface I used before, there was always something missing. In my watchface I want to see as much time related information as possible.
The biggest priority is time and date with the day of the week. Another useful information is week of the year, which can be handy in work/business environment, and possibly the day of the year.
Last but not least, is the connection status and battery status. With battery status I want to know percentage even though it takes a bit more space. And with connection status, I’d like to see the last time when the connection status was changed. That could be handy in situation when you forget your phone somewhere and you want to see when was the last time you were connected.
Code
I started with sample code that was generated when creating new project. Then I deleted everything I didn’t need and this is the bare minimum I was left with.
I also modified appinfo.json
file where I changed watchaface
value to true
. You could also change the name of the watchface, add your company name, and so on.
We will use functions window_load()
and window_unload()
to create/destroy whole layout and later on to set up/destroy handlers.
This is the description about functions from documentation:
load
: called when the window is pushed to the screen when it’s not loaded. This is a good moment to do the layout of the window.unload
: called when the window is deinited, but could be used in the future to free resources bound to windows that are not on screen.
Text on screen is inside TextLayer
, which is added then to root layer. So first I define s_time_layer
variable in top of the file, then in window_load()
we create and assign text layer to it. When creating text layer, you specify location and size. In this case I want this text layer to be at very top and to take the whole width of watch. To get width of screen I need to get bonds of windows_layer
or root layer.
Then I use text_layer_set_font()
to specify size and type of font I want to use. For now I stick with the Pebble system fonts.
I also changed text alignment with text_layer_set_text_alignment()
.
After I set text using text_layer_set_text()
, all I need to do is to add new text layer to window’s root layer, for that I use layer_add_child()
.
This is what I ended up with.
So next I added all other text layers for date info, battery and bluetooth status. Regarding bluetooth and battery status, at this point I am adding only text layers but later on I’ll add icons.
And this is what my watchface looks like with all text layers.
Next I’d like to add some icons for bluetooth and battery status. I have bluetooth icon of size 24x24 pixels, which was big enough to see all details on watch but still small. I copied icon to resources
folder and added it to appinfo.json
as new resource.