Samsung Galaxy Gear2 smartwatches are some nice new wearables from Samsung. Although we already know that Android Wear is on its way, it will take some time for it to arrive. Why not take this opportunity to develop your first app for Gear2?

Developing your first Tizen Galaxy Gear2 App - Tips

Samsung Galaxy Gear2 smartwatches are some nice new wearables from Samsung. As you probably know, creating a widget for Gear2 involves some HTML, CSS and last, but not least, JavaScript skills. In other words - we will be developing on Tizen OS.

Gear2 hit the market only 3 months ago so there aren’t many noteworthy tutorials on the web. Unfortunately, only 2 decent official video tutorials are available. Besides that you can find some valuable information on Gear by looking up the denvycom.com blog and… To be honest - that’s all we’ve got.

Setting up the environment & troubleshooting

Follow the official install guide carefully and you’re all OK. Remember about dealing with certification matters, because you will NOT be able to run your own applications, nor will you be able to build and launch existing tutorial ones. Don’t forget to push the certificate into Galaxy Gear device. Also be sure to enable USB debugging on smartwatch and check ‘Unknown sources’ option in the Gear Manager app.

Understanding the application

Now, when your IDE is ready, it’s time to install and import official HelloAccessory tutorial project. As you can see the app is divided into 2 separate pieces: the Android host-side, and the wearable/gear-side. This is the most common scenario called integrated app type. Android-side service-based class runs the logic, which controls the Gear widget.

Here’s how the skeleton of host-side Provider Service looks like. SAAgent class extends the standard Android Service class. In other words, this file represents a special Service, which contains all host-side functionalities. There’s also SASocket subclass, which drives the connection itself, between Android smartphone and Gear2 widget. It’s your application’s main logic.

In general - SAAgent class can be compared to something like a Peer. On the other hand, SASocket is the ‘bridge’ between SAAgent and Galaxy Gear2 device.

Gear-side logic

Information from the previous paragraph comes very handy when we take a closer look at the Gear-side part of the application. Open up the JavaScript file and compare it with the following method flow diagram:

When Gear tries to connect, it searches for available SAAgent. Then, we go one step further by setting appropriate listener and firing ‘SAAgent.findPeerAgents’ method. Next, we check, if SAAgent’s and Gear’s AppNames correspond with each other. If so, we request service connection with SAAgent and finally can communicate with the host-side Android device using SASocket.

Sending and receiving data

Here comes the most important part of the first tutorial app - transferring data between Android and Gear. We do that using binary data arrays (in this example, Strings). It’s a very crucial skill, essential basic, which you must know in order to be able to jump into developing bigger and more valuable applications. Let’s take a closer look at the Android-side:

onReceive method is fired every time Android device receives data from Gear. You can even create a simple ‘switch’ here, using String or JSON objects. In the code above we have the easiest example - smartphone receives data from Gear and displays it as Toast notification. As simple as that…

Wait, what if you want to send data from host-side to Gear? It’s not explained in the tutorial app, but don’t worry - I’m here to show you.

That’s how you send data to Gear from Android device. First of all, you get a uHandler from connectionsMap (this ensures that the connection with smartwatch is active). Then you simply post binary data on the specific channel using Thread. It looks easy, but the trickiest part is to properly launch this method when needed. As I stated at the beginning, SAAgent class extends Android Service. Then, the simplest, yet quite effective way to communicate with the Service (and our sendNotification method) is to use Broadcasts.

Receiving - Gear

Now, when you know how to handle connection from the Android host part, it’s the time to show you some basic Gear-side scenarios. It’s much easier, so don’t worry. Just remember, that we are coding in JavaScript here.