I'm developing a wearable app and I've noticed a strange behaviour.
I have a class FooService extends Service implements SensorEventListener
and I've override onSensorChanged()
method:
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
Log.e(TAG, getString(R.string.err_wrong_sensor_registered));
return;
}
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];
...
}
This service is started from FooActivity extends WearableActivity
- here i override onEnterAmbient()
, onExitAmbient()
methods doing some UI changes only (no interaction with Service).
Everything works perfect I'm getting onSensorChanged() calls when device is plugged to USB both is Ambient Mode and in normal mode but unfortunatelly i'm not getting calls 3 seconds after device entered ambient mode when not connected to USB.
Q1: So my question is why onSensorChanged() method is not invoked when on Ambient Mode and not connected to USB?
I' have manage to force onSensorChanged() when not connected to USB and in Ambient Mode using manual wakeLock in Activity
wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
but
Q2: Shouldn't Activity has wakeLock acquired automaticaly by ambientMode mechanism?
Any suggestions will be appreciated :)
1.When system is in ambient mode, CPU performs as the same as normal, that means most time during ambient mode, the CPU is sleeping. So unless the sensor is an wake-up sensor, it cannot wake up the system, so you won't receive the onSensorChanged. The reason why it work in pluging USB is that when debug with USB, CPU do not sleep.
2.Ambient does have a mechanism to keep system awake, but the only situation that CPU should not sleep is that CPU is doing drawing action(usually refreshing the clock wallpaper). Ambient is meant to save power, so add wake lock in activity is a bad idea.
3.The code you post seems that you registered a TYPE_ACCELEROMETER sensor, this sensor has a wakeUp version and a non-wakeUp version, may be try the latter one.