lunedì 29 novembre 1999

Portable J2ME application: deal with the optional package

When you want to use the API of a J2ME optional package, you have to start to deal with the differences through the different mobile devices. The implementation of the different JVMs doesn't guarantee that the not mandatory API are supported so you can experience problems already in the installation phase.

You can only try to follow some guidelines to make this risk as small as possible and have a midlet working on a large number of devices.

A good way to do that is try to isolate the part of the code containing references to the not mandatory APIs. An interface is used to decouple the main code of the midlet from the code containing class which can be not included in the implementation of the JVM. All this code needs to be included in a class implementing this interface. The code of the midlet uses the Class.forName() and Class.forInstance() instructions to instantiate this class after the runtime check about the support of the Optional Package we would use. In this way the class using the not supported class is not loaded in the virtual machine if the JVM doesn't support them.

Interface

public interface BluetoothManager {

   public void send(Object O);
}

Class implementing the interface

public class BluetoothManagerImpl {
  
   public void send(Object o) {
      javax.bluetooth.LocalDevice ld = javax.bluetooth.LocaleDevice.getLocalDevice();
      javax.bluetooth.DiscoveryAgent = ld.getDiscoveryAgent();
      .....
}

Main code of the midlet

public class MyMidlet extends MIDlet {
   String btSupport = System.getProperty( "bluetooth.api.version" );
  
   if (btSupport == null )
       return;

  Class c = Class.forName("BluetoothManagerImpl");

  BluetoothManager btManager = (BluetoothManager )c.newInstance();

   .....
}

Nessun commento:

Posta un commento