Application Development with Android Sensor Frameworks
DSC 291
Activity
Activity
.
Activity
.
Activity
As a user navigates through, out of, and back to your app, each activity can transition through different states in their lifecycle. The activity class provides a number of callback functions so that 1) the activity may know a state change, and 2) the activity may know how to behave as the user leaves and re-enter the activity.
For example, if you're building a streaming video player, you might pause the video and terminate the network connection when the user switches to another app. When the user returns, you can reconnect to the network and allow the user to resume the video from the same spot.
Good implementation of the lifecycle callbacks can help ensure that your app avoids:
Source: Android Developer Documentation
Intent
Intent
Sensor Overview
Android Sensor Framework Capabilities
Types of Sensor
The Android sensor framework lets you access two types of sensors.
Types of Sensor
Source: Android Developer Documentation
Android Sensor Framework
In a typical application you use these sensor-related APIs to perform two basic tasks:
Android Sensor Framework
Android Sensor Framework allows you to determine which sensors are available on the device at run time.
If you want to list all of the sensors of a given type, you could use another constant instead of TYPE_ALL such as TYPE_GYROSCOPE, TYPE_LINEAR_ACCELERATION, or TYPE_GRAVITY.
private SensorManager sensorManager;
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
List<Sensor> deviceSensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
Determining if a sensor exists
private SensorManager sensorManager;
...
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
// Success! There's a magnetometer.
} else {
// Failure! No magnetometer.
}
Determining sensor attributes
In addition to listing the sensors that are on a device, you can use the public methods of the Sensor class to determine the capabilities and attributes of individual sensors. This is useful if you want your application to behave differently based on which sensors or sensor capabilities are available on a device.
Listening to sensor data
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor mLight;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mLight = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
@Override
public final void onSensorChanged(SensorEvent event) {
// The light sensor returns a single value.
// Many sensors return 3 values, one for each axis.
float lux = event.values[0];
// Do something with this sensor value.
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
}
To monitor raw sensor data you need to implement two callback methods that are exposed through the SensorEventListener interface: onAccuracyChanged() and onSensorChanged().
Listening to sensor data
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
}
The data delay (or sampling rate) controls the interval at which sensor events are sent to your application via the onSensorChanged() callback method. The default data delay is delay of 200,000 microseconds. You can specify other data delays, such as SENSOR_DELAY_GAME(20,000 microsecond delay), SENSOR_DELAY_UI (60,000 microsecond delay), or SENSOR_DELAY_FASTEST (0 microsecond delay).
Remember, This is just a suggested or requested value to the Android Sensor Framework. There is no guarantee that you will get your sensor data sampled at your requested rate!
The best practice is that you should request the largest delay your application can tolerate.
Dealing with Sensors: Best Practices
private SensorManager sensorManager;
...
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
Android Studio Virtual Devices for Emulating Sensor Data
Android Studio Virtual Devices for Emulating Sensor Data
Plotting Sensor Data on Your Android App
repositories {
maven(url = "https://jitpack.io")
}
dependencies {
implementation("com.github.PhilJay:MPAndroidChart:v<>")
}
1. Open source: https://github.com/PhilJay/MPAndroidChart
Plotting Sensor Data on Your Android App with MPAndroidChart
chartData = new LineData(chartDataSet);//define the chartDataset
chart.setData(chartData); //set the data;
chartDataSet.addEntry(new Entry(event.timestamp,mag));//add incoming data
chartData.notifyDataChanged(); // let the data know a dataSet changed
chart.notifyDataSetChanged(); // let the chart know it's data changed
chart.invalidate();