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

  1. Download and install NetBeans IDE 5.5 and the NetBeans Mobility Pack 5.0.
  2. Download and install the J2EE 1.4 SDK to host the location information web service.
  3. Add the J2EE 1.4 SDK (Sun Java System Application Server) to NetBeans (Tools > Server Manager)
  4. Download and install the Sun Java Wireless Toolkit 2.5 Beta, which supports LBS.
  5. Add the Wireless Toolkit 2.5 Beta platform to NetBeans (Tools > Java Platform Manager).
  6. Deploy the the Location Information web service to the application server (open the project in NetBeans and select deploy).
  7. 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

  1. 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.
  2. 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.
  3. Create a new Visual Midlet, City Guide. Set the MIDlet icon to /visitoron.png and the package to package lbs.
  4. 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.


  5. Reduce the timeout property of the splash screen from 5000ms to 2500.
  6. Add a List screen and change its name the menu.
  7. Add three List Elements to the menu, Get Map, Help and Exit. Connect the three screens. Your diagram should now look as follows:


  8. 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.

  1. 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.
  2. Drag a MapCanvas on to your canvas and name it map. Connect it to the Get Map menu item.
  3. Add a Back command and connect that back to the menu. Your diagram should now look as follows:


  4. Run the project to view the map.
  5. 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.

  1. Add an OK Command to the map and rename it Details.
  2. Add a form named details and link the Details command to the form.
  3. 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.):


  4. Double-click the details form to open the Screen Designer.
  5. Change the Title property to Landmark Details .
  6. 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:


  7. 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(""); 
    }
    
  8. Press Alt+Shift+F to fix the imports.
  9. 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.

  1. Create a new Mobile Class Library project named CityGuideWS.
  2. 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.
  3. Add the CityGuideWS project to the Bundled Libraries and Resources of the CityGuide project.
  4. Add a command titled Get Further Info to the details form.
  5. 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.
  6. 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.
  7. 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:


  8. Double-click the furtherInfo form to open the Screen Designer and add a String Item. Change the label to Details:.
  9. 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);
    } 
  10. Press Alt+Shift+F to fix the imports.
  11. 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.


  12. 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.
  13. 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.

  1. Create a new Web Application named CityGuideWSProxy.
  2. 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.
  3. Deploy the CityGuideWSProxy.
  4. Create a new Mobile Class Library project named CityGuideWSProxy_Support.
  5. Start the Mobile Client to Web Application wizard (File > New > MIDP)
  6. Open the MobileMonitorApplication's properties
  7. 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.
  8. Using code completion, set the if condition to the new NonWS Project Configuration


  9. Then change the code from using the stub to using the InfoProxyClient created above.

    InfoProxyClient client = new InfoProxyClient();
  10. Press Ctrl+Shift+R to comment the appropriate blocks. The finished block of code should look as follows:


  11. 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.
  12. 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

  1. Open the Java Platform Manager and select the Sun Java Wireless Toolkit 2.5.
  2. Select the Tools & Extensions tab and select the Network Monitor check box. Click Close to close the Java Platform Manager.
  3. 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:



  4. 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