package net.benhui.btgallery.bluelet_example;

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

import net.benhui.btgallery.bluelet.*;

/**
*
* <p>Title: Example Application that uses Bluelet Component</p>
* <p>Description:
* This example application show the basic structure of a MIDlet
* that uses Bluelet component to develop a Bluetooth application.
* Read the in-line documentation for details.
* </p>
* @author Ben Hui (www.benhui.net)
* @version 1.0
*
* LICENSE:
* This code is licensed under GPL. (See http://www.gnu.org/copyleft/gpl.html)
*/
public class YourMIDlet extends MIDlet implements CommandListener
{
  // singleton self instance
  public static YourMIDlet instance = null;
  // display reference
  public static Display display = null;
  // a screen to output text (for logging text)
  public YourScreen screen;

  // BLUElet component
  // To perform device discovery, device selection and service discovery
  BLUElet bluelet = null;

  public YourMIDlet()
  {
    instance = this;
    // create a instance of BLUElet
    // the first parameter is host MIDlet
    // this second parameter is CommandListener (also this class)
    bluelet = new BLUElet( this, this );
  }
  protected void startApp() throws javax.microedition.midlet.MIDletStateChangeException
  {
    // do local initialization
    display = Display.getDisplay(this);
    screen = new YourScreen();

    // do BLUElet initialization
    bluelet.startApp();

    display.setCurrent( screen );
  }
  protected void pauseApp()
  {
    // do Bluelet pause
    bluelet.pauseApp();
  }
  protected void destroyApp(boolean unconditional) throws javax.microedition.midlet.MIDletStateChangeException
  {
    // do Bluelet destroy
    bluelet.destroyApp( unconditional );
  }

  public void commandAction(Command c, Displayable d)
  {
    if ( d == screen && c.getLabel().equals("Search") )
    {
      // when user press Search, we invoke Bluelet to
      // perform device discovery, then we should switch the screen
      // to Bluelet Devices screen for user to select an device
      //
      // in this example we seach for OBEX services
      // OBEX PUSH PROFILE 0x1106
      bluelet.startInquiry( DiscoveryAgent.GIAC, new UUID[]{  new UUID( 0x1106 ) });
      display.setCurrent( bluelet.getUI() );

    } else if ( c.equals( BLUElet.SELECTED ) )
    {
      // handle event that user has selected a device for service discovery
      // in this example, we ignore it
    } else if ( c.equals( BLUElet.BACK ) )
    {
      // handle event that user has press Back button on Bluetooth Devices screen
      // usually, we switch back to host application screen
      display.setCurrent( screen );
    } else if ( c.equals( BLUElet.COMPLETED ) )
    {
      // handle event that service discovery is completed
      // usually we pass control back to host MIDlet and connect
      // to remote service for perform network communication
      display.setCurrent( screen );

      ServiceRecord r = bluelet.getSelectedService();

      String url  = r.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false );
      log("url:"+url);

      // use the url to connect....

    }
  }


  /**
   * An utility function to write log to screen
   * @param m String
   */
  public static void log(String m)
  {
    instance.screen.add( m );
    instance.screen.repaint();
  }
}
