Help - Sensor data not appearing in Live Wallpaper

Hello, first post here.

I’m trying to make a Live Wallpaper with a tilt motion, using the accelerometer of my phone. I got the preview and the app working, but it seems like the Live Wallpaper is not capable of retrieving sensor data.

I’ve looked at the example for Live Wallpapers in the Processing for Android’s tutorials page ([Processing for Android](https://Processing for Android’s tutorial page)) and it doesn’t work, I think for the same reasons as mine doesn’t. Maybe it worked before (old android version) but then they changed the wallpaper service and it’s not able to communicate with the sensors anymore? I don’t know.

The problem is specifically when calling at the onSensorChanged. It never gets called, I suppose because the sensor never communicates with the service in which the program is running on.

Here is my accelerometer sensor code:

import processing.core.PApplet;

import java.lang.reflect.*;
import java.util.List;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class AccelerometerManager {

  private Sensor sensor;
  private SensorManager sensorManager;
  Method accelerationEventMethod;

  /** indicates whether or not Accelerometer Sensor is supported */
  private Boolean supported;
  /** indicates whether or not Accelerometer Sensor is running */
  private boolean running = false;
  PApplet parent;
  Context context;

  public AccelerometerManager(PApplet parent) {
    this.parent = parent;
    this.context = parent.getContext();

    try {
      accelerationEventMethod =
        parent.getClass().getMethod("accelerationEvent", new Class[] { Float.TYPE, Float.TYPE, Float.TYPE });
    } 
    catch (Exception e) {
      // no such method, or an error.. which is fine, just ignore
    }
    resume();
  }

  public void resume() {
    if (isSupported()) {
      startListening();
    }
  }

  public void pause() {
    if (isListening()) {
      stopListening();
    }
  }

  /*
   Returns true if the manager is listening to orientation changes
   */
  public boolean isListening() {
    return running;
  }

  /*
   Unregisters listeners
   */
  public void stopListening() {
    running = false;
    try {
      if (sensorManager != null && sensorEventListener != null) {
        sensorManager.unregisterListener(sensorEventListener);
      }
    }
    catch (Exception e) {
    }
  }

  /*
   Returns true if at least one Accelerometer sensor is available
   */
  public boolean isSupported() {
    if (supported == null) {
      sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
      List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
      supported = new Boolean(sensors.size() > 0);
    }
    return supported;
  }

  /*
   Registers a listener and start listening
   */
  public void startListening() {
    sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
    if (sensors.size() > 0) {
      sensor = sensors.get(0);
      running = sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_GAME);
    }
  }


  /*
   The listener that listen to events from the accelerometer listener
   */
  public SensorEventListener sensorEventListener = new SensorEventListener() {
    private float x = 0;
    private float y = 0;
    private float z = 0;
    
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    public void onSensorChanged(SensorEvent event) {

      x = event.values[0];
      y = event.values[1];
      z = event.values[2];
      
      // trigger change event
      
      if (accelerationEventMethod != null) {
        try {
          accelerationEventMethod.invoke(parent, new Object[] { x, y, z });
        } 
        catch (Exception e) {
          e.printStackTrace();
          accelerationEventMethod = null;
        }    
      }    
      }
    }
  };
}


(some parts differ rom it, but it’s the sensor example from APDE, shrunken and troubleshooted on)

If someone has any ideas on how to fix it I would be hugely thankful

EDIT: Forgot to add. I’m pretty sure is not about permissions. I allowed absolutely all permisions APDE has listed on the permissions selection page, and added the permission ACTIVITY_RECOGNITION that the Android Dev page says is only required for the step counting sensors, but I did it just in case, and it still doesn’t work

Hi @juanmar0driguez,

Take a look here for a working example…
(Works also on APDE live wallpaper)

https://android.processing.org/tutorials/wallpapers/index.html

Cheers
— mnse

Hello! Thanks for the reply. I checked that same example in my phone when my own code didn’t work, and the example didn’t work either. But when you told me it still works, I decided it to try it in other phone of mine (I have a Xiaomi, I tried it on a Motorola) and it did work! So I decided it was a problem of my phone, and to check if other live wallpapers with sensors worked, I installed a google play app for live motion wallpapers. Turns out, when I open the app, it tells me that if i have a Xiaomi phone, I have to disable the battery restrictions that Xiaomi automatically puts on rhw app, that don’t allow the app to make use of the sensors. So, if anyone is having the same problem, go to your Xiaomi battery settings and follow these steps





Thanks a lot @mnse !!!

1 Like