• No results found

Android Sensor Programming. Weihong Yu

N/A
N/A
Protected

Academic year: 2021

Share "Android Sensor Programming. Weihong Yu"

Copied!
22
0
0

Loading.... (view fulltext now)

Full text

(1)

Android Sensor Programming

(2)

Sensors Overview

• The Android platform is ideal for creating

innovative applications through the use of

sensors.

• These built-in sensors measure motion,

orientation, and various environmental

conditions.

• These sensors are capable of providing raw

data with high precision and accuracy, and are

useful if you want to monitor three-dimensional

device movement or positioning, or you want to

monitor changes in the ambient environment

(3)

Android Sensors Overview

•Android Sensors:

– MIC – Camera – Temperature – Location (GPS or Network) – Orientation – Accelerometer – Proximity – Pressure – Light

(4)

Sensors

• hardware-based sensors

– physical components built into a handset or tablet device – They derive their data by directly measuring specific

environmental properties, such as acceleration, geomagnetic field strength, or angular change

• software-based sensors

– not physical devices, although they mimic hardware-based sensors

– Software-based sensors derive their data from one or more of the hardware-based sensors and are sometimes called virtual sensors or synthetic sensors

(5)

categories of sensors supported by

Android platform

• Motion sensors :measure acceleration forces and rotational forces along three axes

– accelerometers, gravity sensors, gyroscopes, etc.

• Environmental sensors: measure various

environmental parameters, such as ambient air

temperature and pressure, illumination, and humidity

– barometers, photometers, and thermometers.

• Position sensors: measure the physical position of a device

(6)

Sensor Coordinate System

• When a device is held in its

default orientation

– the X axis is horizontal and points to the right

– the Y axis is vertical and points up

– and the Z axis points toward the outside of the screen face

(7)

Android sensor framework

• The sensor framework provides several classes

and interfaces that help you perform a wide

variety of sensor-related tasks

– Determine which sensors are available on a device. – Determine an individual sensor's capabilities, such

as its maximum range, manufacturer, power requirements, and resolution.

– Acquire raw sensor data and define the minimum rate at which you acquire sensor data.

– Register and unregister sensor event listeners that monitor sensor changes.

(8)

Sensor Framework-sensor Manager

• You can use this class to create an instance of

the sensor service

• This class provides various

methods

for

accessing and listing sensors, registering and

unregistering sensor event listeners, and

acquiring orientation information.

• This class also provides several sensor

constants

that are used to report sensor

accuracy, set data acquisition rates, and

calibrate sensors.

(9)

Sensor Framework---Sensor

• You can use this class to create an

instance of a specific sensor. This

class provides various

methods

that let

you determine a sensor's capabilities.

(10)

Sensor Framework---SensorEvent

• The system uses this class to create a sensor

event object, which provides information about

a sensor event.

• A sensor event object includes the following

information:

– the raw sensor data

– the type of sensor that generated the event – the accuracy of the data

(11)

Sensor Framework---SensorEventListener

• You can use this interface to create two

callback methods that receive notifications

(sensor events) when sensor values change or

when sensor accuracy changes.

– onAccuracyChanged(Sensor sensor, int

accuracy) :Called when the accuracy of a sensor has changed.

– onSensorChanged(SensorEvent event) :Called when sensor values have changed.

(12)

Basic Steps for Programming

• Step 1--access a SensorManager via

getSystemService(SENSOR_SERVICE).

• Android supports several sensors via the SensorManager

• Step 2--access the Sensor via the

sensorManager.getDefaultSensor() method

• Step 3--Once you acquired a sensor, you can register a SensorEventListener object on it. This listener will get informed, if the sensor data changes.

• Step 4--To avoid the unnecessary usage of battery you register your listener in the onResume() method and de-register it in the onPause() method.

(13)
(14)

Example 1:List all sensors

• Step 1--- get a reference to the sensor service

private SensorManager mSensorManager;

mSensorManager = (SensorManager)

getSystemService(Context.SENSOR_SERVI

CE);

• Step2---get a list of all sensors on a device

List<Sensor> deviceSensors =

mSensorManager.getSensorList(Sensor.TYP

E_ALL);

(15)

Example 2:Use Accelerometer

• Step 1---get an instance of SensorManager

and Sensor

mSensorManager = (SensorManager)

getSystemService(Context.SENSOR_SERVIC

E);

mAccelerometer =

mSensorManager.getDefaultSensor(Sensor.T

YPE_ACCELEROMETER);

(16)

Example 2:Use Accelerometer

• Step 2---register a sensor event listener

mSensorManager.registerListener(this,

mAccelerometer,

(17)

Example 2:Use Accelerometer

• Step 3---override

onSensorChanged(SensorEvent event) and

use event.values[] to get the co-ordinates

@Override

public void onSensorChanged(SensorEvent

event) { float x = event.values[0]; float y =

event.values[1]; float z = event.values[2]; }

(18)

Codes for Using Accelerometer

• Get the Sensor Service and Sensor

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.activity_acc_main); mytx=(TextView)this.findViewById(R.id.tx); myty=(TextView)this.findViewById(R.id.ty); mytz=(TextView)this.findViewById(R.id.tz); sm=(SensorManager)this.getSystemService(Context.SE NSOR_SERVICE); mysensor=sm.getDefaultSensor(Sensor.TYPE_ACCELE ROMETER); }

(19)

Codes for Using Accelerometer

• Define an instance of SensorEventListener

private final SensorEventListener mysensorlistenr=new

SensorEventListener(){

public void onAccuracyChanged(Sensor sensor, int accuracy) {

// TODO Auto-generated method stub }

public void onSensorChanged(SensorEvent event) {

// TODO Auto-generated method stub

mytx.setText(String.valueOf(event.values[0])); myty.setText(String.valueOf(event.values[1])); mytz.setText(String.valueOf(event.values[2])); } };

(20)

Codes for Using Accelerometer

• Register the object of

SensorEventListener

protected void onResume() {

// TODO Auto-generated method stub

super.onResume();

sm.registerListener(mysensorlistenr, mysensor,

SensorManager.SENSOR_DELAY_NORMAL);

}

(21)

Codes for Using Accelerometer

• De-Register the object of

SensorEventListener

protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

sm.unregisterListener(mysensorlistenr);

}

(22)

Running result on the Emulator

But you currently can't test all sensor code on the

emulator because the emulator cannot emulate all sensors. Most of the

time,you must test your sensor code on a physical

References

Related documents

Cloud computing, as defined by NIST, is a model for enabling always-on, convenient, on-demand network access to a shared pool of configurable computing resources (e.g.,

Dementia: Course and Preventive Strategies Normal Aging Subjective Memory Compalints Mild Cognitive Impairment Dementia Clinical State Secondary Intervention Treatment Primary

Unemployment and deepening the economic crisis are the main adverse effects of austerity measures, namely cutting spending, adopted by the Government.. He

El Rocío puede presentarse como un símbolo de reivindicación cultural local, pero de hecho va más allá, ya que el conjunto de peregrinos y visitantes pueden llegar a constituir

In order to allow for a reuse of task aspects across different domains, we propose the model depicted in Figure 2 to decompose the scope introduced by the underlying applications

A security may be released when the obligations to import associated with the import licence have been fulfilled and the used licence, plus any extracts, is returned to

Despite playing fast and loose with historicity, Transparent captures some of the fundamental truths of queer history: that the lives of people whose bodies and desires do

OGM frames as input and the position of target car in the next frame as output. To update the positions of the target and surrounding vehicles in the observation sequence after