Search code examples
wear-osandroid-sensorswatch-face-api

Android Wear Watchface with Sensor Data


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


Solution

  • 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.