package profilesj2me;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import profiles.*;

public class ProfilesJ2ME extends MIDlet {
  private static ProfilesJ2ME instance;
  private MainForm displayable = new MainForm();
  J2MEBasicService basicService = null;
  J2MEDevice theDevice = null;
  RoutingManager routingManager = null;
  Profile theProfile;
  ProfileMatcher pMtcr;

  /** Constructor */
  public ProfilesJ2ME() {
    instance = this;

    try {
      basicService = new J2MEBasicService();
      theDevice = new J2MEDevice(javax.bluetooth.LocalDevice.
                                 getLocalDevice().getBluetoothAddress());
      basicService.registerListnerDevice(theDevice);
      RoutingTable rt = new RoutingTable(theDevice);
      routingManager = new RoutingManager(rt, basicService);
      routingManager.start();
      theDevice.setRoutingTable(rt);
      theProfile = new Profile(theDevice.getId());
      pMtcr = new ProfileMatcher(theDevice, basicService,
                                                theProfile);
      pMtcr.start();
    }
    catch (Exception ex) {
      System.out.println("In ProfilesJ2ME constructor:");
      ex.printStackTrace();
    }

  }

  /** Main method */
  public void startApp() {
    Display.getDisplay(this).setCurrent(displayable);
  }

  /** Handle pausing the MIDlet */
  public void pauseApp() {
  }

  /** Handle destroying the MIDlet */
  public void destroyApp(boolean unconditional) {
  }

  /** Quit the MIDlet */
  public static void quitApp() {
    instance.destroyApp(true);
    instance.notifyDestroyed();
    instance = null;
  }

  public static ProfilesJ2ME getInstance() {
    return instance;
  }

  public J2MEDevice getTheDevice() {
    return theDevice;
  }

  public Profile getProfile() {
    return theProfile;
  }

  public ProfileMatcher getProfileMatcher() {
    return pMtcr;
  }
}
