Greg Dziemidowicz's Blog

software development related topics

MelbTram - My First App for Smart Watch

I’ve recently bought Sony Smart Watch 2 with the intention of developing apps for it (check out MelbTram).

I am quite happy with this purchase, it’s really convenient to be able to see who is calling or read a message at simple glance. I have chosen Sony Smart Watch, mainly because it runs Android and supports my Nexus phone.

I have created my first app in two sessions on consecutive weekends (two Sundays). The first session was about being able to deploy simple “Hello World” app to the watch.

As I never developed before for Android, the first challenge was how to put my phone into debug mode. It’s quite easy:

At the “About” screen, scroll to the bottom and tap on “Build number” seven times.

If your device is in debug mode, you should be able to run, and see:

1
2
3
$> ./platform-tools/adb devices
List of devices attached
XXXXXXXXXXXXXXXX  device

Then, I was following instructions from Sony Developer website and I was able successfully deploy my “Hello World”. Yay!

The second sessions was about actually developing the app. The app is truly a minimum viable product, created and deployed to Play store within one day ;) The problem I’ve decided to tackle was “when is the best time to leave home/office in order to catch a tram?”.

I’ve based my solution on “SampleControlExtension” provided with the SDK. Using it as starting point, step by step I’ve transformed it into the final form. The app is very simple. It consists of “Preferences” screen on the phone, where you setup your tram stops and routes, and simple screen on the watch displaying next 2 tram arrival times.

The app let’s you configure 4 different routes. When you open the app on the watch, 4 background tasks like this will be started:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import com.sonyericsson.extras.liveware.extension.util.control.ControlExtension;

import android.os.AsyncTask;
import android.util.Log;

public class RetriveTramInfoTask  extends AsyncTask<String, Void, TramArrivals> {

    private Exception exception;
    private SampleControlSmartWatch2 myListener;


    private TramInfoRequest request;
    int viewLabelId = -1;

    public RetriveTramInfoTask(SampleControlSmartWatch2 myListener, int viewLabelId, TramInfoRequest request) {
      this.myListener = myListener;
      this.viewLabelId = viewLabelId;
      this.request = request;
    }

    protected TramArrivals doInBackground(String... urls) {
        try {
          TramTrackerAPI api = new TramTrackerAPI();
          return api.nextTram(request);
        } catch (Exception e) {
          e.printStackTrace();
            this.exception = e;
            return new TramArrivals(null);
        }
    }

    protected void onPostExecute(TramArrivals result) {
        String message = String.format("%d in %s minutes, then %s", request.routeId, result.get(0), result.get(1));
      myListener.setWatchEvent(viewLabelId, message);
    }
}

The end result looks more or less like this:

The MelbTram app is available for free in Google play store, check it out!

Comments