package io.fabo.test;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import io.fabo.driver.Adx345AccelerometerDriver;
import io.fabo.driver.ISL29034AmbientDriver;
import io.fabo.driver.S11059ColorDriver;
import io.fabo.driver.Si1132UVDriver;
public class MainActivity extends Activity implements SensorEventListener {
private Si1132UVDriver mSi1132UVDriver;
private SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerDynamicSensorCallback(new SensorManager.DynamicSensorCallback() {
@Override
public void onDynamicSensorConnected(Sensor sensor) {
Log.i(TAG, "Connected");
Log.i(TAG, "sensor.getType():" + sensor.getType());
Log.i(TAG, "sensor.getName():" + sensor.getName());
if (sensor.getType() == Sensor.TYPE_DEVICE_PRIVATE_BASE) {
Log.i(TAG, "sensor.getName():" + sensor.getName());
if(sensor.getName().startsWith("FaBoSi1132")) {
Log.i(TAG, "Color sensor connected");
mSensorManager.registerListener(MainActivity.this, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
}
});
try {
mSi1132UVDriver = new Si1132UVDriver(BoardDefaults.getI2CPort());
mSi1132UVDriver.register();
Log.i(TAG, "mSi1132UVDriver driver registered");
} catch (IOException e) {
Log.e(TAG, "Error initializing accelerometer driver: ", e);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mSi1132UVDriver != null) {
mSensorManager.unregisterListener(this);
mSi1132UVDriver.unregister();
try {
mSi1132UVDriver.close();
} catch (IOException e) {
Log.e(TAG, "Error closing accelerometer driver: ", e);
} finally {
mSi1132UVDriver = null;
}
}
}
@Override
public void onSensorChanged(SensorEvent event) {
Log.i(TAG, "event.sensor.getName()=" + event.sensor.getName());
if(event.sensor.getName().startsWith("FaBoSi1132")) {
Log.i(TAG, "FaBoSi1132");
Log.i(TAG, "UV Index(0-11): " + event.values[0] +
",IR: " + event.values[1] + "(count)" +
",Visible: " + event.values[2] +"(lux)");
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}