This commit is contained in:
toni
2017-10-10 16:25:26 +02:00
7 changed files with 325 additions and 4 deletions

View File

@@ -23,6 +23,7 @@ dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.android.gms:play-services:10.2.0'
//compile 'com.google.android.support:wearable:1.3.0'
//compile 'com.google.android.gms:play-services-wearable:8.4.0'
//provided 'com.google.android.wearable:wearable:1.0.0'

View File

@@ -3,6 +3,9 @@
package="de.fhws.indoor.sensorreadout">>
<!--<uses-feature android:name="android.hardware.type.watch" /> -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
@@ -11,6 +14,9 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BODY_SENSORS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" />
<application android:allowBackup="true" android:icon="@drawable/icon"

View File

@@ -25,6 +25,8 @@ import android.view.ViewGroup.LayoutParams;
import java.util.ArrayList;
import java.util.List;
import de.fhws.indoor.sensorreadout.sensors.Gps;
import de.fhws.indoor.sensorreadout.sensors.GpsNew;
import de.fhws.indoor.sensorreadout.sensors.GroundTruth;
import de.fhws.indoor.sensorreadout.sensors.Logger;
import de.fhws.indoor.sensorreadout.sensors.PhoneSensors;
@@ -185,6 +187,14 @@ public class MainActivity extends Activity {
@Override public void onData(final SensorType id, final String csv) {return; }
});
//log gps using sensor number 16
final GpsNew gps = new GpsNew(this);
sensors.add(gps);
gps.setListener(new mySensor.SensorListener(){
@Override public void onData(final String csv) { add(SensorType.GPS, csv); }
@Override public void onData(final SensorType id, final String csv) {return; }
});
// log iBeacons using sensor number 9
final mySensor beacon;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
@@ -209,7 +219,6 @@ public class MainActivity extends Activity {
MY_PERMISSIONS_REQUEST_READ_BT);
}
// heartbeat permission
/* if(ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.BODY_SENSORS)) {
@@ -235,13 +244,15 @@ public class MainActivity extends Activity {
private void stop() {
for (final mySensor s : sensors) {s.onPause(this);}
logger.stop();
((TextView) findViewById(R.id.txtWifi)).setText("");
((TextView) findViewById(R.id.txtBeacon)).setText("");
((TextView) findViewById(R.id.txtWifi)).setText("-");
((TextView) findViewById(R.id.txtBeacon)).setText("-");
((TextView) findViewById(R.id.txtGPS)).setText("-");
}
/** new sensor data */
private int loadCounterWifi = 0;
private int loadCounterBeacon = 0;
private int loadCounterGPS = 0;
private int loadCounterAny = 0;
private void add(final SensorType id, final String csv) {
@@ -256,7 +267,9 @@ public class MainActivity extends Activity {
} else if (id == SensorType.IBEACON){
final TextView txt = (TextView) findViewById(R.id.txtBeacon);
txt.setText( ((++loadCounterBeacon % 2) == 0) ? "ib" : "IB");
}
} else if (id == SensorType.GPS){
final TextView txt = (TextView) findViewById(R.id.txtGPS);
txt.setText( ((++loadCounterGPS % 2) == 0) ? "gps" : "GPS");}
// dump buffer stats every x entries
if (++loadCounterAny % 250 == 0) {

View File

@@ -0,0 +1,111 @@
package de.fhws.indoor.sensorreadout.sensors;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
/**
* Created by student on 20.03.17.
*/
@TargetApi(23)
public class Gps extends mySensor implements LocationListener {
private Activity act;
private LocationManager locationManager;
private Location location;
public Gps(Activity act) {
this.act = act;
initGPS();
}
private void initGPS(){
if ( Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission( act, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( act, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return ;
}
try {
this.locationManager = (LocationManager) act.getSystemService(Context.LOCATION_SERVICE);
// Get GPS
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
//get the most accurate provider
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_HIGH);
String provider = locationManager.getBestProvider(criteria, true);
//use only gps and not network 0 and 0 for fastest updates possible
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0,
0, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
setMostRecentLocation(location);
}
}
} catch (Exception ex) {
throw new MyException("error creating gps!");
}
}
private void setMostRecentLocation(Location loc){
this.location = loc;
}
@Override
public void onResume(Activity act) {
initGPS();
}
@Override
public void onPause(Activity act) {
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
this.location = location;
// inform listeners
if (listener != null){
listener.onData(SensorType.GRAVITY,
Double.toString(location.getLatitude()) + ";" +
Double.toString(location.getLongitude()) + ";" +
Double.toString(location.getAltitude()) + ";" +
Double.toString(location.getBearing())
);
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}

View File

@@ -0,0 +1,181 @@
package de.fhws.indoor.sensorreadout.sensors;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import java.text.DateFormat;
import java.util.Date;
import de.fhws.indoor.sensorreadout.R;
import static android.content.ContentValues.TAG;
/**
* Created by student on 21.03.17.
*/
public class GpsNew extends mySensor implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
protected GoogleApiClient mGoogleApiClient;
protected Location mLastLocation;
private Activity act;
private Location mCurrentLocation;
private LocationRequest mLocationRequest;
protected Boolean mRequestingLocationUpdates;
protected String mLastUpdateTime;
public GpsNew(Activity act) {
this.act = act;
this.mRequestingLocationUpdates = false;
this.mLastUpdateTime = "";
buildGoogleApiClient();
}
/**
* Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API.
*/
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this.act)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
mRequestingLocationUpdates = true;
mGoogleApiClient.connect();
}
/**
* Sets up the location request.
*/
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1);
mLocationRequest.setFastestInterval(1);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
/**
* Requests location updates from the FusedLocationApi.
*/
protected void startLocationUpdates() {
// The final argument to {@code requestLocationUpdates()} is a LocationListener
// (http://developer.android.com/reference/com/google/android/gms/location/LocationListener.html).
if (ActivityCompat.checkSelfPermission(this.act, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this.act, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
/**
* Removes location updates from the FusedLocationApi.
*/
protected void stopLocationUpdates() {
// It is a good practice to remove location requests when the activity is in a paused or
// stopped state. Doing so helps battery performance and is especially
// recommended in applications that request frequent location updates.
// The final argument to {@code requestLocationUpdates()} is a LocationListener
// (http://developer.android.com/reference/com/google/android/gms/location/LocationListener.html).
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
@Override
public void onResume(Activity act) {
if (mGoogleApiClient.isConnected() && mRequestingLocationUpdates) {
startLocationUpdates();
}
}
@Override
public void onPause(Activity act) {
if (mGoogleApiClient.isConnected()) {
stopLocationUpdates();
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.i(TAG, "Connected to GoogleApiClient");
if (this.mCurrentLocation == null) {
if (ActivityCompat.checkSelfPermission(this.act, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this.act, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
this.mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
}
if (mRequestingLocationUpdates) {
startLocationUpdates();
}
}
@Override
public void onConnectionSuspended(int i) {
// The connection to Google Play services was lost for some reason. We call connect() to
// attempt to re-establish the connection.
Log.i(TAG, "Connection suspended");
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// Refer to the javadoc for ConnectionResult to see what error codes might be returned in
// onConnectionFailed.
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}
@Override
public void onLocationChanged(Location location) {
this.mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
// inform listeners
if (listener != null){
listener.onData(SensorType.GRAVITY,
Double.toString(location.getLatitude()) + ";" +
Double.toString(location.getLongitude()) + ";" +
Double.toString(location.getAltitude()) + ";" +
Double.toString(location.getBearing())
);
}
}
}

View File

@@ -21,6 +21,7 @@ public enum SensorType {
LIGHT(13),
AMBIENT_TEMPERATURE(14),
HEART_RATE(15),
GPS(16),
GROUND_TRUTH(99),
GROUND_TRUTH_PATH(-1),

View File

@@ -81,6 +81,14 @@
android:layout_height="wrap_content"
android:text="-"
/>
<TextView
android:id="@+id/txtGPS"
android:textColor="#ffffff"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
/>
<TextView
android:id="@+id/txtBuffer"
android:textColor="#ffffff"