简体   繁体   中英

LibGDX light sensor data

I am using LibGDX to make a desktop/android application. In part of my application I am checking the light sensor if it is android. This works fine for android except when I implement it into my LibGDX project I am confused as to how I can pass this data across.

SensorManager sm;
Sensor s;
float lux = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
s = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
initialize(new MyLibGDXMain(), cfg);
}

@Override
public void onSensorChanged(SensorEvent event) {
lux = event.values[0];
}

I can retrieve my light sensor data here but once I initialize MyLibGDXMain I lose this data. If I add the SensorEventListener to that class I lose compatibility with the desktop version. So I have tried to create another constructor for the MyLibGDXMain to pass the lux value over but this only breaks whatever the LibGDX application is doing.

This question is not how to retrieve sensor data on android but rather How would I go about getting this sensor data into my LibGDX main without breaking compatibility with the desktop project?

You will need to create an interface between your application and the platform-specific code...

https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code

One way would be to create an Interface LightSensorInterface in your main project, containing a method float getCurrentLux();

public interface LightSensorInterface {
    public float getCurrentLux();
}

You then would have to create a class that implements that interface in your android project and one in your desktop project as well.

public class AndroidLightSensor implements LightSensorInterface, SensorEventListener {
    private float currentLux = 0;

    @Override
    public float getCurrentLux() {
        return currentLux;
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        this.currentLux = event.values[0];
    }
}

Since you don't have a light sensor on your desktop application, the implemented method there could just return some fixed value.

And the implemented getCurrentLux() in the android project needs to return the lux value that you got the last time onSensorChanged() was called...

The constructor of your MyLibGDXMain() will need a reference to a LightSensorInterface and when initializing your app, you would pass the platform-specific class:

onCreate() {
     ....
    AndroidLightSensor sensor = new AndroidLightSensor(...);
    initialize(new MyLibGDXMain(sensor);
}

If you want to always have the current value, you need to fetch the current value within your render loop... hope you get what I mean...

Hope it helps.. ::)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM