advanced sensors in series 40 java me apps

28
Attila Csipa [@achipa] Technology Wizard, Nokia 1 © 2012 Nokia Advanced examples of sensor usage in Java ME on Series 40 v1.2 February 19, 2013 Attila Csipa ADVANCED EXAMPLES OF SENSOR USAGE IN JAVA ME ON SERIES 40

Upload: microsoft-mobile-developer

Post on 19-May-2015

2.183 views

Category:

Technology


0 download

DESCRIPTION

This webinar will present advanced examples of how to use sensors embedded in Nokia Asha phones in your Java™ ME apps and games. We’ll cover the structure and usage of the Mobile Sensor API (JSR-256). Attila Csipa, technology wizard at Nokia in Tampere, Finland, will give you a short overview that will be followed by plenty of examples and live coding demos. He’ll use the Nokia IDE for Java™ ME (Eclipse-based) to show you how to build your code. He’ll also share best practices and user-experience recommendations based on the most common use cases. We suggest you review the slides from Attila’s introductory session before attending this advanced webinar. ( http://www.slideshare.net/nokia-developer/using-sensors-in-java-me-apps-on-series-40 ) You’ll get a lot out of the webinar even if you haven’t reviewed the earlier material, but you’ll learn more if you’ve done so.

TRANSCRIPT

Page 1: Advanced sensors in Series 40 Java ME apps

Attila Csipa [@achipa] Technology Wizard, Nokia

1 © 2012 Nokia Advanced examples of sensor usage in Java ME on Series 40 v1.2 February 19, 2013 Attila Csipa

ADVANCED EXAMPLES OF SENSOR USAGE IN JAVA ME ON SERIES 40

Page 2: Advanced sensors in Series 40 Java ME apps

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

Page 3: Advanced sensors in Series 40 Java ME apps

PLATFORMS

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

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

API Differences: bit.ly/S40Apis

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

Page 4: Advanced sensors in Series 40 Java ME apps

DP 2.0 – NEW APIS

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

Full touch UI

Virtual Keyboard

Multipoint Touch APIs

Gestures: Pinch

Sensors & Orientation

...

Page 5: Advanced sensors in Series 40 Java ME apps

5 © 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.

Page 6: Advanced sensors in Series 40 Java ME apps

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

Page 7: Advanced sensors in Series 40 Java ME apps

SENSORS: JSR-256

• 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

JSR page at Nokia Developer JSR docs

Page 8: Advanced sensors in Series 40 Java ME apps

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

Page 9: Advanced sensors in Series 40 Java ME apps

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 (language/culture independent)

• Ideal for games!

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

Page 10: Advanced sensors in Series 40 Java ME apps

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

Page 11: Advanced sensors in Series 40 Java ME apps

SENSORS: API AVAILABILITY

• No System property for the API version? – Check Class availability – ClassNotFoundException? → API not supported

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

// 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) { }

Example: Racer, aMaze

Page 12: Advanced sensors in Series 40 Java ME apps

SENSORS: DYNAMIC DISCOVERY

• Sensors can be discovered dynamically!

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

private List sensorsList; private SensorInfo[] sensorInfos; public SensorBrowser() { display = Display.getDisplay(this); sensorsList = new List("Sensors list:", List.EXCLUSIVE); sensorInfos = SensorManager.findSensors(null, null); for (int i = 0; i < sensorInfos.length; i++) { sensorsList .append(constructVisibleSensorName(sensorInfos[i]), null); } sensorsList.addCommand(CMD_LIST_DETAIL); sensorsList.addCommand(CMD_EXIT); sensorsList.setCommandListener(this); }

Example: SensorBrowser

Page 13: Advanced sensors in Series 40 Java ME apps

SENSORS: CAN SAY WHO/WHAT THEY ARE

• A wealth of inspectable information available

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

SensorInfo sensorInfo = sensorInfos[currentSensorId]; sensorDetail = new Form("Sensor detail: " + constructVisibleSensorName(sensorInfo)); sensorDetail.append(new StringItem("Quantity:", sensorInfo .getQuantity(), StringItem.LAYOUT_LEFT)); sensorDetail.append(new StringItem("Context type:", sensorInfo .getContextType(), StringItem.LAYOUT_LEFT)); sensorDetail.append(new StringItem("Connection type:", connectionTypeToString(sensorInfo.getConnectionType()))); sensorDetail.append(new StringItem("URL:", sensorInfo.getUrl(), StringItem.LAYOUT_LEFT)); String[] properties = sensorInfo.getPropertyNames(); for (int i = 0; i < properties.length; i++) { Object value = sensorInfo.getProperty(properties[i]); sensorDetail.append(new StringItem(properties[i] + ":", value .toString(), StringItem.LAYOUT_LEFT)); }

Example: SensorBrowser

Page 14: Advanced sensors in Series 40 Java ME apps

SENSORS: USING THEM – ROLL YOUR OWN

• Establish sensor connection

• Check data in game loop

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

// 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()); } }

// 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

Example: MovingBall

Page 15: Advanced sensors in Series 40 Java ME apps

SENSORS: USING THEM – COPY WITH PRIDE

• Use the wrapping classes from Nokia Developer examples

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

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; } }

Page 16: Advanced sensors in Series 40 Java ME apps

SENSORS: IT‘S A CONDITION (INTERFACE)

• Operators – OP_EQUALS – OP_GREATER_THAN – OP_GREATER_THAN_OR_EQUALS – OP_LESS_THAN – OP_LESS_THAN_OR_EQUALS

• Three Conditions and isMet() – LimitCondition – a value and an operator – RangeCondition – two operators, two values – ObjectCondition – compares to Objects (Strings, etc)

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

temperature.addCondition(listener, new LimitCondition(50,Condition.GREATER_THAN));

Page 17: Advanced sensors in Series 40 Java ME apps

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

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

Page 18: Advanced sensors in Series 40 Java ME apps

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?)

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

Page 19: Advanced sensors in Series 40 Java ME apps

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

COMPATIBILITY

Page 20: Advanced sensors in Series 40 Java ME apps

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

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

Page 21: Advanced sensors in Series 40 Java ME apps

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)

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

Page 22: Advanced sensors in Series 40 Java ME apps

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

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

Page 23: Advanced sensors in Series 40 Java ME apps

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

RESOURCES & TIPS

Page 24: Advanced sensors in Series 40 Java ME apps

NOKIA IDE FOR JAVA ME

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

Device SDK Manager

Integrated SDK + Toolchain

App Templates

JAD Editor

Page 25: Advanced sensors in Series 40 Java ME apps

CODE EXAMPLES • Nokia IDE

– Nokia Hub → Nokia Series 40 Code Examples

• Online – bit.ly/JavaMeExamples

• Emulator – Help → MIDlet Samples

• Maps & LWUIT – C:\Nokia\devices\Nokia_SDK_2_0_Java\plugins

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

Page 26: Advanced sensors in Series 40 Java ME apps

CODE EXAMPLES SHOWN TODAY

• Wiki • aMaze • Racer • Cottage360 (episode 19)

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

Page 27: Advanced sensors in Series 40 Java ME apps

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/

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

Page 28: Advanced sensors in Series 40 Java ME apps

Want to learn more? www.developer.nokia.com Attila Csipa [@achipa] Technology Wizard, Nokia

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

THANK YOU! QUESTIONS?