package net.benhui.btgallery.discovery_gui;

import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import java.util.*;
import net.benhui.btgallery.*;



/**
 *
 * <p>Title: A GUI list that show all found service records.</p>
 * <p>Description: Service Record ServiceClassIDList are shown on the list. The
 * list is taken from Discovery_MIDlet.services.</p>
 * <p>Copyright: Copyright (c) 2003</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 ServiceDiscoveryUI extends List
{
  public ServiceDiscoveryUI()
  {
    super("Service List", List.IMPLICIT);

    addCommand( new Command( "Back", Command.BACK, 2 ) );

    // direct all user commands to DiscoveryMain
    setCommandListener( Discovery_MIDlet.instance );
  }

  /**
   * clear the list and populate with a list of service record.
   * the ServiceClassIDList first element is displayed for the record.
   * This is translated into human friendly name, based on the website
   * https://www.bluetooth.org/foundry/assignnumb/document/service_discovery
   * This value is usually represent the profile name of the record.
   */
  public void showui()
  {
    int s = this.size();
    for ( int i=0; i< s; i++)
      delete(0);

    for ( int i=0; i< Discovery_MIDlet.bluetooth.services.size(); i++ )
    {
      try
      {
        ServiceRecord rec = (ServiceRecord) Discovery_MIDlet.bluetooth.services.elementAt(i);

        // get the 1st element of ServiceClassIDList
        // and translate it to human friendly name
        DataElement e = rec.getAttributeValue(  0x0001 ); // ServiceClassIDList
        Enumeration enume = (Enumeration)e.getValue();
        DataElement e2 = (DataElement)enume.nextElement();
        Object v = e2.getValue();

        // name looks like "#1 SerialPort"
        String name = "#"+i+" "+ Util.uuidToName( (UUID) v );
        append( name, null );

      } catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  }

}
