1

Apologies for the lack of code examples, but anything I've tried so far has been a complete mess.

I've tried searching for the information online, maybe I'm not searching for the correct terms.

I can create a watch face using samples in Android Studio & I've created an application that retrieve data from the sensors. What I can't do is combine the two.

What I'm trying to do is create an Android Wear watchface that can display various pieces of data collected from the sensors on board the watch.

For example, the accelerometer is always on in order to enable wrist gestures.

Can anyone point me in the right direction as to how to go about solving this, without posting a link to the android API, as I've exhausted that.

Regards,

Emmett

  • If you are planning to have data from sensors to be collected all the time, I would say that is not in the best interest of the users since that kills the battery in no time. A good watchface goes into the ambient mode and while in that state, the CPU is in an almost sleep state to preserve the battery. In that state, it is common to show very basic info to the user, which is usually hour and minute (not even second) and maybe some more static/less changing data. When it comes out if that state, you get a callback and you can do some ore fancy work like reading sensor values. – Ali Naddaf Nov 02 '15 at 01:00
  • Ok thanks. Initially, I just want to see can I access the data from the watch face, the goal is to not display the data, but have it listening for certain events that may generated by changes in the sensors. I've tried searching for tutorials and code examples to basically display steps or a heart beat on the watch face to no avail. – mangledBadger Nov 02 '15 at 09:39
  • You might want to describe what issue you are running into when you try to access the sensor data from a watchface to get direct help there. – Ali Naddaf Nov 02 '15 at 15:24
  • Issues: I'm trying to retrieve the readings from the accelerometer, and have them displayed on the watch face when the readings change, ie detect movement, as X, Y, Z readings. The accelerometer is always active, I assume, as gesture detection allows the watch to leave ambient mode. – mangledBadger Nov 02 '15 at 15:30
  • You keep saying what you want to do but you are not saying where it fails when you try to do that; we are asking for what issues you are running into not what you are trying to do; you need to show your code and logs associated with that – Ali Naddaf Nov 02 '15 at 15:35
  • It's failing because logically, I don't know the steps required to this. I'm not looking for a solution, I'm looking for assistance from someone who may have done this before or who knows how to do it, or someone who can tell me the steps required, logically, to implement this. I don't have code at present because it is messy and all over the place from trying different approaches. – mangledBadger Nov 03 '15 at 09:34

1 Answers1

4

Ok, so after 3 days of playing around and frustrating errors, I've managed to solve it, if anyone else needs to figure this out:

First implement the SensorEventListener:

private class Engine extends CanvasWatchFaceService.Engine implements SensorEventListener

Next, within the onVisibilityChanged, I registered two methods I've created:

 @Override
    public void onVisibilityChanged(boolean visible) {
        super.onVisibilityChanged(visible);

        if (visible) {
            registerReceiver();
            registerAccelerometer();

            mTime.clear(TimeZone.getDefault().getID());
            mTime.setToNow();
        } else {
            unregisterReceiver();
            unregisterAccelerometer();
        }
        updateTimer();
    }

And the two methods:

private void registerAccelerometer() {
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
    }
private void unregisterAccelerometer() {
        sensorManager.unregisterListener(this);
    }

Then, within the onSensorChanged, where I listen for accelerometer changes:

@Override
    public void onSensorChanged(SensorEvent sensorEvent) {

        ......
        // Calculate here
        }
        sensorX  = (int)linear_acceleration[0];
        sensorY  = (int)linear_acceleration[1];
        sensorZ  = (int)linear_acceleration[2];
    }

Assigning these to the three variables, before the onDraw() method is called:

@Override
    public void onDraw(Canvas canvas, Rect bounds) {
        // Get the current Time
        mTime.setToNow();

        // Set the current accelerometer readings.
        accelerometerX.setText("X : " + String.valueOf(sensorX));
        accelerometerY.setText("Y : " + String.valueOf(sensorY));
        accelerometerZ.setText("Z : " + String.valueOf(sensorZ));
        accelerometerRateOfChange.setText("T: " + String.valueOf(totalDisplacement));
    }

In theory, this should be the same for all sensors, not just the accelerometer.

It is worth noting that I created this using Android Studio's sample apps.