using j2me to create field force applications chris clark, mobilehwy, llc

Post on 11-Jan-2016

223 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Using J2ME to create Field Force

Applications

Using J2ME to create Field Force

Applications

Chris Clark, MobileHWY, LLC

THE BUSINESSTHE BUSINESS

The Mobility BusinessThe Mobility Business

Building wireless applications that people can use everyday to get their job done.

- Using cheap or existing hardware. $200 or less

- Providing access to legacy data in a mobile environment

- Using the Internet to get data to and from the mobile devices. $50/mth or less.

- Leveraging GPS and Bluetooth to make the devices more capable.

What do customers want?What do customers want?

Legacy Integration – Integrating with backend systems to make data available to mobile applications.

GPS –Tracking of people, places and things using GPS coordinates.

Credit Card Processing – Creating mobile POS.

Printing – Bluetooth printing capability for receipts.

Broadcast Messaging – Sending broadcast messages to mobile devices.

Bluetooth Camera – To take pictures

Bluetooth Compass – To not only determine location but direction you are facing.

Mobile Building BlocksMobile Building Blocks

Legacy Integration

- Database access: SQL Server, Oracle, mySQL, Btrieve, Foxpro, ASCII Flat file

- Mainframe screen scraping: Navigating 3270 screens.

- Web screen scraping: Automation of web sites.

- TCP Socket communication to host process.

- VoiceXML: To automate Interactive Voice Response systems.

Mobile Building BlocksMobile Building BlocksGPS – Global Positioning System

- Location tracking: Reading GPS coordinates from mobile device.

- Driving Directions – Get directions from where you are to where you need to go.

- Route Navigation – Use GPS updates from the device to determine if route is being followed. Also can track speed and heading.

- Dynamic mapping of field force – plot all data points for last known location of devices.

- Proximity searching – search for locations near your current position. i.e. Location Based Services (LBS)

Mobile Building BlocksMobile Building Blocks

Credit Card Processing

Using Bluetooth a mobile device can attach to a credit card swipe reader.

All credit cards conform to the same specification for storing credit card information on Track 1.

%/1/B<creditcardnumber>^<lastname>/<firstname>^<expiration year><expiration month><otherdigits>CRLF

If you can read one, you can read them all.

Mobile Building BlocksMobile Building Blocks

Printing

Customers want to print a variety of information.

Printing on a mobile device involves using serial IO to connect to the printer.

Then sending printer Esc codes to the printer to format the information for display.

There are 2 flavors of serial printing.

- Bluetooth printing (uses Bluetooth serial profile)

- Tethered serial cable printing

Mobile Building BlocksMobile Building Blocks

Broadcast Messaging

Customers have a high demand for being able to send out broadcast messages to their field force.

Broadcast messaging uses a queuing server which contacts each device seperately and confirms delivery of the message.

Primarly socket communications to the device’s IP address from a centralized server.

THE TECHNOLOGYTHE TECHNOLOGY

Mobile DevelopmentMobile Development

RULE #1

Using the device simulator is no indication of how the application is going to run on the device.

RULE #2

Development tools are not all the same.

RULE #3

You are breaking new ground. There will be issues that will require trial and failure

Several Things to ConsiderSeveral Things to ConsiderBUILD

- Always build for optimal performance. It’s not a PC.

- There will be code differences needed sometimes for different devices. Some java will not pass bytecode verification on the device.

TEST

- Know your carrier requirements for testing.

- Make sure you test on the device.

DEPLOY

- Find out early how you get your application on the device

Getting Some Build ToolsGetting Some Build Tools

Sun Microsystems – Wireless Toolkit 2.2

http://java.sun.com/products/j2mewtoolkit/

Blackberry Java Development Environment

www.blackberry.com/developers

Motorola iDen developer

http://idenphones.motorola.com/idenDeveloper/

EclipseME – J2ME plugin for Eclipse

http://eclipseme.sourceforge.net/

It all starts with the MIDLETIt all starts with the MIDLETpublic class CreditCardDemo extends MIDlet {

public Display display;

/** * Signals the MIDlet to start and enter the Active state. */ protected void startApp() { display = Display.getDisplay(this); scrnCreditCard CreditCardForm = new scrnCreditCard(this); CreditCardForm.Show(); }

/** * Signals the MIDlet to terminate and enter the Destroyed state. */ protected void destroyApp(boolean unconditional) { notifyDestroyed(); }

/** * Signals the MIDlet to stop and enter the Paused state. */ protected void pauseApp() { /* Do Nothing */ }

public void Show(Displayable d) { this.display.setCurrent(d); }}

Understanding startApp()Understanding startApp()Applications will suspend sometimes because of incoming phone calls.

startApp() will be called again when this happens, so make sure you restore to the proper screen. Don’t assume that every time startApp is called you should login.

Normally, I keep a public variable around called display as part of the midlet class. I initialize display to a value when the application first starts. I check the value and restore to the proper state.

Here is a modified startApp()

protected void startApp() {if(display != null) {

/* Do nothing, will restore the screen they were on */} else {

display = Display.getDisplay(this); scrnCreditCard CreditCardForm = new scrnCreditCard(this); CreditCardForm.Show();

} }

Creating your screensCreating your screensCreating screens are pretty straight forward.

You need a reference to the MIDlet’s display object.

Display display = Display.getDisplay(MIDlet m);

With this display object you can change the screen by calling setCurrent.

display.setCurrent(Displayable display);

Creating your screensCreating your screensThere are 2 Displayable objects

Screen : Displayable

Screen is what you should think about when you are designing form based applications. This is primarily what we use.

Canvas : Displayable

Canvas is more centered toward games or custom controls. With Canvas also comes the ability to read the devices keyboard through events. This is really designed more toward games.

Creating your screensCreating your screensThere are 4 types of objects you can use with setCurrent();

Form : ScreenYou insert Items onto the Form using Form.append or Form.insert

Alert : ScreenAlerts are similar to VB message box. They provide quick notifications as to what the application is doing. As with message box there are different types of Alerts (Alarm, Confirmation, Error, Info, Warning).

TextBox : ScreenA textbox is similar to HTML <textarea>. This is for multi-line text entry.

List : ScreenA list is for displaying a list of choices. Lists can contain images.

Creating your screensCreating your screensThere are several Items you can add to your Form.

ChoiceGroup : ItemSimilar to List except in a Form.

DateField : ItemFor presenting calendar (date and time) information.

Gauge : ItemBar graph

ImageItem : ItemAdd image

StringItem : ItemAdd string

TextField : ItemInput text box with optional formatting constraints.

Processing CommandsProcessing CommandsYour class needs to support the CommandListener interfacepublic class scrnCreditCard implements CommandListener {

You need to define the commandprivate static Command CMD_EXIT = new Command("EXIT", Command.ITEM, 1);

You need to implement the CommandListener interface public void commandAction(Command c, Displayable d) { if (c == CMD_EXIT) { app.destroyApp(true); } }

If you intend to access your Form’s Items within the commandAction, you need to define your Item at the class level.private StringItem progressLabel;

Data StorageData Storagejavax.microedition.rms.RecordStore – Record Management System

J2ME provides a really basic storage capability for storing strings called a RecordStore.

A database is a stretch. Think of being able to store strings in a file and retrieve them based on a record ID.

In practical use, we create ENTITY CLASSES that are capable of going to and from strings.

We create a class with all it’s properties and provide:

- parse() method for retrieving the state of the object from a string.- toString() method for storing the state of the object to a string.

Using this capability, we are able to effectively retrieve objects from the RecordStore and return them to their state.

Data StorageData StorageSELECTpublic void getRecord( int recordId );

INSERTpublic int RecordStore.addRecord( byte[ ] data, int offset, int numBytes ) ;

UPDATEpublic void setRecord( int recordId, byte[ ] newData, int offset, int numBytes );

DELETEpublic void deleteRecord( int recordId );

HTTPHTTPStringBuffer text = new StringBuffer();

HttpConnection c = (HttpConnection) Connector.open(getUrl, Connector.READ_WRITE, true);

c.setRequestMethod(HttpConnection.GET);

int rc = c.getResponseCode();

if (rc == HttpConnection.HTTP_OK) {

InputStream is = c.openDataInputStream();int ch;while((ch = is.read()) != -1) {

text.append((char) ch);}

} else {throw new IOException("HTTP response code: " + rc);

}

String response = text.toString();

Entity ClassesEntity ClassesWe heavily use ENTITY CLASSES for the data operations in the mobile device.

After retrieving data through some method of HTTP.

Create a new instance of your ENTITY CLASS

Set the properties of the ENTITY CLASS from the data you have retrieved through HTTP. A simple method maybe to parse through some XML.

After you have the state of your ENTITY CLASS. Convert this state to a string by calling the toString() method.

Store this string in your RecordStore using RecordStore.add(…);

GPSGPSGPS support is going to be dependant on device. JSR129 or MAYBE NOT

JSR129 EXAMPLE try {

// Lots of Criteria options are available per spec, few supported Criteria myCriteria = new Criteria();

LocationProvider myLocProv = LocationProvider.getInstance(myCriteria);

// If no result could be retrieved, a LocationException is thrown. Location myLocation = myLocProv.getLocation(-1);

// returns a QualifiedCoordinates object which also has accuracy information // inherits from Coordinates object QualifiedCoordinates myCoordinates = myLocation.getQualifiedCoordinates();

// get the Coordinates from the QualifiedCoordinates double myLat = myCoordinates.getLatitude(); double myLng = myCoordinates.getLongitude();}catch (LocationException locex){ // Could not get a fix}

GPS MappingGPS MappingOnce you have the coordinates of the device. Most of the GPS features are implemented using other services.

A dynamic mapping example.Tracking the device may require you to send the coordinates to a centralized server and time and date stamp them into a database. A web based system may retrieve the coordinates and display them to the user on a map. Lot of technologies may be in play here with the mobile device being the most simple of the technologies employed.

Mapping StrategiesAutomation of MapPoint to create custom .NET Web services

Using Microsoft MapPoint Web Services

Using MapQuest Web Services

J2ME ConnectorJ2ME ConnectorWhether you are doing serial printing using a tethered cable to the device or Bluetooth. The process is pretty much the same.

J2ME offers a Connector architecture for communications. Serial communications is just about the same as HTTP except for the ConnectionString.

If you know how to work with Streams in J2ME you can communicate to most anything.

In serial communication, you are primarily dealing with COM0. String connstring = "comm:0;baudrate=19200;parity=n;databits=8;stopbits=1;flowcontrol=s/s";

Bluetooth varies and most connection strings appear to be proprietary based on the hardware. In some cases the connection string is even abstracted through classes as you will see in the next example.

J2ME ConnectorJ2ME ConnectorString connstring = "comm:0;baudrate=19200;parity=n;databits=8;stopbits=1;flowcontrol=s/s";

Bluetooth Serial PrintingBluetooth Serial PrintingBluetoothSerialPortInfo[] info = BluetoothSerialPort.getSerialPortInfo();

// Get first Bluetooth Serial Device registered with this mobile deviceString bluetoothConnectionString = info[0].toString();

StreamConnection sc = (StreamConnection) Connector.open(bluetoothConnectionString, Connector.READ_WRITE);

DataOutputStream outstream = sc.openDataOutputStream();

outstream.write(this.data.getBytes());

outstream.flush();

outstream.close();

sc.close();

top related