Sensors in Android (old)

958 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
958
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
97
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sensors in Android (old)

  1. 1. Android Application Development Sensor Programming Ahsanul Karim ahsanul.karim@sentinelbd.com Sentinel Solutions Ltd. http://www.sentinelbd.com
  2. 2. SensorProgrammingAndroidSensors phones support different sensors.1. Android2. Today we write test code that reports which sensors our Android device supports.3. Unfortunately, the emulator supports none upto SDK 1.5.4. But we’ll see how many sensors are supported in real device5. We’ll create a project named SensorTest for this. We’ll test following sensor list: 1. Accelerometer 2. Light 3. Magnetic Field 4. Orientation 5. Orientation Raw 6. Proximity 7. Temperature 8. Tricorder This is for SDK
  3. 3. SensorProgrammingAndroid SensorsTest We create a project named SensorTest
  4. 4. SensorProgrammingAndroid SensorsTest change our Activity to a 1. We ListActivity
  5. 5. SensorProgrammingAndroid SensorsTest use SensorManager class 2. We’ll 3. So we declare a List for SensorInfo 4. SensorInfo is a class defined by us which holds the sensor name and a boolean value whether its supported or not. 5. So we create a SensorInfo class
  6. 6. SensorProgrammingAndroid SensorsTest get back to our 6. Let’s ListActivity 7. We’ll now populate the list with all sensors and check whether they are enabled
  7. 7. SensorProgrammingAndroid SensorsTest 8. We declare a List of SensorInfo 9. Now we check each sensor one by one: First Accelerometer 10. Now we check each sensor one by one: then Light
  8. 8. SensorProgrammingAndroid SensorsTestNow we check each sensor one by 11. one: First Magnetic field 12. Now we check each sensor one by one: then Orientation
  9. 9. SensorProgrammingAndroid SensorsTestNow we check each sensor one by 13. one: First Orientation Raw 14. Now we check each sensor one by one: then Proximity
  10. 10. SensorProgrammingAndroid SensorsTestNow we check each sensor one by 15. one: First Temperature 16. Now we check each sensor one by one: then Tricorder
  11. 11. SensorProgrammingAndroid SensorsTestTo show the list we do the following by setting 17. adapterNow we run the app in emulator.Except Accelerometer we see all sensors aredisabledMay be in later SDKs we’ll get more enabledsensors
  12. 12. SensorProgrammingAndroid SensorsTest run this in Galaxy Lets tab We see the following are enabled: 1. Accelerometer 2. Light 3. Magnetic Field 4. Orientation 5. Orientation Raw 6. Proximity So the sensor support varies from Vendor to vendor and model to model
  13. 13. SensorProgrammingAndroid SensorProgrammingAs we see that Accelerometer is the 1. most commonly used sensor and also 2. it has many applications in games development, We’ll see and example of Sensor programming with Accelerometer And as we don’t have option to move the emulator we may have to use a sensor simulator which will simulate the sensor data. So we download the library for sensorsimulation at: http://openintents.googlecode.com/files/sensorsimulator-1.1.0-rc1.zip http://code.google.com/p/openintents/downloads/detail?name=sensorsimulator-1.1.0-rc1. zip&can=2&q= http://code.google.com/p/openintents/wiki/SensorSimulator We can follow their instruction to use it. But here, we’ll do it in our real device, so simulator will not be required
  14. 14. SensorProgrammingAndroid SensorProgramming as an Let’s create a project We create a layout with a simple example image which will move according to the Accelerometer:
  15. 15. SensorProgrammingAndroid SensorProgramming Our Activity:
  16. 16. SensorProgrammingAndroid SensorProgramming Adding SensorEventListener inner class with data update
  17. 17. SensorProgrammingAndroid SensorProgramming and registering Declaring variables listener
  18. 18. SensorProgrammingAndroid SensorProgramming Register and unregister listener on activity onStop and onResume:

×