fixed several potential nullptrs

code cleanups
logging enhancements (memory/cpu)
reduced log footprint (diskspace)
fixed some potential layout issues
added comments
This commit is contained in:
2016-06-06 16:02:25 +02:00
parent 69ad4c2272
commit 79e0acf7ed
22 changed files with 456 additions and 401 deletions

1
.idea/.name generated
View File

@@ -1 +0,0 @@
SensorReadout

6
.idea/encodings.xml generated
View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

9
.idea/gradle.xml generated
View File

@@ -5,20 +5,13 @@
<GradleProjectSettings> <GradleProjectSettings>
<option name="distributionType" value="LOCAL" /> <option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="$APPLICATION_HOME_DIR$/gradle/gradle-2.10" /> <option name="gradleHome" value="D:\programme\android-studio\gradle\gradle-2.10" />
<option name="gradleJvm" value="1.8" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" /> <option value="$PROJECT_DIR$/app" />
</set> </set>
</option> </option>
<option name="myModules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
</set>
</option>
</GradleProjectSettings> </GradleProjectSettings>
</option> </option>
</component> </component>

View File

@@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="org.jetbrains.plugins.gradle.execution.test.runner.AllInPackageGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer" />
</set>
</option>
</component>
</project>

2
.idea/vcs.xml generated
View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="" vcs="Git" />
</component> </component>
</project> </project>

View File

@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android { android {
compileSdkVersion 23 compileSdkVersion 23
buildToolsVersion "23.0.2" buildToolsVersion '23.0.3'
defaultConfig { defaultConfig {
applicationId "de.fhws.indoor.sensorreadout" applicationId "de.fhws.indoor.sensorreadout"
@@ -20,10 +20,10 @@ android {
} }
dependencies { dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.android.support:wearable:1.3.0' compile 'com.google.android.support:wearable:1.3.0'
compile 'com.google.android.gms:play-services-wearable:8.4.0' //compile 'com.google.android.gms:play-services-wearable:8.4.0'
//provided 'com.google.android.wearable:wearable:1.0.0' //provided 'com.google.android.wearable:wearable:1.0.0'
} }

View File

@@ -2,7 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.fhws.indoor.sensorreadout">> package="de.fhws.indoor.sensorreadout">>
<!--
<uses-feature android:name="android.hardware.type.watch" /> <uses-feature android:name="android.hardware.type.watch" />
-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />

View File

@@ -1,15 +1,14 @@
package de.fhws.indoor.sensorreadout; package de.fhws.indoor.sensorreadout;
import android.Manifest; import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Color; import android.graphics.Color;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.wearable.activity.WearableActivity;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
@@ -32,7 +31,7 @@ import de.fhws.indoor.sensorreadout.sensors.mySensor;
import de.fhws.indoor.sensorreadout.sensors.SensorType; import de.fhws.indoor.sensorreadout.sensors.SensorType;
public class MainActivity extends WearableActivity { public class MainActivity extends Activity {
private final ArrayList<mySensor> sensors = new ArrayList<mySensor>(); private final ArrayList<mySensor> sensors = new ArrayList<mySensor>();
private final ArrayList<TextView> txtFields = new ArrayList<TextView>(); private final ArrayList<TextView> txtFields = new ArrayList<TextView>();
@@ -41,17 +40,21 @@ public class MainActivity extends WearableActivity {
private Button btnStop; private Button btnStop;
private Button btnGround; private Button btnGround;
private int groundTruthCounter = 0; private int groundTruthCounter = 0;
private int loadCounterWifi = 0;
private int loadCounterBeacon = 0;
private boolean isInitialized = false; private boolean isInitialized = false;
final private int MY_PERMISSIONS_REQUEST_READ_BT = 123; final private int MY_PERMISSIONS_REQUEST_READ_BT = 123;
final private int MY_PERMISSIONS_REQUEST_READ_HEART = 321; final private int MY_PERMISSIONS_REQUEST_READ_HEART = 321;
// static context access
private static Context context;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
// context access
MainActivity.context = getApplicationContext();
//init Path spinner //init Path spinner
final Spinner pathSpinner = (Spinner) findViewById(R.id.pathspinner); final Spinner pathSpinner = (Spinner) findViewById(R.id.pathspinner);
List<String> pathList = new ArrayList<String>(); List<String> pathList = new ArrayList<String>();
@@ -90,7 +93,7 @@ public class MainActivity extends WearableActivity {
sensors.add(grndTruth); sensors.add(grndTruth);
grndTruth.setListener(new mySensor.SensorListener() { grndTruth.setListener(new mySensor.SensorListener() {
@Override public void onData(final String csv) { return; } @Override public void onData(final String csv) { return; }
@Override public void onData(int id, final String csv) {add(id, csv); } @Override public void onData(SensorType id, final String csv) {add(id, csv); }
}); });
@@ -167,15 +170,15 @@ public class MainActivity extends WearableActivity {
sensors.add(phoneSensors); sensors.add(phoneSensors);
phoneSensors.setListener(new mySensor.SensorListener(){ phoneSensors.setListener(new mySensor.SensorListener(){
@Override public void onData(final String csv) { return; } @Override public void onData(final String csv) { return; }
@Override public void onData(int id, final String csv) {add(id, csv); } @Override public void onData(final SensorType id, final String csv) {add(id, csv); }
}); });
// logo wifi using sensor number 8 // logo wifi using sensor number 8
final WiFi wifi = new WiFi(this); final WiFi wifi = new WiFi(this);
sensors.add(wifi); sensors.add(wifi);
wifi.setListener(new mySensor.SensorListener() { wifi.setListener(new mySensor.SensorListener() {
@Override public void onData(final String csv) { add(SensorType.WIFI.ordinal(), csv); } @Override public void onData(final String csv) { add(SensorType.WIFI, csv); }
@Override public void onData(int id, final String csv) {return; } @Override public void onData(final SensorType id, final String csv) {return; }
}); });
// log iBeacons using sensor number 9 // log iBeacons using sensor number 9
@@ -188,8 +191,8 @@ public class MainActivity extends WearableActivity {
sensors.add(beacon); sensors.add(beacon);
beacon.setListener(new mySensor.SensorListener() { beacon.setListener(new mySensor.SensorListener() {
@Override public void onData(final String csv) { add(SensorType.IBEACON.ordinal(), csv); } @Override public void onData(final String csv) { add(SensorType.IBEACON, csv); }
@Override public void onData(int id, final String csv) {return; } @Override public void onData(final SensorType id, final String csv) {return; }
}); });
final LinearLayout lay = (LinearLayout) findViewById(R.id.layoutMain); final LinearLayout lay = (LinearLayout) findViewById(R.id.layoutMain);
@@ -197,7 +200,8 @@ public class MainActivity extends WearableActivity {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
params.setMargins(10,10,10,10); params.setMargins(10,10,10,10);
for (int i = 0; i < sensors.size(); ++i) { // fixed number of textfields
for (int i = 0; i < 16; ++i) {
final TextView tv = new TextView(this); final TextView tv = new TextView(this);
//Set margin and color //Set margin and color
@@ -239,39 +243,33 @@ public class MainActivity extends WearableActivity {
private void stop() { private void stop() {
logger.stop(); logger.stop();
for(int i = 0; i < txtFields.size(); i++) for(int i = 0; i < txtFields.size(); i++) {
{
txtFields.get(i).setText(""); txtFields.get(i).setText("");
} }
for (final mySensor s : sensors) {s.onPause(this);} for (final mySensor s : sensors) {s.onPause(this);}
} }
/** new sensor data */ /** new sensor data */
private void add(final int nr, final String csv) { private int loadCounterWifi = 0;
logger.addCSV(nr, csv); private int loadCounterBeacon = 0;
private int loadCounterAny = 0;
private void add(final SensorType id, final String csv) {
logger.addCSV(id, csv);
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override public void run() { @Override public void run() {
if (nr == 8) { if (id == SensorType.WIFI) {
if (loadCounterWifi % 2 == 0) { txtFields.get(0).setText( ((++loadCounterWifi % 2) == 0) ? "wi" : "WI");
txtFields.get(0).setText("wifi"); } else if (id == SensorType.IBEACON){
++loadCounterWifi; txtFields.get(1).setText( ((++loadCounterBeacon % 2) == 0) ? "ib" : "IB");
} else {
txtFields.get(0).setText("WIFI");
++loadCounterWifi;
}
} }
if (nr == 9){ if (++loadCounterAny % 10 == 0) {
if(loadCounterBeacon % 2 == 0){ txtFields.get(2).setText( (logger.getSize() / 1024) + " kb - " + logger.getNumEntries());
txtFields.get(1).setText("beacon");
++loadCounterBeacon;
} else {
txtFields.get(1).setText("BEACON");
++loadCounterBeacon;
}
} }
} }
}); });
@@ -300,4 +298,9 @@ public class MainActivity extends WearableActivity {
} }
} }
/** static context access */
public static Context getAppContext() {
return MainActivity.context;
}
} }

View File

@@ -26,15 +26,22 @@ public class DataFolder {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
folder = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), folderName); folder = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), folderName);
} else { } else if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
folder = new File(Environment.getExternalStorageDirectory() + "/" + folderName); folder = new File(Environment.getExternalStorageDirectory() + "/" + folderName);
} else {
folder = new File(context.getApplicationInfo().dataDir);
} }
// build folders
folder.mkdirs(); folder.mkdirs();
boolean isExtistingFolder = folder.exists();
boolean isDirectory = folder.isDirectory();
Log.d("DataFolder", String.valueOf(isExtistingFolder)); // sanity check:
if (!folder.exists() || !folder.isDirectory()) {
throw new MyException("failed to create/access storage folder: " + folder.getAbsolutePath());
} else {
Log.d("dataFolder", "using: " + folder);
}
} }
public File getFolder(){ public File getFolder(){

View File

@@ -11,18 +11,17 @@ public class GroundTruth extends mySensor {
//empty as my soul //empty as my soul
} }
public void writeGroundTruth(int groundTruthCounter){ public void writeGroundTruth(final int groundTruthCounter){
if (listener != null){listener.onData(SensorType.GROUND_TRUTH,
if (listener != null){listener.onData(99, Integer.toString(groundTruthCounter)
Integer.toString(groundTruthCounter) + ";"
);} );}
} }
public void writeInitData(int pathID, int numGroundTruthPoints){ public void writeInitData(int pathID, int numGroundTruthPoints){
if (listener != null){listener.onData(-1, if (listener != null){listener.onData(SensorType.GROUND_TRUTH_PATH,
Integer.toString(pathID) + ";" + Integer.toString(pathID) + ";" +
Integer.toString(numGroundTruthPoints) + ";" Integer.toString(numGroundTruthPoints)
);} );}
} }

View File

@@ -0,0 +1,17 @@
package de.fhws.indoor.sensorreadout.sensors;
/**
* Created by Frank on 05.06.2016.
*/
public class Helper {
/** remove all ":" within the MAC to reduce file footprint */
public static final String stripMAC(final String mac) {
return mac.replaceAll(":", "");
}
}

View File

@@ -1,25 +1,25 @@
package de.fhws.indoor.sensorreadout.sensors; package de.fhws.indoor.sensorreadout.sensors;
/** ///**
* Created by Frank on 04.02.2016. // * Created by Frank on 04.02.2016.
*/ // */
public class Limiter { //public class Limiter {
//
long last_ms = 0; // long last_ms = 0;
final int limit_ms; // final int limit_ms;
//
/** ctor */ // /** ctor */
Limiter(final int limit_ms) { // Limiter(final int limit_ms) {
this.limit_ms = limit_ms; // this.limit_ms = limit_ms;
} // }
//
/** limit reached? */ // /** limit reached? */
boolean isOK() { // boolean isOK() {
final long cur_ms = System.currentTimeMillis(); // final long cur_ms = System.currentTimeMillis();
final long diff = cur_ms - last_ms; // final long diff = cur_ms - last_ms;
final boolean ok = diff > limit_ms; // final boolean ok = diff > limit_ms;
if (ok) {last_ms = cur_ms;} // if (ok) {last_ms = cur_ms;}
return ok; // return ok;
} // }
//
} //}

View File

@@ -13,44 +13,47 @@ import java.io.OutputStreamWriter;
* log sensor data to file * log sensor data to file
* Created by Frank on 25.03.2015. * Created by Frank on 25.03.2015.
*/ */
public class Logger { public final class Logger {
//private BufferedWriter bw; private StringBuilder sb = new StringBuilder();
private ByteArrayOutputStream baos;
private OutputStreamWriter osw;
private File file; private File file;
private FileOutputStream fos;
private Context context; private Context context;
private int entries = 0;
public Logger(Context context) { public Logger(Context context) {
this.context = context; this.context = context;
} }
/** start logging (into RAM) */ /** start logging (into RAM) */
public void start() { public final void start() {
// start empty
sb.setLength(0);
entries = 0;
// open the output-file immeditaly (to get permission errors)
// but do NOT yet write anything to the file
DataFolder folder = new DataFolder(context, "sensorOutFiles"); DataFolder folder = new DataFolder(context, "sensorOutFiles");
file = new File(folder.getFolder(), System.currentTimeMillis() + ".csv"); file = new File(folder.getFolder(), System.currentTimeMillis() + ".csv");
Log.d("logger", file.toString()); Log.d("logger", "will write to: " + file.toString());
baos = new ByteArrayOutputStream();
osw = new OutputStreamWriter(baos); try {
fos = new FileOutputStream(file);
} catch (final Exception e) {
throw new MyException("error while opening log-file", e);
}
} }
/** stop logging and flush RAM-data to the flash-chip */ /** stop logging and flush RAM-data to the flash-chip */
public void stop() { public final void stop() {
synchronized (this) { synchronized (this) {
try { try {
try { fos.write(sb.toString().getBytes());
// finally write the ram data to disk fos.close();
osw.close();
final FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.close();
} catch (final Exception e) {
throw new RuntimeException(e);
}
} catch (final Exception e) { } catch (final Exception e) {
throw new RuntimeException(e); throw new MyException("error while writing log-file", e);
} }
} }
} }
@@ -59,23 +62,29 @@ public class Logger {
return file; return file;
} }
public int getSize() {return sb.length();}
public int getNumEntries() {return entries;}
/** add a new CSV entry for the given sensor number */ /** add a new CSV entry for the given sensor number to the internal buffer */
public void addCSV(final int sensorNr, final String csv) { public final void addCSV(final SensorType sensorNr, final String csv) {
final String s = System.currentTimeMillis() + ";" + sensorNr + ";" + csv;
// log to RAM
synchronized (this) { synchronized (this) {
try { sb.append(System.currentTimeMillis());
osw.write(s); sb.append(';');
osw.write("\r\n"); sb.append(sensorNr.id());
} catch (final Exception e) { sb.append(';');
throw new RuntimeException(e); sb.append(csv);
} sb.append("\n");
++entries;
} }
debug();
}
int cnt = 0;
private final void debug() {
if (++cnt % 10 == 0) {
Log.d("buffer", "size: " + sb.length());
}
} }
} }

View File

@@ -0,0 +1,24 @@
package de.fhws.indoor.sensorreadout.sensors;
import android.widget.Toast;
import de.fhws.indoor.sensorreadout.MainActivity;
import de.fhws.indoor.sensorreadout.R;
/**
* Created by Frank on 05.06.2016.
*/
public class MyException extends RuntimeException {
MyException(final String err, final Throwable t) {
super(err, t);
Toast.makeText(MainActivity.getAppContext(), err, Toast.LENGTH_LONG);
}
MyException(final String err) {
super(err);
Toast.makeText(MainActivity.getAppContext(), err, Toast.LENGTH_LONG);
}
}

View File

@@ -7,20 +7,25 @@ import android.hardware.SensorEvent;
import android.hardware.SensorEventListener; import android.hardware.SensorEventListener;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.os.Build; import android.os.Build;
import android.util.Log;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Wearable;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
/** /**
* all available sensors
* and what to do within one class
*
*
* Created by Toni on 25.03.2015. * Created by Toni on 25.03.2015.
*/ */
public class PhoneSensors extends mySensor implements SensorEventListener{ public class PhoneSensors extends mySensor implements SensorEventListener{
private static final int SENSOR_TYPE_HEARTRATE = 65562;
private SensorManager sensorManager; private SensorManager sensorManager;
private Sensor acc; private Sensor acc;
private Sensor grav; private Sensor grav;
@@ -35,20 +40,19 @@ public class PhoneSensors extends mySensor implements SensorEventListener{
private Sensor light; private Sensor light;
private Sensor temperature; private Sensor temperature;
private static final int SENSOR_TYPE_HEARTRATE = 65562; /** local gravity copy (needed for orientation matrix) */
private float[] mGravity = new float[3]; private float[] mGravity = new float[3];
/** local geomagnetic copy (needed for orientation matrix) */
private float[] mGeomagnetic = new float[3]; private float[] mGeomagnetic = new float[3];
//Limiter limitGyro = new Limiter(10);
//Limiter limitMag = new Limiter(10);
//Limiter limitAcc = new Limiter(10);
Limiter limitAOri = new Limiter(10);
/** ctor */
public PhoneSensors(final Activity act){ public PhoneSensors(final Activity act){
// fetch the sensor manager from the activity
sensorManager = (SensorManager) act.getSystemService(Context.SENSOR_SERVICE); sensorManager = (SensorManager) act.getSystemService(Context.SENSOR_SERVICE);
// try to get each sensor
acc = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); acc = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
grav = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY); grav = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
lin_acc = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION); lin_acc = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
@@ -62,265 +66,237 @@ public class PhoneSensors extends mySensor implements SensorEventListener{
light = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); light = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
temperature = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE); temperature = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
//Write Vendor to file // dump sensor-vendor info to file
dumpVendors(act);
DataFolder folder = new DataFolder(act, "sensorOutFiles"); }
File file = new File(folder.getFolder(), "vendors.txt");
final char NL = '\n';
/** Write Vendors to file */
private void dumpVendors(final Activity act) {
final DataFolder folder = new DataFolder(act, "sensorOutFiles");
final File file = new File(folder.getFolder(), "vendors.txt");
try { try {
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
//write sensor vendor information final FileOutputStream fos = new FileOutputStream(file);
bw.write("Device: " + android.os.Build.MODEL); final StringBuilder sb = new StringBuilder();
bw.newLine();
bw.write("Android: " + Build.VERSION.RELEASE);
bw.newLine();
bw.newLine();
bw.write("id" + String.valueOf(SensorType.ACCELEROMETER.ordinal()) + " Accelerometer: " + getSensorName(acc));
bw.newLine();
bw.write("id" + String.valueOf(SensorType.GRAVITY.ordinal()) + " Gravity: " + getSensorName(grav));
bw.newLine();
bw.write("id" + String.valueOf(SensorType.LINEAR_ACCELERATION.ordinal()) + " Linear Acceleration: " + getSensorName(lin_acc));
bw.newLine();
bw.write("id" + String.valueOf(SensorType.GYROSCOPE.ordinal()) + " Gyroscope: " + getSensorName(gyro));
bw.newLine();
bw.write("id" + String.valueOf(SensorType.MAGNETIC_FIELD.ordinal()) + " Magnetic Field: " + getSensorName(magnet));
bw.newLine();
bw.write("id" + String.valueOf(SensorType.PRESSURE.ordinal()) + " Pressure: " + getSensorName(press));
bw.newLine();
bw.write("id" + String.valueOf(SensorType.RELATIVE_HUMIDITY.ordinal()) + " Humidity: " + getSensorName(humidity));
bw.newLine();
bw.write("id" + String.valueOf(SensorType.ORIENTATION_OLD.ordinal()) + " Orientation Old: " + getSensorName(ori));
bw.newLine();
bw.write("id" + String.valueOf(SensorType.LIGHT.ordinal()) + " Light: " + getSensorName(light));
bw.newLine();
bw.write("id" + String.valueOf(SensorType.AMBIENT_TEMPERATURE.ordinal()) + " Temperature: " + getSensorName(temperature));
bw.newLine();
bw.write("id" + String.valueOf(SensorType.HEART_RATE.ordinal()) + " Heart Rate: " + getSensorName(heart));
bw.newLine();
bw.close(); // constructor smartphone details
fw.close(); sb.append("Device: " + android.os.Build.MODEL).append(NL);
sb.append("Android: " + Build.VERSION.RELEASE).append(NL);
}catch (IOException e) { // construct sensor details
e.printStackTrace(); dumpSensor(sb, SensorType.ACCELEROMETER, acc);
dumpSensor(sb, SensorType.GRAVITY, grav);
dumpSensor(sb, SensorType.LINEAR_ACCELERATION, lin_acc);
dumpSensor(sb, SensorType.GYROSCOPE, gyro);
dumpSensor(sb, SensorType.MAGNETIC_FIELD, magnet);
dumpSensor(sb, SensorType.PRESSURE, press);
dumpSensor(sb, SensorType.RELATIVE_HUMIDITY, humidity);
dumpSensor(sb, SensorType.ORIENTATION_OLD, ori);
dumpSensor(sb, SensorType.LIGHT, light);
dumpSensor(sb, SensorType.AMBIENT_TEMPERATURE, temperature);
dumpSensor(sb, SensorType.HEART_RATE, heart);
// write
fos.write(sb.toString().getBytes());
fos.close();
}catch (final IOException e) {
throw new RuntimeException(e);
} }
} }
private String getSensorName(Sensor sensor){ /** dump all details of the given sensor into the provided stringbuilder */
private void dumpSensor(final StringBuilder sb, final SensorType type, final Sensor sensor) {
if(sensor != null){ sb.append("Sensor").append(NL);
sb.append("id: ").append(type.ordinal()).append(NL);
StringBuilder message = new StringBuilder(2048); sb.append("type: ").append(type).append(NL);
message.append("\nVendor: " + sensor.getVendor() + "\n"); if (sensor != null) {
message.append("Version: " + sensor.getVersion() + "\n"); sb.append("\tVendor: ").append(sensor.getVendor()).append(NL);
message.append("MinDelay: " + sensor.getMinDelay() + "\n"); sb.append("\tVersion: ").append(sensor.getVersion()).append(NL);
//message.append("MaxDelay: " + sensor.getMaxDelay() + "\n"); sb.append("\tMinDelay: ").append(sensor.getMinDelay()).append(NL);
message.append("MaxRange: " + sensor.getMaximumRange() + "\n"); //sb.append("\tMaxDelay: ").append(sensor.getMaxDelay()).append(NL);
message.append("Power: " + sensor.getPower() + "\n"); sb.append("\tMaxRange: ").append(sensor.getMaximumRange()).append(NL);
//message.append("ReportingMode: " + sensor.getReportingMode() + "\n"); sb.append("\tPower: ").append(sensor.getPower()).append(NL);
message.append("Resolution: " + sensor.getResolution() + "\n"); //sb.append("ReportingMode: ").append(sensor.getReportingMode()).append(NL);
message.append("Type: " + sensor.getType() + "\n\n"); sb.append("\tResolution: ").append(sensor.getResolution()).append(NL);
//message.append("FifoMaxEventCount: " + sensor.getFifoMaxEventCount() + "\n"); sb.append("\tType: ").append(sensor.getType()).append(NL);
//message.append("FifoReservedEventCount: " + sensor.getFifoReservedEventCount() + "\n"); } else {
sb.append("\tnot available!\n");
return message.toString();
} else{
return "\nSensor not in device\n";
} }
sb.append("\n");
} }
@Override @Override
public void onSensorChanged(SensorEvent event) { public void onSensorChanged(SensorEvent event) {
// to compare with the other orientation // to compare with the other orientation
if(event.sensor.getType() == Sensor.TYPE_ORIENTATION) { if(event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
if (listener != null){ // inform listeners
listener.onData(SensorType.ORIENTATION_OLD.ordinal(), if (listener != null){
listener.onData(SensorType.ORIENTATION_OLD,
Float.toString(event.values[0]) + ";" + Float.toString(event.values[0]) + ";" +
Float.toString(event.values[1]) + ";" + Float.toString(event.values[1]) + ";" +
Float.toString(event.values[2]) + ";" Float.toString(event.values[2])
); );
} }
return;
} }
if(event.sensor.getType() == Sensor.TYPE_HEART_RATE) { else if(event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
// inform listeners
if (listener != null){ if (listener != null){
listener.onData(SensorType.HEART_RATE.ordinal(), listener.onData(SensorType.HEART_RATE,
Float.toString(event.values[0]) + ";" Float.toString(event.values[0])
); );
} }
return;
} }
if(event.sensor.getType() == Sensor.TYPE_LIGHT) { else if(event.sensor.getType() == Sensor.TYPE_LIGHT) {
// inform listeners
if (listener != null){ if (listener != null){
listener.onData(SensorType.LIGHT.ordinal(), listener.onData(SensorType.LIGHT,
Float.toString(event.values[0]) + ";" Float.toString(event.values[0])
); );
} }
return;
} }
if(event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) { else if(event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
// inform listeners
if (listener != null){ if (listener != null){
listener.onData(SensorType.AMBIENT_TEMPERATURE.ordinal(), listener.onData(SensorType.AMBIENT_TEMPERATURE,
Float.toString(event.values[0]) + ";" Float.toString(event.values[0])
); );
} }
return;
} }
if(event.sensor.getType() == Sensor.TYPE_RELATIVE_HUMIDITY) { else if(event.sensor.getType() == Sensor.TYPE_RELATIVE_HUMIDITY) {
// inform listeners
if (listener != null){ if (listener != null){
listener.onData(SensorType.RELATIVE_HUMIDITY.ordinal(), listener.onData(SensorType.RELATIVE_HUMIDITY,
Float.toString(event.values[0]) + ";" Float.toString(event.values[0])
); );
} }
return;
} }
if(event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) { else if(event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
// inform listeners
if (listener != null){ if (listener != null){
if(event.values.length > 3){ if(event.values.length > 3){
listener.onData(SensorType.ROTATION_VECTOR.ordinal(), listener.onData(SensorType.ROTATION_VECTOR,
Float.toString(event.values[0]) + ";" + Float.toString(event.values[0]) + ";" +
Float.toString(event.values[1]) + ";" + Float.toString(event.values[1]) + ";" +
Float.toString(event.values[2]) + ";" + Float.toString(event.values[2]) + ";" +
Float.toString(event.values[3]) + ";" Float.toString(event.values[3])
); );
} else { } else {
listener.onData(SensorType.ROTATION_VECTOR.ordinal(), listener.onData(SensorType.ROTATION_VECTOR,
Float.toString(event.values[0]) + ";" + Float.toString(event.values[0]) + ";" +
Float.toString(event.values[1]) + ";" + Float.toString(event.values[1]) + ";" +
Float.toString(event.values[2]) + ";" Float.toString(event.values[2])
); );
} }
} }
return;
} }
if(event.sensor.getType() == Sensor.TYPE_GYROSCOPE) { else if(event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
// skip?
//if (!limitGyro.isOK()) {return;}
// inform listeners
if (listener != null){ if (listener != null){
listener.onData(SensorType.GYROSCOPE.ordinal(), listener.onData(SensorType.GYROSCOPE,
Float.toString(event.values[0]) + ";" +
Float.toString(event.values[1]) + ";" +
Float.toString(event.values[2])
);
}
}
else if(event.sensor.getType() == Sensor.TYPE_PRESSURE) {
// inform listeners
if (listener != null){
listener.onData(SensorType.PRESSURE,
Float.toString(event.values[0])
);
}
}
else if(event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
// inform listeners
if (listener != null){
listener.onData(SensorType.LINEAR_ACCELERATION,
Float.toString(event.values[0]) + ";" + Float.toString(event.values[0]) + ";" +
Float.toString(event.values[1]) + ";" + Float.toString(event.values[1]) + ";" +
Float.toString(event.values[2]) + ";" Float.toString(event.values[2]) + ";"
); );
} }
return;
} }
if(event.sensor.getType() == Sensor.TYPE_PRESSURE) { else if(event.sensor.getType() == Sensor.TYPE_GRAVITY) {
if (listener != null){
listener.onData(SensorType.PRESSURE.ordinal(),
Float.toString(event.values[0]) + ";"
);
}
return;
}
if(event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
if (listener != null){
listener.onData(SensorType.LINEAR_ACCELERATION.ordinal(),
Float.toString(event.values[0]) + ";" +
Float.toString(event.values[1]) + ";" +
Float.toString(event.values[2]) + ";"
);
}
return;
}
if(event.sensor.getType() == Sensor.TYPE_GRAVITY) {
float gravity_X = event.values[0];
float gravity_Y = event.values[1];
float gravity_Z = event.values[2];
// inform listeners
if (listener != null){ if (listener != null){
listener.onData(SensorType.GRAVITY.ordinal(), listener.onData(SensorType.GRAVITY,
Float.toString(gravity_X) + ";" + Float.toString(event.values[0]) + ";" +
Float.toString(gravity_Y) + ";" + Float.toString(event.values[1]) + ";" +
Float.toString(gravity_Z) + ";" Float.toString(event.values[2])
); );
} }
} }
else if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// inform listeners
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// skip?
//if (!limitAcc.isOK()) {return;}
System.arraycopy(event.values, 0, mGravity, 0, 3);
if (listener != null){ if (listener != null){
listener.onData(SensorType.ACCELEROMETER.ordinal(), listener.onData(SensorType.ACCELEROMETER,
Float.toString(event.values[0]) + ";" + Float.toString(event.values[0]) + ";" +
Float.toString(event.values[1]) + ";" + Float.toString(event.values[1]) + ";" +
Float.toString(event.values[2]) + ";" Float.toString(event.values[2])
); );
} }
// keep a local copy (needed for orientation matrix)
System.arraycopy(event.values, 0, mGravity, 0, 3);
updateOrientation(); updateOrientation();
} }
if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) { else if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
// skip?
//if (!limitMag.isOK()) {return;}
System.arraycopy(event.values, 0, mGeomagnetic, 0, 3);
// inform listeners
if (listener != null){ if (listener != null){
listener.onData(SensorType.MAGNETIC_FIELD.ordinal(), listener.onData(SensorType.MAGNETIC_FIELD,
Float.toString(event.values[0]) + ";" + Float.toString(event.values[0]) + ";" +
Float.toString(event.values[1]) + ";" + Float.toString(event.values[1]) + ";" +
Float.toString(event.values[2]) + ";" Float.toString(event.values[2])
); );
} }
// keep a local copy (needed for orientation matrix)
System.arraycopy(event.values, 0, mGeomagnetic, 0, 3);
updateOrientation(); updateOrientation();
} }
@@ -330,34 +306,33 @@ public class PhoneSensors extends mySensor implements SensorEventListener{
/** calculate orientation from acc and mag */ /** calculate orientation from acc and mag */
private void updateOrientation() { private void updateOrientation() {
// skip? // skip orientation update if either grav or geo is missing
if (!limitAOri.isOK()) {return;}
if (mGravity == null) {return;} if (mGravity == null) {return;}
if (mGeomagnetic == null) {return;} if (mGeomagnetic == null) {return;}
//rotationMatrix and orientation // calculate rotationMatrix and orientation
float R[] = new float[16]; float R[] = new float[16];
float I[] = new float[16]; float I[] = new float[16];
// derive rotation matrix from grav and geo sensors
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (!success) {return;} if (!success) {return;}
// derive orientation-vector using the rotation matrix
float orientation[] = new float[3]; float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation); SensorManager.getOrientation(R, orientation);
if (listener != null){ // inform listeners
if (listener != null) {
listener.onData(SensorType.ORIENTATION_NEW.ordinal(), listener.onData(SensorType.ORIENTATION_NEW,
Float.toString(orientation[0]) + ";" + Float.toString(orientation[0]) + ";" +
Float.toString(orientation[1]) + ";" + Float.toString(orientation[1]) + ";" +
Float.toString(orientation[2]) + ";" Float.toString(orientation[2])
); );
//Write the whole rotationMatrix R into the Listener. //Write the whole rotationMatrix R into the Listener.
listener.onData(SensorType.ROTATION_MATRIX.ordinal(), listener.onData(SensorType.ROTATION_MATRIX,
Float.toString(R[0]) + ";" + Float.toString(R[0]) + ";" +
Float.toString(R[1]) + ";" + Float.toString(R[1]) + ";" +
Float.toString(R[2]) + ";" + Float.toString(R[2]) + ";" +
@@ -366,53 +341,59 @@ public class PhoneSensors extends mySensor implements SensorEventListener{
Float.toString(R[5]) + ";" + Float.toString(R[5]) + ";" +
Float.toString(R[6]) + ";" + Float.toString(R[6]) + ";" +
Float.toString(R[7]) + ";" + Float.toString(R[7]) + ";" +
Float.toString(R[8]) + ";" + Float.toString(R[8])
Float.toString(R[9]) + ";" + // Float.toString(R[9]) + ";" +
Float.toString(R[10]) + ";" + // Float.toString(R[10]) + ";" +
Float.toString(R[11]) + ";" + // Float.toString(R[11]) + ";" +
Float.toString(R[12]) + ";" + // Float.toString(R[12]) + ";" +
Float.toString(R[13]) + ";" + // Float.toString(R[13]) + ";" +
Float.toString(R[14]) + ";" + // Float.toString(R[14]) + ";" +
Float.toString(R[15]) + ";" // Float.toString(R[15])
); );
} }
} }
@Override @Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { public void onAccuracyChanged(Sensor sensor, int accuracy) {
// nothing to-do here
}
@Override
public void onResume(final Activity act) {
// attach as listener to each of the available sensors
registerIfPresent(acc, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(grav, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(gyro, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(lin_acc, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(magnet, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(press, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(ori, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(heart, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(humidity, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(rotationVector, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(light, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(temperature, SensorManager.SENSOR_DELAY_FASTEST);
} }
private void applyLowPassFilter(float[] input, float[] output) { private void registerIfPresent(final Sensor sens, final int delay) {
float alpha = 0.90f; if (sens != null) {
for (int i = 0; i < input.length; ++i) { sensorManager.registerListener(this, sens, delay);
//output[i] = (output[i] * (alpha)) + (input[i] * (1-alpha)); Log.d("PhoneSensors", "added sensor " + sens.toString());
output[i] = input[i]; // disabled } else {
Log.d("PhoneSensors", "sensor not present. skipping");
} }
} }
@Override @Override
public void onResume(Activity act) { public void onPause(final Activity act) {
// Register a listener for the sensor.
sensorManager.registerListener(this, acc, SensorManager.SENSOR_DELAY_FASTEST); // detach from all events
sensorManager.registerListener(this, grav, SensorManager.SENSOR_DELAY_FASTEST); sensorManager.unregisterListener(this);
sensorManager.registerListener(this, gyro, SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(this, lin_acc, SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(this, magnet, SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(this, press, SensorManager.SENSOR_DELAY_FASTEST); // speed OK
sensorManager.registerListener(this, ori, SensorManager.SENSOR_DELAY_FASTEST); // speed OK
sensorManager.unregisterListener(this, heart);
sensorManager.registerListener(this, heart, SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(this, humidity, SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(this, rotationVector, SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(this, light, SensorManager.SENSOR_DELAY_FASTEST);
sensorManager.registerListener(this, temperature, SensorManager.SENSOR_DELAY_FASTEST);
} }
@Override
public void onPause(Activity act) {
sensorManager.unregisterListener(this);
}
} }

View File

@@ -4,20 +4,35 @@ package de.fhws.indoor.sensorreadout.sensors;
* Created by toni on 02/06/16. * Created by toni on 02/06/16.
*/ */
public enum SensorType { public enum SensorType {
ACCELEROMETER, // 0
GRAVITY, // 1 ACCELEROMETER(0),
LINEAR_ACCELERATION, // 2 GRAVITY(1),
GYROSCOPE, // 3 LINEAR_ACCELERATION(2),
MAGNETIC_FIELD, // 4 GYROSCOPE(3),
PRESSURE, // 5 MAGNETIC_FIELD(4),
ORIENTATION_NEW, // 6 PRESSURE(5),
ROTATION_MATRIX, // 7 ORIENTATION_NEW(6),
WIFI, // 8 ROTATION_MATRIX(7),
IBEACON, // 9 WIFI(8),
RELATIVE_HUMIDITY, // 10 IBEACON(9),
ORIENTATION_OLD, // 11 RELATIVE_HUMIDITY(10),
ROTATION_VECTOR, // 12 ORIENTATION_OLD(11),
LIGHT, // 13 ROTATION_VECTOR(12),
AMBIENT_TEMPERATURE, // 14 LIGHT(13),
HEART_RATE // 15 AMBIENT_TEMPERATURE(14),
HEART_RATE(15),
GROUND_TRUTH(99),
GROUND_TRUTH_PATH(-1),
;
private int id;
SensorType(final int id) {
this.id = id;
}
public final int id() {return id;}
} }

View File

@@ -33,9 +33,9 @@ public class WiFi extends mySensor {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
final List<ScanResult> res = wifi.getScanResults(); final List<ScanResult> res = wifi.getScanResults();
for(final ScanResult sr : res) { for(final ScanResult sr : res) {
sb.append(sr.BSSID).append(";"); sb.append(Helper.stripMAC(sr.BSSID)).append(";");
sb.append(sr.frequency).append(";"); sb.append(sr.frequency).append(";");
sb.append(sr.level).append(";"); sb.append(sr.level);
} }
startScan(); startScan();
if (listener != null && isReceiverRegistered) {listener.onData(sb.toString());} if (listener != null && isReceiverRegistered) {listener.onData(sb.toString());}
@@ -45,14 +45,18 @@ public class WiFi extends mySensor {
} }
/** exception-safe scanning start */
private void startScan() { private void startScan() {
try { try {
//Method startScanActiveMethod = wifi.getClass().getMethod("startScanActive"); //Method startScanActiveMethod = wifi.getClass().getMethod("startScanActive");
//startScanActiveMethod.invoke(wifi); //startScanActiveMethod.invoke(wifi);
wifi.startScan(); wifi.startScan();
Log.e("start", "wifi"); Log.d("wifi", "start scan");
}catch (final Exception e) { }catch (final Exception e) {
throw new RuntimeException(e);} throw new RuntimeException(e);
}
} }
@Override @Override
@@ -69,8 +73,8 @@ public class WiFi extends mySensor {
if (isReceiverRegistered) { if (isReceiverRegistered) {
try { try {
act.unregisterReceiver(this.receiver); act.unregisterReceiver(this.receiver);
} catch (Exception e) { } catch (final Exception e) {
// Do nothing e.printStackTrace();
} }
isReceiverRegistered = false; isReceiverRegistered = false;
} }

View File

@@ -15,8 +15,8 @@ import android.widget.Toast;
*/ */
public class iBeacon extends mySensor { public class iBeacon extends mySensor {
private final BluetoothAdapter bt; private BluetoothAdapter bt = null;
private final BluetoothLeScanner scanner; private BluetoothLeScanner scanner = null;
private static final int REQUEST_ENABLE_BT = 1; private static final int REQUEST_ENABLE_BT = 1;
private ScanCallback mLeScanCallback; private ScanCallback mLeScanCallback;
@@ -26,6 +26,7 @@ public class iBeacon extends mySensor {
// sanity check // sanity check
if (!act.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { if (!act.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(act, "Bluetooth-LE not supported!", Toast.LENGTH_SHORT).show(); Toast.makeText(act, "Bluetooth-LE not supported!", Toast.LENGTH_SHORT).show();
return;
} }
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to // Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
@@ -36,33 +37,41 @@ public class iBeacon extends mySensor {
// bluetooth supported? // bluetooth supported?
if (bt == null) { if (bt == null) {
Toast.makeText(act, "Bluetooth not supported!", Toast.LENGTH_SHORT).show(); Toast.makeText(act, "Bluetooth not supported!", Toast.LENGTH_SHORT).show();
return;
} }
// create the scanner
scanner = bt.getBluetoothLeScanner(); scanner = bt.getBluetoothLeScanner();
// and attach the callback
mLeScanCallback = new ScanCallback() { mLeScanCallback = new ScanCallback() {
@Override public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) { @Override public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) {
//Log.d("BT", device + " " + rssi); //Log.d("BT", device + " " + rssi);
if (listener != null) {listener.onData(result.getDevice().getAddress() + ";" + result.getRssi());} if (listener != null) {
listener.onData(Helper.stripMAC(result.getDevice().getAddress()) + ";" + result.getRssi() + ";" + result.getScanRecord().getTxPowerLevel());
}
} }
}; };
} }
void enableBT(final Activity act) { private final void enableBT(final Activity act) {
if (!bt.isEnabled()) { if (bt == null) {throw new RuntimeException("BT not supported!");}
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
act.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); act.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
} }
@Override public void onResume(final Activity act) { @Override public void onResume(final Activity act) {
enableBT(act); if (bt != null) {
scanner.startScan(mLeScanCallback); enableBT(act);
scanner.startScan(mLeScanCallback);
}
} }
@Override public void onPause(final Activity act) { @Override public void onPause(final Activity act) {
scanner.stopScan(mLeScanCallback); if (bt != null) {
scanner.stopScan(mLeScanCallback);
}
} }
} }

View File

@@ -16,7 +16,7 @@ import android.widget.Toast;
public class iBeaconOld extends mySensor { public class iBeaconOld extends mySensor {
private final BluetoothAdapter bt; private BluetoothAdapter bt = null;
private static final int REQUEST_ENABLE_BT = 1; private static final int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter.LeScanCallback mLeScanCallback; private BluetoothAdapter.LeScanCallback mLeScanCallback;
@@ -26,6 +26,7 @@ public class iBeaconOld extends mySensor {
// sanity check // sanity check
if (!act.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { if (!act.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(act, "Bluetooth-LE not supported!", Toast.LENGTH_SHORT).show(); Toast.makeText(act, "Bluetooth-LE not supported!", Toast.LENGTH_SHORT).show();
return;
} }
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to // Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
@@ -36,12 +37,16 @@ public class iBeaconOld extends mySensor {
// bluetooth supported? // bluetooth supported?
if (bt == null) { if (bt == null) {
Toast.makeText(act, "Bluetooth not supported!", Toast.LENGTH_SHORT).show(); Toast.makeText(act, "Bluetooth not supported!", Toast.LENGTH_SHORT).show();
return;
} }
// attach scan callback
mLeScanCallback = new BluetoothAdapter.LeScanCallback() { mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
//Log.d("BT", device + " " + rssi); //Log.d("BT", device + " " + rssi);
if (listener != null) {listener.onData(device + ";" + rssi);} if (listener != null) {
listener.onData(Helper.stripMAC(device.getAddress()) + ";" + rssi);
}
} }
}; };

View File

@@ -3,21 +3,33 @@ package de.fhws.indoor.sensorreadout.sensors;
import android.app.Activity; import android.app.Activity;
/** /**
* base-class for all Sensors
*
* Created by Frank on 25.03.2015. * Created by Frank on 25.03.2015.
*/ */
public abstract class mySensor { public abstract class mySensor {
/** listen for sensor events */
public interface SensorListener { public interface SensorListener {
public void onData(final String csv); public void onData(final String csv);
public void onData(int id, final String csv);
/** received data from the given sensor */
public void onData(final SensorType id, final String csv);
} }
/** the listener to inform (if any) */
protected SensorListener listener = null; protected SensorListener listener = null;
/** start the sensor */
public abstract void onResume(final Activity act); public abstract void onResume(final Activity act);
/** stop the sensor */
public abstract void onPause(final Activity act); public abstract void onPause(final Activity act);
/** attach the given listener to the sensor */
public void setListener(final SensorListener listener) {this.listener = listener;} public void setListener(final SensorListener listener) {this.listener = listener;}
} }

View File

@@ -18,62 +18,55 @@ tools:context="de.fhws.indoor.sensorreadout.MainActivity">
tools:context=".MainActivity" tools:context=".MainActivity"
android:background="#333"> android:background="#333">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="50dp" <RelativeLayout
android:layout_height="wrap_content" android:layout_width="match_parent"
android:orientation="vertical"> android:layout_height="match_parent">
<Button <Button
android:layout_width="50dp" android:layout_width="50dp"
android:layout_height="wrap_content" android:layout_height="48dp"
android:text="stop" android:text="stop"
android:id="@+id/btnStop" android:id="@+id/btnStop"
android:background="@drawable/btnstopcolor"/> android:background="@drawable/btnstopcolor"/>
<Button <Button
android:layout_width="50dp" android:layout_width="50dp"
android:layout_height="wrap_content" android:layout_height="48dp"
android:layout_below="@+id/btnStop"
android:text="start" android:text="start"
android:id="@+id/btnStart" android:id="@+id/btnStart"
android:background="@drawable/btnstartcolor"/> android:background="@drawable/btnstartcolor"/>
</LinearLayout>
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<Spinner <Spinner
android:layout_width="58dip" android:layout_width="68dip"
android:layout_height="48dip" android:layout_height="48dip"
android:layout_toRightOf="@+id/btnStop"
android:id="@+id/pathspinner" android:id="@+id/pathspinner"
android:background="#64bbe5" android:background="#64bbe5"
/> />
<Spinner <Spinner
android:layout_width="58dip" android:layout_width="68dp"
android:layout_height="48dip" android:layout_height="48dp"
android:layout_below="@+id/pathspinner"
android:layout_toRightOf="@id/btnStart"
android:id="@+id/groundspinner" android:id="@+id/groundspinner"
android:background="#64bbe5" android:background="#64bbe5"
android:layout_alignBottom="@+id/btnGround" /> />
</LinearLayout> <Button
android:layout_width="fill_parent"
android:layout_height="96dip"
android:layout_toRightOf="@+id/pathspinner"
android:text="Ground Truth"
android:id="@+id/btnGround"
android:layout_gravity="right"
android:background="#64bbe5"
/>
<Button </RelativeLayout>
android:layout_width="fill_parent"
android:layout_height="96dip"
android:text="Ground Truth"
android:id="@+id/btnGround"
android:layout_gravity="right"
android:background="#64bbe5"
/>
</LinearLayout>
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout

View File

@@ -5,7 +5,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:2.1.0' classpath 'com.android.tools.build:gradle:2.2.0-alpha2'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files