Programming Java 2 Micro Editionon Symbian OS A developer’s guide to MIDP 2.0 Martin de Jode With Jonathan Allin, Darren Holland, Alan Newman and Colin Turfus... Programming Java 2 Micro
Trang 2Programming Java 2 Micro Edition
on Symbian OS
A developer’s guide to MIDP 2.0
Martin de Jode
With
Jonathan Allin, Darren Holland, Alan Newman
and Colin Turfus
Trang 4Programming Java 2 Micro Edition
on Symbian OS
Trang 6Programming Java 2 Micro Edition
on Symbian OS
A developer’s guide to MIDP 2.0
Martin de Jode
With
Jonathan Allin, Darren Holland, Alan Newman
and Colin Turfus
Trang 7Copyright 2004 Symbian Ltd
Published by John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester,
West Sussex PO19 8SQ, England
Telephone ( +44) 1243 779777
Email (for orders and customer service enquiries): cs-books@wiley.co.uk
Visit our Home Page on www.wileyeurope.com or www.wiley.com
All Rights Reserved No part of this publication may be reproduced, stored in a retrieval system
or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except under the terms of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London W1T 4LP, UK, without the permission in writing of the Publisher, with the exception of any material supplied specifically for the purpose of being entered and executed on
a computer system for exclusive use by the purchase of the publication Requests to the Publisher should be addressed to the Permissions Department, John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex PO19 8SQ, England, or emailed to
Other Wiley Editorial Offices
John Wiley & Sons Inc., 111 River Street, Hoboken, NJ 07030, USA
Jossey-Bass, 989 Market Street, San Francisco, CA 94103-1741, USA
Wiley-VCH Verlag GmbH, Boschstr 12, D-69469 Weinheim, Germany
John Wiley & Sons Australia Ltd, 33 Park Road, Milton, Queensland 4064, Australia
John Wiley & Sons (Asia) Pte Ltd, 2 Clementi Loop #02-01, Jin Xing Distripark, Singapore 129809 John Wiley & Sons Canada Ltd, 22 Worcester Road, Etobicoke, Ontario,
Canada M9W 1L1
Wiley also publishes its books in a variety of electronic formats Some content that
appears in print may not be available in electronic books.
Library of Congress Cataloging-in-Publication Data
Jode, Martin de.
Programming the Java 2 micro edition for symbian OS: a developer’s guide to MIDP 2.0/ Martin de Jode [et al.].
British Library Cataloguing in Publication Data
A catalogue record for this book is available from the British Library
ISBN 0-470-09223-8
Typeset in 10/12pt Optima by Laserwords Private Limited, Chennai, India
Printed and bound in Great Britain by Biddles Ltd, King’s Lynn
This book is printed on acid-free paper responsibly manufactured from sustainable
forestry in which at least two trees are planted for each one used for paper production.
Trang 8Contents
Trang 9vi CONTENTS
4 Java APIs for Bluetooth Wireless Technology 205
Section 2: Writing Quality Code
Trang 108 The Market, the Opportunities
Appendix 3: Using the Wireless Toolkit Tools
Appendix 4: Developer Resources and Bibliography 439 Appendix 5: Specifications of Symbian OS Phones 445
Trang 12About This Book
In 2001, Symbian’s first book devoted to Java on Symbian OS waspublished Jonathan Allin’s Wireless Java for Symbian Devices (WJSD)provided an in-depth exposition targeted at programming PersonalJava
on Symbian OS The embedded Java story has moved on a lot in twoyears and so has Symbian’s implementation, so once again we decided
to put pen to paper to produce a new book aimed at helping developersprogram Java on the latest generation of Symbian OS phones
This book is not intended to supersede Jonathan Allin’s WJSD, whichdealt very thoroughly with Symbian’s PersonalJava implementation andstill remains the definitive guide for developers programming PersonalJava
on Symbian devices such as the Nokia 9200 or Sony Ericsson P800 andP900 Instead, this new book covers very different territory, focusing onprogramming MIDP, particularly MIDP 2.0, on Symbian OS
Symbian’s Java implementation has evolved over the years from aJDK 1.1.4-based implementation in Symbian OS Version 5.0, throughPersonalJava on Symbian OS Version 6.0 and is now moving, withSymbian OS Version 7.0 and subsequent releases, to a single Java 2 MicroEdition (J2ME) CLDC/MIDP-based implementation The latest generation
of Symbian OS phones support MIDP 2.0 plus a range of additional,optional APIs, all conforming to Java Specification Requests (JSRs) arrived
at through the Java Community Process
Phones based on the latest releases of Symbian OS, such as theNokia 6600 and Sony Ericsson P900, support MIDP 2.0 as well asimplementations of the Wireless Messaging API (JSR 120), Java BluetoothAPI (JSR 82) and also, in the case of the Nokia 6600, the Mobile MediaAPI (JSR 135)
This book is not just about MIDP 2.0 Instead we will show developershow to get the best out of the latest generation of Symbian OS phones,
by providing a practical, in-depth, guide to programming J2ME on thesedevices In addition to a thorough discussion of MIDP we have alsoincluded an in-depth exposition of all the optional J2ME APIs that can befound on phones such as the Sony Ericsson P900 and Nokia 6600
Trang 13x ABOUT THIS BOOK
Our approach has been to illustrate the new MIDP 2.0 features andoptional APIs by way of concrete examples tested on real devices
In addition to extensive sample code we include a chapter of casestudies that develop more or less complete applications By adopting thisapproach developers will be equipped with code that they know willrun on real devices Where specifications allow optional functionality weindicate whether this is supported on Symbian phones We also pointout known bugs and possible work-arounds In addition we aim to usethe considerable experience available within Symbian to show the readerhow to write efficient and effective code for constrained devices Tocomplete the picture we also discuss what Java has to offer in the wirelessspace and how it may enrich the wireless value chain We also provide
an insight into how Java is likely to evolve on Symbian OS in the future
In writing this book, our desire has been to give enough information inone volume for developers to make the most of the Java 2 Micro Edition
on Symbian OS, enabling them to provide the compelling content thatwill enrich the wireless ecosystem
The book is divided three sections:
• Section 1: J2ME and MIDP
• Section 2: Writing Quality Code for Smartphones
• Section 3: The Evolution of the Wireless Java Market
In Section 1 we introduce the Java 2 Micro Edition and the ideas behindconfigurations and profiles We then concentrate on programming MIDPand the additional APIs that make up the Java platform on the latestgeneration of Symbian OS phones
Section 2 investigates design and implementation considerations volved in writing high-quality code, focusing on the issues of portabilityand efficiency
in-The final section looks at the strategic importance of Java to the wirelessecosystem and provides a glimpse as to how Wireless Java may evolve
on Symbian OS
Who Is This Book For?
The book is aimed at Java developers already programming in thewireless space or interested in moving into the wireless space and whowish to know what can be achieved with J2ME on the latest Symbian
OS phones Enough introductory information and examples are providedfor newcomers to J2ME to get going with MIDP programming, while thethorough treatment of the new MIDP 2.0 and optional APIs provides moreweighty fare for the experienced MIDP programmer
Trang 14ABOUT THIS BOOK xi
Conventions
To help you get the most from the text and keep track of what’s happening,we’ve used a number of simple conventions throughout this book.When we refer to words you use in your code, such as classes,attributes and methods, or to the name of a file, we use this style:Personclass: we obtain the name attribute by invoking the getNamemethod on our Person instance
When we list code, or the contents of files, we use the ing convention:
follow-SocketConnection conn = (follow-SocketConnection)Connector.open(url);
DataOutputStream out = conn.openDataOutputStream();
byte[] buf= request.getBytes();
out.write(buf);
out.flush();
out.close();
We show commands typed at the command line like this:
C: \WTK20\apps\Example\src>javac -d tmpclasses -bootclasspath %MIDPAPI%
-classpath %J2MECLASSPATH% *.java
URLs are written:www.symbian.com/developer
Trang 16to model the interaction of light with biological tissue using Fortran.Martin joined Symbian in 2000, after completing an MSc in ObjectOriented Software Systems from City University As a Java DeveloperConsultant in Symbian’s Developer Network he spends his time providingsupport to Symbian’s Java developer community In addition to writingtechnical papers on PersonalJava and J2ME for Symbian’s website, Martinhas developed numerous utility and sample applications showing how
to make the most of Symbian’s Java platform Other activities includedelivering training, evangelizing Java on Symbian OS and trying to keep
up with the proliferation of J2ME JSRs
Outside of work, Martin is passionate about cricket, having spent whatseems like a lifetime playing competitive club cricket in and around thehome counties
Jonathan Allin
Jonathan is Symbian’s Product Manager for Java Technology His role
is to ensure that Symbian OS provides a first class Java platform formobile phones, and covers Symbian’s Java strategy and implementationroadmap, partnerships, and, of course, how Java relates to other devel-opment environments Jonathan was the lead author of Wireless Javafor Symbian Devices, authored the ‘‘Developing with Java’’ chapter in
Trang 17xiv AUTHOR BIOGRAPHIES
Professional Symbian Programming, and presents regularly on wirelessJava opportunities and optimizing Java code for mobile devices
Jonathan has a BSc in Electronics and a DSc in Biomedical Engineering
He picked up an MBA when working for Acorn Computers, where hehelped develop computers and software for schools Prior to joiningSymbian in 1999, he worked for Origin BV for three years, where hefirst became interested in Java and particularly the role it can play withinthe enterprise
Jonathan is married to Lauren, who is a social worker and essential forkeeping in order their three children: Benjamin, Daniel, and Victoria, whoare into rugby, music, and hockey respectively Java reminds Jonathanthat computing can be fun, interesting and useful
Darren Holland
Darren joined Symbian in 2002 He graduated in 1995 with a BSc
in Computing Systems Technology and started his career developingtelephone billing and enterprise fax software in C++ before starting towork with Java in 1999
Darren would like to thank the IS department of Symbian who vided support throughout the development of the Expense applicationprototype, ensuring that the resources and infrastructure required were inplace More importantly, they supported the project remit and suppliedthe encouragement that ultimately ensured success In particular OliviaHawkins, Belen Ares Paredes and Tarek Meliti contributed greatly.Darren would also like to thank his wife Sol`ene for her continualsupport and for helping to keep him sane during life’s more stressfulmoments No matter how much Darren enjoys work he would alwaysrather be sailing!
on to the Commodore Pet, and BBC B Micro
After graduating with a business degree, he began working in the NHS
as an analyst, automating many previously manual data entry tasks forhis department He then moved into banking and programmed tradeentry and loan collateral management systems on the trade floor ofthe Republic National Bank of New York where, in 1998, he took akeen interest in learning Java and its interaction with the Internet He
Trang 18AUTHOR BIOGRAPHIES xv
then spent a year with Internet sports magazine Sportal.com, beforesetting up his own company, Sensible Development, which created andnow runs a multiplayer football manager game, which can be found at
www.effeffelle.com
He is an advocate of plain speaking as a means of preventing thatglazed-over look consumers often show when confronted with technol-ogy He also believes that technology should not dictate but insteadenhance consumer and business tasks
Colin Turfus
Colin graduated from the University of Dundee, Scotland with a BSc(Hons) in maths and physics, and from the University of Cambridgewith a PhD in applied mathematics He has researched and lectured atuniversities in the UK and in South Korea, publishing papers in fluiddynamics and computational astrophysics He became interested in Javaprogramming while developing intranet-based maths teaching resourcesand lecturing about Internet technology in South Korea
He joined Symbian shortly after its inception in the summer of 1998,and has been involved since in establishing and building the SymbianDeveloper Network, which he now heads He was a contributing author
toWireless Java for Symbian Devices
Colin’s interests include jogging, hill-walking and classical guitar He
is married to Keum-ye from South Korea They have three girls: Selina,Sonya and Emily
Trang 20Author’s Acknowledgements
First and foremost I would like to thank my co-authors Alan Newman,Jonathan Allin, Colin Turfus and Darren Holland without whose help wewould not have been able to create this book
I must also thank Phil Northam of Symbian Press, whose initial ideathis book was and who lobbied hard for its realization Also thanks to hisassistant Freddie Gjertsen for his painstaking work in ensuring consistency
in the style and grammar of our work and who kept us all on the straightand narrow
I’m also very indebted to the reviewers from Symbian’s Java ing team: Hana Bisada, Roy Hayun, Simon Lewis, Michael Aubert, IvanLitovski and George Sewell, the real experts, who develop Symbian’sJava implementation They have all spent considerable time ensuring thecorrectness and quality of the author’s contributions
Engineer-I’m grateful to the guys at Rococo Software for providing us with theirImpronto Simulator for JSR 82, and in particular to Steven Crane forsuggesting numerous improvements to Chapter 5 I’d also like to thankJarmo Lahtinen and Janne Levula from Nokia for their advice on aspects
of the MIDP implementation running on the Nokia 6600
I would also like to extend my thanks to Gaynor Redvers-Mutton whohas ably managed the publication of the book at John Wiley
Last, but far from least, I must mention the Symbian work placementstudents Xi Chen and Sunny Khaila, who provided much of the ground-work for this book As well as exploring the MIDP 2.0 specification, theyalso produced early prototypes of several of the example applicationsfeatured in the text
Trang 22Symbian Press Acknowledgements
Symbian licenses, develops and supports Symbian OS, the platform fornext-generation data-enabled mobile phones Symbian is headquartered
in London, with offices worldwide For more information see the bian website, www.symbian.com ’Symbian’, ’Symbian OS’ and otherassociated Symbian marks are all trademarks of Symbian Ltd Symbianacknowledges the trademark rights of all third parties referred to inthis material
Sym-Thanks to all who have had input into this book, including the manywhose behind-the-scenes work ensured the book was delivered on time!Let’s not forget the Laughing Gravy and the Stage Door either .
About the cover
The cover concept, designed by Jonathan Tastard, was inspired by DavidLevin, CEO of Symbian, in a 2003 keynote presentation at Exposium,where he pronounced:
The mobile phone has traditionally connected the mouth to the ear.Symbian develops Symbian OS to enable a new generation of connectedcommunications devices to connect the mouth to the ear to the eye
To realize this vision, the mobile phone industry is working together todevelop the latest technologies, support open industry standards, and ensureinteroperability between advanced mobile phones as networks evolve from2.5G to 3G
Trang 24Tim Lindholm, Architect of the J2ME platform at Sun Microsystems, Inc.
The rate of adoption of the Java platform in wireless devices is dented, but more important is the change in perspective that theadoption reflects
unprece-The desktop and server have relatively long histories as open platformsand, as such, have evolved developer communities and vibrant marketsfor third-party software In contrast, until recently, wireless devices were
as closed as the legendary mainframes of decades past; the only opers of software for the early mobile phones were hidden away in thelaboratories of the large companies who made those phones
devel-Coupled with advances in the raw computational capabilities of thedevices themselves, the development and adoption of the Java 2 MicroEdition (J2ME) platform has changed all that Within the last few years,wireless devices have emerged as a new, open, networked computingplatform deployed on a massive scale Its effectiveness has been in largepart facilitated by the availability of a standard software architecture,
Trang 25This book focuses on MIDP programming of Symbian OS phones Farfrom just reciting a litany of API descriptions, it uses example applications
to make practical points It digs into the details that are relevant to goodapplication design and getting good performance As well as coveringMIDP 2.0 programming, the book also covers programming for MIDP1.0, the platform in many already-deployed devices Finally, the bookexplores many of the standard J2ME optional packages that Symbian
OS currently supports, or will support in the near future The examplesare developed completely, through to their installation and execution onreal devices
Although presented in the context of Symbian OS, the worldwideavailability of MIDP on wireless devices means that the lessons of thisbook are not tied to any particular operating system This book shouldappeal to all developers who want to take better advantage of the wirelessJ2ME platform