USING ACCELEROMETERS AND OTHER
SENSORS IN JAVA ME ON SERIES 40


Attila Csipa [@achipa]
Technology Wizard, Nokia

 1     © 2012 Nokia Using accelerometers and other sensors in Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
CONTENTS
• Introduction
     – Platforms & Versions
• Sensors
     –   Physical vs virtual
     –   JSR-256
     –   Measurables
     –   Accelerometers
     –   Usage
     –   UI considerations
     –   Performance
     –   App Compatibility
• Resources



 2          © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
PLATFORMS
API Differences: bit.ly/S40Apis




5th Ed., FP1    6th Ed., Lite            6th Ed.              6th Ed., FP1                  DP 1.0                       DP 1.1   Developer Platform 2.0




          http://www.developer.nokia.com/Devices/Device_specifications/Comparison.xhtml?dev=Asha_306,Nokia_Asha_309,Asha_311


 3             © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
DP 2.0 – NEW APIS

         Full touch                                  Sensors &                                        Multipoint
              UI                                     Orientation                                      Touch APIs

         Gestures:                                       Virtual
                                                                                                                       ...
          Pinch                                         Keyboard

4   © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
SENSORS
    Etymology
    Originated 1925–30 from sense + -or.

    Noun
    sensor (plural sensors)
    A device or organ that detects certain external stimuli and responds in a
    distinctive manner.


5         © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
SENSORS: PHYSICAL VS VIRTUAL
• Physical sensors (physical values, g or m/s, mbar, etc)
     – Acceleration
     – Light
     – ...
• Virtual sensors (combined or interpreted values, %, enum-s)
    – Battery level
    – Orientation
    –…
 6      © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
SENSORS: JSR-256
                                                                               JSR page at http://bit.ly/VdZDlG

• JSR 256 Sensor API
    – Generic: designed also for temperature, blood pressure, etc.
    – Support on Series40 from DP2.0 (Asha Full Touch)
    – Also available on Symbian (from S60 5th edition onwards)
• Two packages
        javax.microedition.sensor (read information)
        javax.microedition.sensor.control (settings, start/stop)
7      © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
SENSORS: WHAT TO LOOK FOR
• Currently supported
    – Battery Charge: 0 .. 100, charge percentage
    – Network Field Intensity: 0 .. 100, signal strength
    – Charger State: 0 .. 1, charger connected
    – Acceleration: –2g .. +2g, x / y / z axis
    – Double Tap: 1 .. 63, phone sides
    – Orientation: 0 .. 6, phone orientation


8      © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
SENSORS: ACCELEROMETERS ARE COOL
• Enrich user interaction
    – Doesn’t suffer from finger size limits
    – Doesn’t suffer from screen size limits
    – Doesn’t interfere with what the user sees on the screen
    – Natural interaction
• Ideal for games!


9      © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
SENSORS: UNDERSTANDING ACCELEROMETERS
• Acceleration is CHANGE of the speed vector
• Standstill normalized value is 1.0, why? GRAVITY
• Nitpick - accelerometers measure translation not rotation
• Rotation can still be measured indirectly (we measure
 gravity vector hopping from one axis to the other)



10    © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
Example:
SENSORS: API AVAILABILITY                                                                                                  Racer, aMaze



• No System property for the API version?
     – Check Class availability
     – ClassNotFoundException? → API not supported
       // Virtual keyboard support
       try {
           // Check if class is available
           Class.forName("javamicroedition.sensor.SensorConnection");
           // SensorManager.findSensors("acceleration", null);
           useSensors = true;
       } catch (ClassNotFoundException e) {
           // Class not available: -> no Sensor API support.
           useSensors = false;
       } catch (Exception e) { }



11      © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
Example: MovingBall
SENSORS: USING THEM – ROLL YOUR OWN
• Establish sensor connection
     // Find all acceleration sensors, the contextType is left undefined
     SensorInfo[] sensorInfos = SensorManager.findSensors("acceleration", null);
     // Find an acceleration sensor that returns double values
     for (int i = 0; i < sensorInfos.length; i++) {
         if (sensorInfos[i].getChannelInfos()[0].getDataType() == ChannelInfo.TYPE_DOUBLE) {
             accSensor = (SensorConnection) Connector.open(sensorInfos[i].getUrl());
         }
     }


• Check data in game loop
     // Use   1 as a buffer size to get exactly 1 value for each axis
     Data[]   data = accSensor.getData(1);
     speedX   = -data[0].getDoubleValues()[0]; // data[0] => x-axis
     speedY   = data[1].getDoubleValues()[0];   // data[1] => y-axis




12            © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
SENSORS: USING THEM – COPY WITH PRIDE
• Use the wrapping classes from Nokia Developer examples
     private void enableSensors() {
         if (accelerationProvider != null) {
             accelerationProvider.close();
         }
         accelerationProvider = AccelerationProvider.getProvider(
             new AccelerationProvider.Listener() {
                  public void dataReceived(double ax, double ay, double az) {
                      if (isSensorTurning()) {
                          tilt = (int) ay;
                      }
                 }
             });
         if (accelerationProvider != null) {
             Main.sensorsSupported = true;
         } else {
             Main.sensorsSupported = false;
         }
     }

13          © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
SENSORS: UI CONSIDERATIONS
• Calibrate! (see aMaze example)
• Look for change, not particular values
• Which axis is which (portrait vs landscape)
• Anticipate noise in readings (average values if needed)
• Don’t force accelerometer usage if it doesn’t add to the
 user experience

14    © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
SENSORS: PERFORMANCE
• Filter (LimitCondition) for values you’re really interested in
• Separate from mainloop – use Threads (see Cottage360
 video and source)
• Responsiveness is more critical than with keyboard input
• Choose right frequency (is 100 reads/sec really needed?)



15    © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
COMPATIBILITY




16   © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
COMPATIBILITY? CODE CONSIDERATIONS
• Source & binary compatible
    – xx years old Java ME apps run on
      full touch phones (and vice versa)!
• Downwards compatibility
     – Check API support of target phones
     – Lowest common denominator:
         → Nokia Java SDK 2.0 compiled app
         runs on old phones


17      © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
COMPATIBILITY? USE-CASE CONSIDERATIONS
• Input type
     – Sensors complementing buttons (universal)
         – Discrete input CAN be easier with buttons (f.ex Snake)!
     – Sensors complementing touch controls (touch&type)
         – Most common
     – Accelerometer only (no pre-DP2.0)
         – Rarer than you think, but possible! (f.ex wire loop game)


18      © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
DYNAMIC API USAGE
• Single code base for different phones
     – Code that uses new APIs
         – Externalize to extra class
     – Check API support at runtime
         – Instantiate class if supported
         – Different methods for checking available




19      © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
RESOURCES & TIPS




22   © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
NOKIA IDE FOR JAVA ME
     Integrated SDK + Toolchain                                                                                           JAD Editor




     App Templates                                                                                        Device SDK Manager


23     © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
CODE EXAMPLES
• Nokia IDE
      – Nokia Hub → Nokia Series 40
        Code Examples
• Online
      – bit.ly/JavaMeExamples
• Emulator
      – Help → MIDlet Samples
• Maps & LWUIT
      – C:NokiadevicesNokia_SDK_2_0_Javaplugins



 24       © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
CODE EXAMPLES SHOWN TODAY
• Wiki
• aMaze
• Racer
• Cottage360 (episode 19)




25       © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
GET STARTED
• Overview
     – www.developer.nokia.com/Develop/Java/Getting_started/
• Downloads
     – SDK: www.developer.nokia.com/Develop/Java/
     – LWUIT: projects.developer.nokia.com/LWUIT_for_Series_40
• Guides
     –   Design & User Experience
     –   Porting from Android
     –   www.developer.nokia.com/Develop/Java/Documentation/
     –   Training Videos: www.developer.nokia.com/Develop/Java/Learning/
     –   Code Examples: www.developer.nokia.com/Develop/Java/Code_examples/

27         © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
THANK YOU!                                                      QUESTIONS?
Want to learn more?
www.developer.nokia.com

Attila Csipa [@achipa]
Technology Wizard, Nokia



 28    © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa

Using sensors in java me apps on series 40

  • 1.
    USING ACCELEROMETERS ANDOTHER SENSORS IN JAVA ME ON SERIES 40 Attila Csipa [@achipa] Technology Wizard, Nokia 1 © 2012 Nokia Using accelerometers and other sensors in Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 2.
    CONTENTS • Introduction – Platforms & Versions • Sensors – Physical vs virtual – JSR-256 – Measurables – Accelerometers – Usage – UI considerations – Performance – App Compatibility • Resources 2 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 3.
    PLATFORMS API Differences: bit.ly/S40Apis 5thEd., FP1 6th Ed., Lite 6th Ed. 6th Ed., FP1 DP 1.0 DP 1.1 Developer Platform 2.0 http://www.developer.nokia.com/Devices/Device_specifications/Comparison.xhtml?dev=Asha_306,Nokia_Asha_309,Asha_311 3 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 4.
    DP 2.0 –NEW APIS Full touch Sensors & Multipoint UI Orientation Touch APIs Gestures: Virtual ... Pinch Keyboard 4 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 5.
    SENSORS Etymology Originated 1925–30 from sense + -or. Noun sensor (plural sensors) A device or organ that detects certain external stimuli and responds in a distinctive manner. 5 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 6.
    SENSORS: PHYSICAL VSVIRTUAL • Physical sensors (physical values, g or m/s, mbar, etc) – Acceleration – Light – ... • Virtual sensors (combined or interpreted values, %, enum-s) – Battery level – Orientation –… 6 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 7.
    SENSORS: JSR-256 JSR page at http://bit.ly/VdZDlG • JSR 256 Sensor API – Generic: designed also for temperature, blood pressure, etc. – Support on Series40 from DP2.0 (Asha Full Touch) – Also available on Symbian (from S60 5th edition onwards) • Two packages javax.microedition.sensor (read information) javax.microedition.sensor.control (settings, start/stop) 7 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 8.
    SENSORS: WHAT TOLOOK FOR • Currently supported – Battery Charge: 0 .. 100, charge percentage – Network Field Intensity: 0 .. 100, signal strength – Charger State: 0 .. 1, charger connected – Acceleration: –2g .. +2g, x / y / z axis – Double Tap: 1 .. 63, phone sides – Orientation: 0 .. 6, phone orientation 8 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 9.
    SENSORS: ACCELEROMETERS ARECOOL • Enrich user interaction – Doesn’t suffer from finger size limits – Doesn’t suffer from screen size limits – Doesn’t interfere with what the user sees on the screen – Natural interaction • Ideal for games! 9 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 10.
    SENSORS: UNDERSTANDING ACCELEROMETERS •Acceleration is CHANGE of the speed vector • Standstill normalized value is 1.0, why? GRAVITY • Nitpick - accelerometers measure translation not rotation • Rotation can still be measured indirectly (we measure gravity vector hopping from one axis to the other) 10 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 11.
    Example: SENSORS: API AVAILABILITY Racer, aMaze • No System property for the API version? – Check Class availability – ClassNotFoundException? → API not supported // Virtual keyboard support try { // Check if class is available Class.forName("javamicroedition.sensor.SensorConnection"); // SensorManager.findSensors("acceleration", null); useSensors = true; } catch (ClassNotFoundException e) { // Class not available: -> no Sensor API support. useSensors = false; } catch (Exception e) { } 11 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 12.
    Example: MovingBall SENSORS: USINGTHEM – ROLL YOUR OWN • Establish sensor connection // Find all acceleration sensors, the contextType is left undefined SensorInfo[] sensorInfos = SensorManager.findSensors("acceleration", null); // Find an acceleration sensor that returns double values for (int i = 0; i < sensorInfos.length; i++) { if (sensorInfos[i].getChannelInfos()[0].getDataType() == ChannelInfo.TYPE_DOUBLE) { accSensor = (SensorConnection) Connector.open(sensorInfos[i].getUrl()); } } • Check data in game loop // Use 1 as a buffer size to get exactly 1 value for each axis Data[] data = accSensor.getData(1); speedX = -data[0].getDoubleValues()[0]; // data[0] => x-axis speedY = data[1].getDoubleValues()[0]; // data[1] => y-axis 12 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 13.
    SENSORS: USING THEM– COPY WITH PRIDE • Use the wrapping classes from Nokia Developer examples private void enableSensors() { if (accelerationProvider != null) { accelerationProvider.close(); } accelerationProvider = AccelerationProvider.getProvider( new AccelerationProvider.Listener() { public void dataReceived(double ax, double ay, double az) { if (isSensorTurning()) { tilt = (int) ay; } } }); if (accelerationProvider != null) { Main.sensorsSupported = true; } else { Main.sensorsSupported = false; } } 13 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 14.
    SENSORS: UI CONSIDERATIONS •Calibrate! (see aMaze example) • Look for change, not particular values • Which axis is which (portrait vs landscape) • Anticipate noise in readings (average values if needed) • Don’t force accelerometer usage if it doesn’t add to the user experience 14 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 15.
    SENSORS: PERFORMANCE • Filter(LimitCondition) for values you’re really interested in • Separate from mainloop – use Threads (see Cottage360 video and source) • Responsiveness is more critical than with keyboard input • Choose right frequency (is 100 reads/sec really needed?) 15 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 16.
    COMPATIBILITY 16 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 17.
    COMPATIBILITY? CODE CONSIDERATIONS •Source & binary compatible – xx years old Java ME apps run on full touch phones (and vice versa)! • Downwards compatibility – Check API support of target phones – Lowest common denominator: → Nokia Java SDK 2.0 compiled app runs on old phones 17 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 18.
    COMPATIBILITY? USE-CASE CONSIDERATIONS •Input type – Sensors complementing buttons (universal) – Discrete input CAN be easier with buttons (f.ex Snake)! – Sensors complementing touch controls (touch&type) – Most common – Accelerometer only (no pre-DP2.0) – Rarer than you think, but possible! (f.ex wire loop game) 18 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 19.
    DYNAMIC API USAGE •Single code base for different phones – Code that uses new APIs – Externalize to extra class – Check API support at runtime – Instantiate class if supported – Different methods for checking available 19 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 20.
    RESOURCES & TIPS 22 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 21.
    NOKIA IDE FORJAVA ME Integrated SDK + Toolchain JAD Editor App Templates Device SDK Manager 23 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 22.
    CODE EXAMPLES • NokiaIDE – Nokia Hub → Nokia Series 40 Code Examples • Online – bit.ly/JavaMeExamples • Emulator – Help → MIDlet Samples • Maps & LWUIT – C:NokiadevicesNokia_SDK_2_0_Javaplugins 24 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 23.
    CODE EXAMPLES SHOWNTODAY • Wiki • aMaze • Racer • Cottage360 (episode 19) 25 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 24.
    GET STARTED • Overview – www.developer.nokia.com/Develop/Java/Getting_started/ • Downloads – SDK: www.developer.nokia.com/Develop/Java/ – LWUIT: projects.developer.nokia.com/LWUIT_for_Series_40 • Guides – Design & User Experience – Porting from Android – www.developer.nokia.com/Develop/Java/Documentation/ – Training Videos: www.developer.nokia.com/Develop/Java/Learning/ – Code Examples: www.developer.nokia.com/Develop/Java/Code_examples/ 27 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa
  • 25.
    THANK YOU! QUESTIONS? Want to learn more? www.developer.nokia.com Attila Csipa [@achipa] Technology Wizard, Nokia 28 © 2012 Nokia Using accelerometers and other sensors from Java ME on Series 40 v1.1 October 23, 2012 Attila Csipa