Android Sensors

2,776 views

Published on

Published in: Technology, Business
  • Be the first to comment

Android Sensors

  1. 1. Android  Sensors   Jussi  Pohjolainen  
  2. 2. Overview   •  SensorManager:  Access  to  sensors  if   hardware  supports   •  SensorEventListener:  Interface  for  class  to   listen  sensor  values  as  they  change  in  =me  
  3. 3. SensorManager •  Access  to  device’s  sensors   •  Reference  by  calling  getSystemService •  Disable  sensors  if  you  don’t  need  them!  (drain   baEery)    
  4. 4. Sensor  Type   •  Accelerometer,  Orienta=on   •  Light   •  Magne=c  field   •  Proximity   •  Temperature   •  …    
  5. 5. Sampling  Rate   •  How  fast  try  to  get  the  sensor  data   •  Reques=ng  specific  sampling  rate..  Hint  to  the   system,  no  guarantees   •  SensorManager  holds  constant  variables:   – Fastest   – Game   – Normal   – User  Interface    
  6. 6. Accuracy   •  How  accurate  is  the  informa=on?   – High   – Low   – Medium   – Unreliable   •  This  is  informa=on  is  sent  to  listener  
  7. 7. SensorManager  Example   public class SensorActivity extends Activity implements SensorEventListener { private SensorManager mSensorManager; private Sensor mAccelerometer; public SensorActivity() { mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) } protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } protected void onPause() { super.onPause(); mSensorManager.unregisteredListener(this); } public void onAccuracyChanged(Sensor sensor, int accuracy) { } public void onSensorChanged(SensorEvent event) { } }
  8. 8. Sensor  Simulator   •  The  current  Android  emulator  does  not   support  sensors   •  Use  Sensor  Simulator,  open  source  project   developed  by  Open  Intents   •  Supports  accelerometer,  compass,   orienta=on,  and  temperature  sensors,   •  Download  it  from:     – hEp://code.google.com/p/openintents/wiki/ SensorSimulator  
  9. 9. Instruc=ons   1.  Download  the  latest  sensorsimulator- x.x.x.zip from  the  download  tab  and  unzip.   2.  Start  bin/sensorsimulator-x.x.x.jar (Java   standalone  applica=on).   1.  java –jar sensorsimulator-x.x.x.jar 3.  Install  bin/SensorSimulatorSettings- x.x.x.apk on  your  Android  emulator.   4.  Launch  SensorSimulator  on  the  emulator.   5.  Enter  IP  address  and  socket  (port)  number  into   the  emulator  
  10. 10. 2.  Start  the  Sensor  Simulator  Jar  
  11. 11. 3.  Install  Sensor  Simulator  Apk  
  12. 12. 4.  Launch  Sensor  Simulator  in   Emulator  
  13. 13. 5.  Configure  App  
  14. 14. 6.  Enable  Connec=on  
  15. 15. Using  Sensor  Simulator  in  Your  App   •  Add  external  JAR  lib/sensorsimulator- lib.x.x.x.jar  to  your  project   •  Use  sensorsimulator  classes   •  Get  reference  to  SensorManagerSimulator   instead  of  SensorManager   •  Connect  to  simulator   •  Add  permission  to  use  internet  
  16. 16. Add  .jar  to  Project  

×