diff --git a/app/build.gradle b/app/build.gradle index 3c0d7ed..792fcd1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e9f7c56..42540b0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,9 @@ package="de.fhws.indoor.sensorreadout">> + + + @@ -11,6 +14,9 @@ + + + = 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) { diff --git a/app/src/main/java/de/fhws/indoor/sensorreadout/sensors/Gps.java b/app/src/main/java/de/fhws/indoor/sensorreadout/sensors/Gps.java new file mode 100644 index 0000000..eec513f --- /dev/null +++ b/app/src/main/java/de/fhws/indoor/sensorreadout/sensors/Gps.java @@ -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) { + + } +} diff --git a/app/src/main/java/de/fhws/indoor/sensorreadout/sensors/GpsNew.java b/app/src/main/java/de/fhws/indoor/sensorreadout/sensors/GpsNew.java new file mode 100644 index 0000000..f969c03 --- /dev/null +++ b/app/src/main/java/de/fhws/indoor/sensorreadout/sensors/GpsNew.java @@ -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()) + ); + } + + } +} diff --git a/app/src/main/java/de/fhws/indoor/sensorreadout/sensors/SensorType.java b/app/src/main/java/de/fhws/indoor/sensorreadout/sensors/SensorType.java index 114d641..7a21578 100644 --- a/app/src/main/java/de/fhws/indoor/sensorreadout/sensors/SensorType.java +++ b/app/src/main/java/de/fhws/indoor/sensorreadout/sensors/SensorType.java @@ -21,6 +21,7 @@ public enum SensorType { LIGHT(13), AMBIENT_TEMPERATURE(14), HEART_RATE(15), + GPS(16), GROUND_TRUTH(99), GROUND_TRUTH_PATH(-1), diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6625edf..d8630ed 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -81,6 +81,14 @@ android:layout_height="wrap_content" android:text="-" /> + +