Hardware

Sensores

  • Dependen del dispositivo
  • Android nos ofrece una forma homogénea para tratar con ellos
  • Usaremos el SensorManager
SensorManager sensorManager = 
                (SensorManager)getSystemService(Context.SENSOR_SERVICE);

Sensores

  • Comprovamos si el dispositivo tiene el sensor
if (sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null)
  • Para usar el sensor se usa el SensorEventListener

SensorEventListener

SensorEventListener sensorList = new SensorEventListener() {

@Override

public void onSensorChanged(SensorEvent event) { 
    //Llevar a cabo la acción 
} 

@Override

public void onAccuracyChanged(Sensor sensor, int accuracy) { 
    // TODO Auto-generated method stub 
} 

}; 

sm.registerListener(sensorList, sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_FASTEST);

SensorEventListener

  • Propiedades del SensorEventListener
    • accuracy: presición del sensor
    • sensor: el sensor que genera el evento
    • timestamp: tiempo en que ha ocurrido
    • values: vector de floats, la longitud depende del sensor
      • el sensor TYPE_MAGNETIC_FIELD devuelve x,y,z
      • TYPE_LIGHT devuelve un unico valo, los lux detectados

Ejemplo

SensorEventListener mySensorListener = new SensorEventListener() { 

@Override

public void onSensorChanged(SensorEvent event) { 
 Double val3 = Double.valueOf(d.format((double) event.values[2])); 
 DecimalFormat d = new DecimalFormat("000"); 
 Double val1 = Double.valueOf(d.format((double) event.values[0])); 
 Double val2 = Double.valueOf(d.format((double) event.values[1])); 
 t1.setText("x:" + val1.toString()); 
 t2.setText("y:" + val2.toString()); 
 t3.setText("z:" + val3.toString());
 float average = (values[0]+values[1]+values[2])/3; 
 float modValue = Math.abs(average); 
 mIndicator.setProgress((int)modValue);
}

GPS

  • Se gestiona mediante LocationManager y Listeners
  • Disponemos de 2 proveedores de localización
    • NETWORK_PROVIDER: Usa el Wifi
    • GPS_PROVIDER: Usa la red GPS

GPS

  • addGPSStatusListener 
    • Con este listenerpodemos gestionar los cambios de estado del GPS (ENABLED/DISABLED).
  • getLastKnowLocation
    • Nos da la última posición conocida del terminal (no tiene por qué ser la actual).
  • requestLocationUpdate
    • Nos avisa con cada cambio de posición.
    • Recibe LocationProvider, tiempo minimo entre updates, distancia minima entre updates y un LocationListener

LocationListener

LocationListener locationListener = new LocationListener() { 

public void onLocationChanged(Location location) { 
//makeUseOfNewLocation(location); 
    Log.v("GPS", ((Double)location.getLatitude()).toString()); 
} 

public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

public void onProviderEnabled(String provider) { 

} 

public void onProviderDisabled(String provider) { 

}

GeoDecoder

  • Nos permite traducir direcciones a coordenadas y viceversa


 

l = gc.getFromLocation(location.getLatitude(),location.getLongitude(), 5);

24 - Hardware

By androidjedi

24 - Hardware

  • 456