NetBeans Location Based Services Mobility Demo
Contributed and maintained by
Martin Brehovsky and
Brian
Leonard, 23 Jan 2006
This demo showcases the NetBeans 5.5 Mobility Pack's support for JSR
179 location-based services (LBS). Location-based services provide users
of mobile devices personalized services tailored to their current location.
In this demo we will create an application that loads a map based on your current
location (for the purposes of this demo, that location will be Prague, Czech
Republic).
 |
An
icon represents your location on the map and there are icons for nearby
landmarks such as restaurants, shops, theatres and museums. As you walk
through the town, your icon moves across the map. As you approach
landmarks, you can get additional details on that landmark such as its
name, address and phone number as well as update to date information as
the daily specials at the restaurant or vacancy at the hotel. The up to
date information is provided via a web service. For phones that support
SOAP ( JSR 172),
this web service is called directly. Otherwise, the phone uses HTTP to
call a web application which acts as a proxy to the web service.
Throughout this demo you'll also be exposed to other features
of the NetBeans 5.5 Mobility Pack, such as the Wireless Connection Bridge,
Visual Designer and Device Configuration options. It assumes you are already
familiar with the basics on how to use NetBeans IDE.
|
Demo Prep
- Download and
install NetBeans IDE 5.5 and the NetBeans Mobility Pack 5.0.
- Download and install the J2EE 1.4 SDK to host the location information web service.
- Add the J2EE 1.4 SDK (Sun Java System Application Server) to NetBeans (Tools > Server Manager)
- Download
and install the Sun Java Wireless Toolkit 2.5 Beta, which supports LBS.
- Add the Wireless Toolkit 2.5 Beta platform to NetBeans (Tools > Java
Platform Manager).
- Deploy the the Location Information web service to the application server (open the project in NetBeans and select deploy).
- Open the LBS Library
project in NetBeans. This library project, based on sample code that
ships with the wireless toolkit, provides the location based services
for Prague.
Demo
Create the LBS Project
- Create a new Mobile Application, CityGuide.
Run the application and view the HelloMidlet that was created. Return
to NetBeans and familiarize yourself with the Visual Midlet that was
created. Delete HelloMidlet.
- Open the project's properties and set the Emulator Platform to Sun
Java Wireless Toolkit 2.5. Also, add the LBS_Library project to the
Libraries & Resources. Build the project.
- Create a new Visual Midlet, City Guide. Set the MIDlet icon to /visitoron.png and the package to package lbs.
- Add
a splash screen and add an image resource to the splash screen. Select
the image in the Inspector and open it's Resource path property. Set
the image to the logo.png.
- Reduce the timeout property of the splash screen from 5000ms to 2500.
- Add a List screen and change its name the menu.
- Add three List Elements to the menu, Get Map, Help and Exit. Connect the three screens. Your diagram should now look as follows:
- Run the project to preview the splash screen and menu.
Add the Map
The LBS_Library project contains a custom map component. We'll first
add this map component to the palette of our visual designer, then
we'll incorporate it into our project.
- Open the Palette Manager (Tools > Palette Manager >
Mobility Editor). Add the MapCanvas from the LBS_Library project. The
MapCanvas now appears as a custom component in your palette.
- Drag a MapCanvas on to your canvas and name it map. Connect it to the Get Map menu item.
- Add a Back command and connect that back to the menu. Your diagram should now look as follows:
- Run the project to view the map.
- Choose
MIDLet > External events from the emulator's menu. Browse to the
location where you extracted LBS_Library.zip and open citywalk.xml.
Press the Start button at the bottom of the External Event Generator to
emulate walking around town.
Add a Details Button
Here we'll add a details button so we can get basic information on landmarks that we are near.
- Add an OK Command to the map and rename it Details.
- Add a form named details and link the Details command to the form.
- Add a Back Command to the details form and link it back to the map.
Your diagram should now look as follows (Note,
to change the location of an anchor point, hold down the Ctrl key and
drag it around the form. This is how I made the details and back links
look cleaner below.):
- Double-click the details form to open the Screen Designer.
- Change the Title property to Landmark Details .
- Add 4 StringItems to the form. Set their Label properties to: Name, Description, Address, and Phone No. Your Basic Information Screen Design should look as follows:
- Return
to the Flow Design, right-click the Details link connecting the map to
the details form, and choose Go to Source. Add the following pre-action
code:
Landmark lm = get_map().getClosestLandmark();
if (lm != null) {
get_stringItem1().setText(lm.getName());
get_stringItem2().setText(lm.getDescription());
get_stringItem3().setText(lm.getAddressInfo().getField(AddressInfo.STREET));
get_stringItem4().setText(lm.getAddressInfo().getField(AddressInfo.PHONE_NUMBER));
} else {
get_stringItem1().setText("");
get_stringItem2().setText("");
get_stringItem3().setText("");
get_stringItem4().setText("");
}
- Press Alt+Shift+F to fix the imports.
- Run the application and execute the citywalk script. As you walk past landmarks, press the details button.
Use a Web Service to get Current Information
During the demo preparation you installed a Location Information web
service. This web service provides up to date information on the
selected landmark, such as the daily special at a restaurant. Here
we'll add a link on the details form to get this additional
information.
- Create a new Mobile Class Library project named CityGuideWS.
- Add a new J2ME Web Service Client. Set the WSDL URL to http://localhost:1900/LocationInfoWS/InfoService?WSDL and click the Retrieve WSDL button. Then click Finish to generate the web service stub code.
- Add the CityGuideWS project to the Bundled Libraries and Resources of the CityGuide project.
- Add a command titled Get Further Info to the details form.
- We're
going to add a form to show further information. However, since we're
going to make a potentially long running network call to populate this
form, we're going to insert a WaitScreen before the form. Add a
WaitScreen named furtherInfoWS and link the Get Further Info menu to the wait screen.
- Add a Form named furtherInfo
with a Back Command button. Link the furtherInfoWS Success to the
furtherInfo form. Link the back command button to the details form.
- Finally, add an Alert named failureAlert
and link that to the Failure node on furtherInfoWS. By default, an
alert dismisses back to the screen from which it originated, which in
this case is the wait screen. In a connection failure situation, this
would put us in an endless loop - so change the Dismiss node to return
back to the detail form. Your diagram should now look as follows:

- Double-click the furtherInfo form to open the Screen Designer and add a String Item. Change the label to Details:.
- Switch to the source view and add the following method to populate the further information page:
private void getInfo() throws Exception {
InfoServiceSEI client = new InfoServiceSEI_Stub();
String result = client.getInfo(get_map().getClosestLandmark().getName());
get_stringItem5().setText(result);
}
- Press Alt+Shift+F to fix the imports.
- In
order to call blocking operations, such as we will be doing with the
networking call to the monitor servlet in our web application, we need
to set up a separate thread of execution. NetBeans provides a special
class to handle this for us, called a SimpleCancellableTask. You'll
find the SimpleCancellableTask under the Resources section of the
Palette. Drag one onto the furtherInfoWS. The SimpleCancellableTask is
not a visible component, but you can view it under the Resources node
of the Inspector.
- In the property sheet, change the Instance Name of simpleCancellableTask1 to getFurtherInformationTask and set the Executable Method Body to getInfo(); Notice code completion works within the property editor.
- Run
the application and execute the citywalk script. As you walk past
landmarks, press the details button. From the details page, press the
Get Further Info button.
Use an HTTP Proxy for Phones That Don't Support Web Services (Most)
Accessing web services from a mobile device is powerful, however,
very few phones on the market today support JSR 172. In this section
we'll show you how to take advantage of the same web service using
straight HTTP.
- Create a new Web Application named CityGuideWSProxy.
- Add a new Web Service Client. Set the WSDL URL to http://localhost:1900/LocationInfoWS/InfoService?WSDL and click the Retrieve WSDL button. Set the package to infoservice. Click Finish to generate the web service stub code.
- Deploy the CityGuideWSProxy.
- Create a new Mobile Class Library project named CityGuideWSProxy_Support.
- Start the Mobile Client to Web Application wizard (File > New > MIDP)
- CityGuideWSProxy should be the selected Web Application.
- Set the package to infoservice.
- Select Mobile Client Uses Web Service Client in Web Application InfoService.wsdl. Click Next.
- Select all of the getInfo method and click Next.
- Change the Client Class Name to InfoProxyClient, set the package to infoservice, deselect Create sample MIDlet and click Finish.
- Open the MobileMonitorApplication's properties
- Add a new configuration named NonWS
- Uncheck Use Values from "DefaultConfiguration"
- Uncheck J2ME Web Services 1.0.
- Select the Libraries & Resources Category
- Uncheck Use Values from "DefaultConfiguration
- Remove the CityGuideWS.jar.
- Add the WSproxy_Support project to the Bundled Libraries and Resources of the CityGuide project.
- Click OK
- Open the Source view of the City Guide MIDlet and right-click the line
InfoServiceSEI client = new InfoServiceSEI_Stub();
in the getInfo() method and choose Preprocessor Blocks > Create If / Else Block.
- Using code completion, set the if condition to the new NonWS Project Configuration
- Then change the code from using the stub to using the InfoProxyClient created above.
InfoProxyClient client = new InfoProxyClient();
- Press Ctrl+Shift+R to comment the appropriate blocks. The finished block of code should look as follows:
- Change the imports to just
infoservice.*;
Since both the web service and the web service proxy use the same
package name, this saves us the trouble of wrapping the class specific
import statements with preprocessor blocks.
- Run the application and execute the citywalk script. As
you walk past landmarks, press the details button. From the details
page, press the Get Further Info button. The application operates the
same, however, the details are being pulled from the web service via
the web application proxy.
Use the Network Monitor to Verify the Protocol
- Open the Java Platform Manager and select the Sun Java Wireless Toolkit
2.5.
- Select the Tools & Extensions tab and select the Network Monitor check box. Click Close to close the Java Platform Manager.
- Run
the CityGuide project and get further information. Assuming you're
still running under the NonWS configuration, you'll see the HTTP POST
to the WebToMobileServlet when you execut the Get Furter Info step:

- Now switch to the DefaultConfiguration and run the project again. This time you'll see the SOAP call to the InfoService:
Summary
This demo introduced you to location based services. We took an existing sample
application from the Sun Wireless Toolkit 2.3 and augmented it to use web services
to get up to date information on the location to which we are closest. If the
phone does not have direct web services support, we used the code preprocessor
to create a version of the application that calls the web service via a web
application that acts as a proxy for the web service.
Copyright and Trademark Notice