Clip Documentation

Android SDK

Introduction

Aria App launches a background service that takes care of the bluetooth connection and delivers the gestures to all the applications that connects to it. This service takes advantage of the Google Message Api for Android. You can bind your activity to this bradcast messenger and receive the gestures.

At this page you will first find a list of the lines of code you need to add to your activity to make this communication possible and in the last part of this page you will find a very simple example of how those lines look together in a complete Android Wear app.

Following, the instructions to connect your app to the background service and a real implementation example.

Code implementation

Your Wear activity is going to need to import the following packages:

 
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Handler;
    import android.os.IBinder;
    import android.os.Message;
    import android.os.Messenger;
    import android.widget.Toast;
        

Inside the activity you need to declare the following variables that we will use to receive the messages coming from the barckground service.

    /**
     * AriaService constants
     */
    public static final String ARIA_PACKAGE_NAME = "com.deus_tech.aria";
    public static final String ARIA_SERVICE_NAME = "com.deus_tech.aria.AriaService";

    public static final int ARIA_MSG_REGISTER_CLIENT = 1;
    public static final int ARIA_MSG_UNREGISTER_CLIENT = 2;
    public static final int ARIA_MSG_GESTURE = 3;

    public final static int ARIA_GESTURE_ENTER = 1;
    public final static int ARIA_GESTURE_HOME = 2;
    public final static int ARIA_GESTURE_UP = 3;
    public final static int ARIA_GESTURE_DOWN = 4;

    /**
     * Messenger for communicating with AriaService
     */
    private Messenger mAriaService = null;

    /**
     * Messenger object used by AriaService to send messages to your app
     */
    private final Messenger mMessenger = new Messenger(new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // A gesture message
                // AriaService puts payload to msg.arg1
                case ARIA_MSG_GESTURE:
                    handleGesture(msg.arg1);
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    });
    
    /**
     * Standard Android object interacting with services
     */
    private ServiceConnection mConnection = new ServiceConnection() {
        /**
         * This is called when the connection with the service has been established
         */
        public void onServiceConnected(ComponentName className, IBinder service) {
            switch (className.getClassName()) {
                // You can have multiple service in your app
                // So we check if this is AriaService
                case ARIA_SERVICE_NAME:
                    // We are communicating with our service through an IDL interface,
                    // so get a client-side representation of that from the raw service object.
                    mAriaService = new Messenger(service);
                    // Register AriaService client to receive gesture messages
                    ariaRegisterClient();
                    break;

                // Check your connected services here
            }
        }

        /**
         * This is called when the connection with the service has been unexpectedly disconnected
         */
        public void onServiceDisconnected(ComponentName className) {
            switch (className.getClassName()) {
                case ARIA_SERVICE_NAME:
                    ariaUnregisterClient();
                    mAriaService = null;
                    break;

                // Check your disconnected services here
            }
        }
    };
    private void ariaRegisterClient() {
        try {
            // Send a registration message to AriaService
            // AriaService will send you gestures until you unregister
            Message msg = Message.obtain(null, ARIA_MSG_REGISTER_CLIENT);
            msg.replyTo = mMessenger;
            mAriaService.send(msg);

            // Show user we are connected to service
            Toast.makeText(MainActivity.this, "Hi", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, "Bye", Toast.LENGTH_SHORT).show();
            e.getStackTrace();
        }
    }

    private void ariaUnregisterClient() {
        try {
            // Try to send undegister message
            Message msg = Message.obtain(null, ARIA_MSG_UNREGISTER_CLIENT);
            msg.replyTo = mMessenger;
            mAriaService.send(msg);

            // Show user we are disconnected to service
            Toast.makeText(MainActivity.this, "Bye", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            // Connection to service was lost
            e.getStackTrace();
        }
    }
    
    

The function handleGesture(int gesture) is the fucntion where we decide what to do with the received gesture. Here a common usage:

    private void handleGesture (int gest){
        switch (gest) {
            case ARIA_GESTURE_HOME:
//  We usually keep the HOME gesture as the "closing gesture", that shuts down the application            
//                this.finish();
                break;
            case ARIA_GESTURE_ENTER:

                break;
            case ARIA_GESTURE_UP:

                break;
            case ARIA_GESTURE_DOWN:

                break;
        }
    }
    

Finally in order connect the activity and start the communication with the background service, we link our activity in the onStart() method, and we unbind the communication in the onStop() method (don't forget it!)

    protected void onStart(){
        super.onStart();
        // Bind AriaService that's running in another process
        // You can do multiple bindings in any of your Activities or Services
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(ARIA_PACKAGE_NAME, ARIA_SERVICE_NAME));
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    protected void onStop() {
        super.onStop();

        // Unregister AriaService client first before unbind (required)
        ariaUnregisterClient();

        // Unbind from AriaService
        unbindService(mConnection);
    }
    

Complete example

Here you can find the sample code of the whole Wear activity where on a black background, a text changes as the gesture are performed. In order to run this example you need Aria to be connected with the smartwatch, the Aria app running in the background and the calibration ready.

Let's start with the super simple xml example

activity_main.xml

      <?xml version="1.0" encoding="utf-8"?>

            <FrameLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:map="http://schemas.android.com/apk/res-auto"
                android:id="@+id/root_container"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:background="#000000">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Gesture"
                    android:id="@+id/textView"
                    android:layout_gravity="center"
                    android:textColor="#ffffff" />
            </FrameLayout>
            

Then the whole main activity looks like this

MainActivity.java

package flicktek.aria_sdk;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.support.wearable.activity.WearableActivity;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends WearableActivity {

    private TextView mTextView;


    /**
     * AriaService constants
     */
    public static final String ARIA_PACKAGE_NAME = "com.deus_tech.aria";
    public static final String ARIA_SERVICE_NAME = "com.deus_tech.aria.AriaService";

    public static final int ARIA_MSG_REGISTER_CLIENT = 1;
    public static final int ARIA_MSG_UNREGISTER_CLIENT = 2;
    public static final int ARIA_MSG_GESTURE = 3;

    public final static int ARIA_GESTURE_ENTER = 1;
    public final static int ARIA_GESTURE_HOME = 2;
    public final static int ARIA_GESTURE_UP = 3;
    public final static int ARIA_GESTURE_DOWN = 4;

    /**
     * Messenger for communicating with AriaService
     */
    private Messenger mAriaService = null;

    /**
     * Messenger object used by AriaService to send messages to your app
     */
    private final Messenger mMessenger = new Messenger(new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // A gesture message
                // AriaService puts payload to msg.arg1
                case ARIA_MSG_GESTURE:
                    handleGesture(msg.arg1);
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    });

    private void handleGesture (int gest){
        switch (gest) {
            case ARIA_GESTURE_HOME:
                this.finish();
                break;
            case ARIA_GESTURE_ENTER:
                mTextView.setText("Enter");
                 break;

            case ARIA_GESTURE_UP:
                mTextView.setText("Up");

                break;
            case ARIA_GESTURE_DOWN:
                mTextView.setText("Down");

                break;
        }
    }

    /**
     * Standard Android object interacting with services
     */
    private ServiceConnection mConnection = new ServiceConnection() {
        /**
         * This is called when the connection with the service has been established
         */
        public void onServiceConnected(ComponentName className, IBinder service) {
            switch (className.getClassName()) {
                // You can have multiple service in your app
                // So we check if this is AriaService
                case ARIA_SERVICE_NAME:
                    // We are communicating with our service through an IDL interface,
                    // so get a client-side representation of that from the raw service object.
                    mAriaService = new Messenger(service);
                    // Register AriaService client to receive gesture messages
                    ariaRegisterClient();
                    break;

                // Check your connected services here
            }
        }

        /**
         * This is called when the connection with the service has been unexpectedly disconnected
         */
        public void onServiceDisconnected(ComponentName className) {
            switch (className.getClassName()) {
                case ARIA_SERVICE_NAME:
                    ariaUnregisterClient();
                    mAriaService = null;
                    break;

                // Check your disconnected services here
            }
        }
    };

    private void ariaRegisterClient() {
        try {
            // Send a registration message to AriaService
            // AriaService will send you gestures until you unregister
            Message msg = Message.obtain(null, ARIA_MSG_REGISTER_CLIENT);
            msg.replyTo = mMessenger;
            mAriaService.send(msg);

            // Show user we are connected to service
            Toast.makeText(MainActivity.this, "Hi", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, "Bye", Toast.LENGTH_SHORT).show();
            e.getStackTrace();
        }
    }

    private void ariaUnregisterClient() {
        try {
            // Try to send undegister message
            Message msg = Message.obtain(null, ARIA_MSG_UNREGISTER_CLIENT);
            msg.replyTo = mMessenger;
            mAriaService.send(msg);

            // Show user we are disconnected to service
            Toast.makeText(MainActivity.this, "Bye", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            // Connection to service was lost
            e.getStackTrace();
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setAmbientEnabled();

        mTextView = (TextView) findViewById(R.id.textView);
    }



    protected void onStart(){
        super.onStart();
        // Bind AriaService that's running in another process
        // You can do multiple bindings in any of your Activities or Services
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(ARIA_PACKAGE_NAME, ARIA_SERVICE_NAME));
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    protected void onStop() {
        super.onStop();

        // Unregister AriaService client first before unbind (required)
        ariaUnregisterClient();

        // Unbind from AriaService
        unbindService(mConnection);
    }


}