Wednesday, November 12, 2014

Coursera Android week 7 : Sensors, and Maps/Location


Sensors  Part 1 Presentation and Slides

Context aware computing
- location, how held, ambient light, how fast traveling with sensors

Sensors - measure physical environment such as motion, position in world or orientation, environment

motion 3 axis accelerometer - forces exerted on device such as shake

position 3 axis magnetic field  - orientation in respect to magnetic field

environment- atmospheric pressure

SensorManager manages sensors

http://developer.android.com/reference/android/hardware/SensorManager.html

instance to sensor manager - getSystemService(Context.SENSOR_SERVICE)

access to specific sensor SensorManager.getDefaultSensor (int type)

Constants:
Sensor.TYPE_ACCELEROMTER, Sensor.TYPE_MAGNETIC_FIELD, Sensor.TYPE_PRESSURE

SensorEventListener - interface callback manager when accuracy changes and new reading

onAccuraccyChanged (Sensor sensor, int accuracy) : accuracy changed

onSensorChanged (SensorEvent event) - new reading

unregister/register with SensorManager using SensorEventListener

- public boolean registerListener ( SensorEventListener listener, Sensor sensor, int rate)
- public void unregisterListener ( SensorEventListener listener, Sensor sensor)

SensorEvent - represents a sensor event
data is sensor specific , sensor type, time stamp, accuracy, measurements data

http://developer.android.com/reference/android/hardware/SensorEvent.html

x right to left
y bottom to top
z down to up

AccelerometerRaw app

// Get reference to SensorManager
SensorManager  mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

// Get reference to Accelerometer
mSensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER)))

mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);

mSensorManager.unregisterListener(this);


Sensors Part 2
 Presentation

Accelerometer values

transforms to smooth out data
low pass filters
- deemphasize small transient force changes
- emphasize constant force changes

such as when hand shakes ie carpenters level

high pass filters
- emphasize  transient force changes
- deemphasize constant force changes

ignore gravity, but specific moves . i.e. how user shakes device

SensorFilteredAccelerometer app


SensorCompass app - accelerometer and magnetometer


// Get a reference to the magnetometer magnetometer = mSensorManager .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);

// Users the accelerometer and magnetometer readings
// to compute the device's rotation with respect to
// a real world coordinate system - Converts device coordinates into world coordinates.
SensorManager.getRotationMatrix(rotationMatrix, null, mGravity, mGeomagnetic);

// Returns the device's orientation given // the rotationMatrix SensorManager.getOrientation(rotationMatrix, orientationMatrix);

http://developer.android.com/reference/android/hardware/SensorManager.html


Location & Maps  Part 1 Presentation and Slides

Location aware
- Find X from users location, Direct user from current location to place Y

Define GEOFence

Location class - position on earth
Location instance consists of:
  • Lattitude -required
  • Longitude -required
  • Timestamp -required
  • Accuracy
  • Altitude
  • Speed 
  • Bearing


http://developer.android.com/reference/android/location/Location.html

Location from LocationProvider and devices have access to multiple Location providers.
- represents a location data source
- actual data may come from GPS,Cell towers, wifi access pts
- may request info from network (wifi/cell), gps (satellite), passive (piggy back on readings from other apps)

Network Provider - determines location based on cell and wifi
- permission is android.permission.ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION
- cheaper, less accurate,faster, availability varies

GPS Provider - determines location based on satellite
- permission is android.permission.ACCESS_FINE_LOCATION
- expensive, accurate, slower, outdoors

Passive Provider returns locations generated by other providers
- permission is android.permission.ACCESS_FINE_LOCATION
- cheapest, fastest, not always available

LocationManager class
- service for location data
getSystemService(Context.LOCATION_SERVICE)
- determine last known user loc
- register for loc updates
- register for intents when moves/leaves a specific geo area

LocationListener interface
-defines callback methods when lo or LocationProvider status changes

void onLocationChanged(Location loc)
void onProviderDisabled(String provider)
void onProviderEnabled(String provider)
void onStatusChanged(provider,status,bundle)

obtain location (first determine last location)
- start listening for updates from location providers
- maintain best estimate of location
- when estimate is good enough, stop listening to updates
- use best estimate

measurement time, accuracy are factors for best location

LocationGetLocation app - last known location

// Acquire reference to the LocationManager
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE)

// Get the last known location from all providers
List<String> matchingProviders = mLocationManager.getAllProviders();

for (String provider : matchingProviders) {
Location location =    mLocationManager.getLastKnownLocation(provider);
...
float accuracy = location.getAccuracy();
long time = location.getTime();

// Register for network location updates 
if (null != mLocationManager .getProvider(LocationManager.NETWORK_PROVIDER)) 

mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, POLLING_FREQ, MIN_DISTANCE, mLocationListener); 


// Register for GPS location updates
if (null != mLocationManager .getProvider(LocationManager.GPS_PROVIDER)) 

mLocationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, POLLING_FREQ, MIN_DISTANCE, mLocationListener);


// Schedule a runnable to unregister location listeners 
Executors.newScheduledThreadPool(1).schedule(new Runnable() 
{ @Override public void run() { 
Log.i(TAG, "location updates cancelled"); 
mLocationManager.removeUpdates(mLocationListener); 
} }, MEASURE_TIME, TimeUnit.MILLISECONDS);


always check last known measurement
return updates infrequently as possible - limit measurement time
use least accurate measurement as necessary
turn off updates in onPause()

Location & Maps  Part 2 Presentation

Location -> visualize real places using Maps

Google Maps Android V2 API

Normal Map - traditional road map
satellite - aerial photographs of area
hybrid map combine
terrain map - topographical details such as elevation
customize map add markers and ground overlays on top of map
map respond to gestures
indicate the user location using special marker

GoogleMap class
MapFragment class
Camera class
Marker class

set up maps :

Google Play services SDK
obtain API key
specify settings in Application Manifest xml file
add Map to project
https://developers.google.com/maps/documentation/android/start

Permissions:
<uses-permission android:name  = "android.permission.INTERNET" />

<uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE" />

- download data
uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE: />

<uses-permission android:name = "com.google.providers.gsf.permission.READ_GSERVICES"/>

also permission is android.permission.ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION
- for location

MapEarthQuakeMap app

remember NetworkingAndroidHttpClientJSON app
- earthquake data in list view


// The Map Object
private GoogleMap mMap;

private final static String URL =
"http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=" + UNAME;

new HttpGetTask().execute(URL);


mMap.addMarker(new MarkerOptions()
// Set the Marker's position
.position(new LatLng(rec.getLat(), rec.getLng()))

// Set the title of the Marker's information window
.title(String.valueOf(rec.getMagnitude()))

// Set the color for the Marker
 .icon(BitmapDescriptorFactory .defaultMarker(getMarkerColor(rec .getMagnitude()))));


// Should compute map center from the actual data mMap.moveCamera(CameraUpdateFactory.newLatLng(
new LatLng( CAMERA_LAT, CAMERA_LNG)));




same data but presented in a map with markers in Map of world, color indicates magnitude

https://developers.google.com/maps/documentation/android/map
GoogleMap.MAP_TYPE_TERRAIN






No comments:

Post a Comment