Showing posts with label SensorManager. Show all posts
Showing posts with label SensorManager. Show all posts

How to Set ProgressBar Progress on Light Sensor Change

API Level 3 or greater needed.


1. Set the Upper Range of a ProgressBar to the Maximum Range of a Light Sensor named myProgressBar.

2. Determine if Sensor Light Changed

3. In the onSensorChanged method, add the following line after the float myEventValues is created. This convert the float value of myEventValues to an integer and set it to the Progress on the myProgressBar.

myProgressBar.setProgress((int)myEventValues);

4. Compile and run!

Resources:

How to Register a SensorEventListener

public boolean registerListener (SensorEventListener listener, Sensor sensor, int rateUs) was added in API level 3
Registers a SensorEventListener for the given sensor.
Note: Don't use this method with a one shot trigger sensor such as TYPE_SIGNIFICANT_MOTION. Use requestTriggerSensor(TriggerEventListener, Sensor) instead.
Parameters:
listener - A SensorEventListener object.
sensor - The Sensor to register to.
rateUs - The rate sensor events are delivered at. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST or, the desired delay between events in microseconds. Specifying the delay in microseconds only works from Android 2.3 (API level 9) onwards. For earlier releases, you must use one of theSENSOR_DELAY_* constants.
Returns: true if the sensor is supported and successfully enabled.

1. Determine if Android Device has Light Sensor, using SensorManager with SensorManager named mySensorManager, a Sensor named mySensor. 

2. Add a SensorEventListener named mySensorEventListener

3. Add the line below to the onCreate method. This will register the SensorEventListener named mySensorEventListener to the Sensor named mySensor, at a rate of Normal Delay.

mySensorManager.registerListener(mySensorEventListener, mySensor,  SensorManager.SENSOR_DELAY_NORMAL);

4. Compile and run!

5. You can use the below options to change the sensor delay.

SENSOR_DEALY_NORMAL - rate (default) suitable for screen orientation changes
SENSOR_DELAY_UI - rate suitable for the user interface
SENSOR_DELAY_GAMES - rate suitable for games
SENSOR_DELAY_FASTEST - get sensor data as fast as possible

Resources:http://developer.android.com/reference/android/hardware/SensorManager.html#registerListener(android.hardware.SensorEventListener, android.hardware.Sensor, int)

How to Determine if Android Device has Light Sensor, using SensorManager

Needs API Level 3, or greater.
For API Level 7 and above you may use, How to Determine if Android Device has a Light Sensor, using PackageManager.

1. Get the Default Light Sensor with a Sensor named mySensor.

2. Add the below code to the onCreate method. This will test the mySensor to determine if light sensor is available. It will display a toast as determined.

if (mySensor == null){
Toast.makeText(this,  "No Light Sensor Available", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,  "Light Sensor Available", Toast.LENGTH_LONG).show();
}

3. Compile and run!

Resources:

How to Get the Default Light Sensor

public Sensor getDefaultSensor (int type) was added in API level 3
Use this method to get the default sensor for a given type. Note that the returned sensor could be a composite sensor, and its data could be averaged or filtered. If you need to access the raw sensors use getSensorList.
Parameters: type - of sensors requested
Returns: the default sensors matching the asked type.

1. Declare a Sensor object named mySensor.

2. Get a Reference to SensorManager named mySensorManager.

3. Save Sensor TYPE_LIGHT to integer named intMySensor.

4. Add the below line to the onCreate method. This will get the default light sensor and save it to mySensor.

mySensor = mySensorManager.getDefaultSensor(intMySensor);

5. Compile and run!

Resources:
http://developer.android.com/reference/android/hardware/SensorManager.html#getDefaultSensor(int)
http://android-coding.blogspot.com/2011/10/using-android-device-build-in-light.html

How to Get a Reference to SensorManager

public abstract Object getSystemService (String name) was added in API level 1
Return the handle to a system-level service by name. The class of the returned object varies by the requested name.

1. In the MainActivity.java file, Get the Context.SENSOR_SERVICE and save to a string named svcName.

2. In the onCreate method, Declare a SensorManager named mySensorManager.

3. In the onCreate method, add the following line. This will get a reference to the SensorManager saved to mySensorManager.

mySensorManager = (SensorManager)getSystemService(svcName);

4. Compile and run!

Next Recommended Article:

Resources:
Professional Android 4 Application Development by Reto Meier , pg 482
http://developer.android.com/reference/android/content/Context.html#getSystemService(java.lang.String)

How to Declare a SensorManager

SensorManager was added in API Level 1
SensorManager lets you access the device's sensors

1. In the MainActivity.java file, add the below line to the import section.

import android.hardware.SensorManager;

2. Add the below line to the onCreate method.