Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to get user activity from Gear using Remote Sensor

0.00/5 (No votes)
3 Jul 2014 1  
This tutorial discusses how to create a Samsung Gear Remote Sensor application.

The tutorial found below discusses how to create a Samsung Gear Remote Sensor application. Remote Sensor allows you to collect remote sensor data from a wearable device. The data can be a pedometer data, user activity event, or wearing state. Bluetooth communication facilitates the transfer of sensor data from the wearable device. The Remote Sensor Service, packaged in the SDK, uses the Gear Manager to support the Bluetooth communication with wearable device.

Introduction

For a brief overview, let me discuss what remote sensor is. Well, remote sensor allows you to collect pedomoter data, user activity events, or wearing states. Additionally, it supports Bluetooth for connection.

In this tutorial, we will teach you to re-create the sample pedometer application using the remote sensor SDK.

Let's get started. Let's start off by getting all the things we need to develop a remote sensor application with Gear 2. Make sure to have Eclipse with Androind Development tools installed. Make sure it is updated to include Jelly Bean packages. Jelly Bean is the minimum requirement to use the Samsung Remote Sensor.

We also need the following JAR files:

  • accessory-v1.0.0.jar
  • remotesensor-v1.0.0.jar
  • sdk-v1.0.0.jar

These will be used to introduce your remote sensor to the device. These files can be found in the Samsung Mobile SDK 1.5 beta archive file. (Remote Sensor Library)

Also, make sure to have a Samsung Android Smart Device that has Jelly Bean OS or above. And of course, a Samsung Gear 2.

Install the following in order. First install Gear Manager, followed by the remote sensor service. You can download this from Samsung apps.

And finally, the remote sensor application. We'll be doing this later for testing. But you can go ahead and download this from the sample application attached to this post.

Let's learn the architecture and classes of the remote sensor library. Basically, the layers are composed of the application layer, which is an application that uses remote sensor. The sensor protocol, that is primarily responsible in accessing wearable device sensor data. And finally, the remote sensor service, and the wearable sensor service, which are responsible for Bluetooth connectivity.

Let's proceed with the classes. First, the Srs class. This class provides methods for initializing the Remote Sensor package and for checking if the Remote Sensor is supported.

It has four methods: getVersionCode, getVersionName, initialize, and IsFeatureEnabled.

For IsFeatureEnabled we have TYPE_GEAR_MANAGER, a constant used to check whether your manager is supported or not. We have TYPE_GEAR_FIT_MANAGER, a constant used to check whether gear fit manager is supported or not. And lastly, TYPE_REMOTE_SENSOR_SERVICE, a constant used to check whether remote sensor service is supported or not.

remoteSensor = new Srs();

try {
remoteSensor.initialize(mContext);

} catch (SsdkUnsupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();

)

The SrsRemoteSensorManager is a class that provides access to the wearable device sensor. Note that the system does not disable sensors automatically when the screen turns off. That is why you should always disable sensors when you do not need them, especially when your activity is on pause.

As seen in the code displayed below, failure to do so can drain the battery in just a few hours.

mServiceManager = new SrsRemoteSensorManager(remoteSensor);

... 

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();

mServiceManager.unregisterListener(this, wearableStatusSensor);

...

}

The SrsRemoteSensorManager consists of four methods, mainly: GET_SENSOR_LIST, REGISTER_LISTENER, REQUEST_TRIGGER_SENSOR, and UNREGISTER_LISTENER. It has a nested class SrsRemoteSensorManager.eventlistener which is a listener interface that receives sensor data events. We shall see the use of this class once we proceed to our sample.

The SrsRemoteSensor is an interface that provides remote sensor information. It provides several useful methods, such as retrieving the device type or version, getting how much power the sensor is using, inquiring the resolution of the sensor, and the life.

The last one, SrsRemoteSensorEvent. This class represents a sensor event and holds event information such as sensor type, timestamp, accuracy, and sensor data.

public void onSensorValueChanged(final SrsRemoteSensorEvent event){
        ...
    if(event.sensor.getType() ==
        SrsRemoteSensor.TYPE_WEARING_STATE){
        ...
    }

    if(event.sensor.getType() ==
        SrsRemoteSensor.TYPE_USER_ACTIVITY){
        ...
    }

    if(event.sensor.getType() ==
        SrsRemoteSensor.TYPE_PEDOMETER){
        ...
    }
        ...
{

The values array depends on which sensor type is being monitored. TYPE_USER_ACTIVITY provides us three values: UNKNOWN_STATE, WALK, and RUN. TYPE_PEDOMETER gives us the value of the step counter. This event is updated every five minutes to reduce the power consumption of the wearable device. TYPE_WEARING_STATE gives us two values, NOT_WEARING, and WEARING.

Let's analyze the sample application. This is also similar to the sample from Samsung Mobile SDK 1.5 beta. Let's open our Eclipse IDE. As you can see we have an app named RSDemo with an API set to 18.

Now let's check the libraries.

It's complete. And now the manifest permissions:

These are to use the Bluetooth feature, as well as the accessory framework and remote sensor.

Let's check out the XML file. Basically these text views will display data for the wearable status, user activity, and pedometer data. The button will act as a refresh button, meaning this button will retrieve the data from the wearable device.

Let's go to MainActivity.java. On the onCreate method, we see Srs being initialized.

remoteSensor = new Srs();

try {
    remoteSensor.initialize(mContext);

} catch (SsdkUnsupportedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

    switch (e.getType ()) {
        case SsdkUnsupportedException.LIBRARY_NOT_INSTALLED;
            // Handle the exception
            break;
        case SsdkUnsupportedException.LIBRARY_UPDATE_IS_REQUIRED;
            // Handle the exception
            break;
        default:
            // Handle the exception
            break;
        }
    }

We created the service manager as well.

mServiceManager = new SrsRemoteSensorManager(remoteSensor);

Moving on, let's see check the onSensorValueChanged method. Here we see how to retrieve from the values array.

// Called when sensor values have changed.
@Override
public void onSensorValueChanged(final SrsRemoteSensorEvent event) {
    runOnUiThread(new Runnable() { @Override public void run() {

        if(event.sensor.getType() == SrsRemoteSensor.TYPE_WEARING_STATE){
            if(event.values[0] == 1.0)
            {    wearableStatus.setText("User is WEARING the device.");
            }else{
                 wearableStatus.setText("User is NOT WEARING the device.");
            }
        }

        if (event.sensor.getType() == SrsRemoteSensor.TYPE_USER_ACTIVITY)
            if(event.values[0] == 2.0)
            {    userActivity.setText("User is in WALKING.");
            }else{
                 userActivity.setText("User is in an UNKNOWN STATE.");
            }
        }

        if (event.sensor.getType() == SrsRemoteSensor.TYPE_PEDOMETER)
        {    pedometerData.setText("User has taken " + Float.toString(event.values[0]) + " steps.");
        }

     }
     });
}

We see how data is retrieved from the wearable, through the registered sensors and the speed of the sensor.

public void getWearableEvent(View view){
    wearableStatusList = mServiceManager.getSensorList(SrsRemoteSensor.TYPE_WEARING_STATE);
    wearableStatusSensor = wearableStatusList.get(0);
    mServiceManager.registerListener(this, wearableStatusSensor, SrsRemoteSensorManager.SENSOR_DELAY_NORMAL, 0);
}

public void getActivityEvent(View view){
    activitySensorList = mServiceManager.getSensorList(SrsRemoteSensor.TYPE_USER_ACTIVITY);
    userActivitySensor = activitySensorList.get(0);
    mServiceManager.registerListener(this, userActivitySensor, SrsRemoteSensorManager.SENSOR_DELAY_NORMAL, 0);
}

public void getPedometerEvent(View view){
    pedoSensorList = mServiceManager.getSensorList(SrsRemoteSensor.TYPE_PEDOMETER);
    pedometerSensor = pedoSensorList.get(0);
    mServiceManager.registerListener(this, pedometerSensor, SrsRemoteSensorManager.SENSOR_DELAY_NORMAL, 0);
}

All these methods are called under the "Check Info" button.

btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
            getWearableEvent(v);
            getActivityEvent(v);
            getPedometerEvent(v);

    }
});

Now that we're done let's test our application. Before that, remember to install gear manager and the remote sensor service. Make sure that the wearable and the host device are connected successfully.

Once those conditions are met you can install the android application and simply enable the pedometer in your Gear 2 device. Walk around to gather data, then tap on the "Check User Info" button on your android application to retrieve the data on your wearable device.

The data is sent over your phone over Bluetooth via the remote sensor SDK.

The Remote Sensor package can be utilized to get user activity from the wearable device. That is, the application in the host device is notified when the user of the wearable device walks or runs. Changing an activity will also notify the user. Using the Remote Sensor, the wearable device can also provide the pedometer data from the start of the day until the current time. You can get data every 5 minutes. The state sensor provides the current state of the wearable device, whether it is worn or not. This is done by registering an event listener. Data is provided as soon as 1 second after registration.

This tutorial provides sample application, please check out the attached file.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here