Back to project page SensorReader.
The source code is released under:
Apache License
If you think the Android project SensorReader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package shrine.sensorreader; //from w w w .j a v a 2s . c o m import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Build; import java.io.IOException; import java.io.OutputStream; import java.util.List; /** * Get Sensor Events, extract critical information, format it (e.g. for easier gnuplot parsing or * efficient wire footprint) and write to output stream */ public class SensorReader implements SensorEventListener { protected static final String fileName = "output.csv"; private static final String CSV_SEPARATOR = ";"; /** * Output stream where the formatted data is being sent to */ private OutputStream outStream; private final Context context; /** * sampleCounter is counting the overall amount of raw sensor samples. It is needed to plot the * data with GNUPlot */ private int sampleCounter; private SensorManager mSensorManager; public SensorReader(OutputStream w, SensorManager sm, Context c) throws IOException { this(w, sm, c, true); } public SensorReader(OutputStream stream, SensorManager sm, Context c, boolean printHeader) throws IOException { sampleCounter = 0; outStream = stream; mSensorManager = sm; context = c; if (printHeader) { // Write the file header String header = getDeviceInfo() + "# sample; timestamp; type; accuracy; data1; ...; dataN\n" + "#------------------------------------------------------------------\n"; outStream.write(header.getBytes()); } } @Override public final void onAccuracyChanged(Sensor sensor, int accuracy) { // Nothing to do here } @Override public void onSensorChanged(SensorEvent event) { try { outStream.write(eventToString(event).getBytes()); } catch (IOException e) { e.printStackTrace(); } } private String eventToString(SensorEvent event) { String result = String.format("%d" + CSV_SEPARATOR + " %d" + CSV_SEPARATOR + " %d" + CSV_SEPARATOR + " %d", sampleCounter++, event.timestamp, event.sensor.getType(), event.accuracy); for (float f : event.values) { result += String.format(CSV_SEPARATOR + " %f", f); } return result + "\n"; } private String getDeviceInfo() { String deviceInfo = "# Device Information: \n"; if (null == mSensorManager) { mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); } // Get device name deviceInfo += "# Manufacturer:" + Build.MANUFACTURER + "\n"; deviceInfo += "# Model: " + Build.MODEL + "\n"; // get OS version deviceInfo += "# API-level: " + Build.VERSION.SDK_INT + "\n"; // TODO: Maybe check if the device is rooted or not. See http://stackoverflow.com/questions/1101380/determine-if-running-on-a-rooted-device // get available sensors List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ALL); for (Sensor s : sensors) { deviceInfo += gatherInfo(s); } return deviceInfo; } private String gatherInfo(Sensor s) { String result; result = "# " + s.getName() + ": \n"; result += "# Resolution: " + s.getResolution() + "\n"; result += "# Minimal Delay: " + s.getMinDelay() + "\n"; result += "# Sensor Type: " + s.getType() + "\n"; return result; } }