⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serviceslist.java

📁 Bluetooth echo between pc server and client
💻 JAVA
字号:

// ServicesList.java
// Andrew Davison, ad@fivedots.coe.psu.ac.th, August 2005

/* The device names for the matching services are shown in a list.
   The user selects one of the names, and the corresponding
   service record is used by ClientForm to start interacting
   with the service.
*/


import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import java.util.*;
import java.io.*;


public class ServicesList extends List
                     implements CommandListener
{
  private Command startCmd, exitCmd;

  private Display display;
  private EchoClientMIDlet ecm;

  // table of key/value pairs of the form {device name, service record}
  private Hashtable servicesTable;

  private String[] deviceNames;      
     // hold names extracted from servicesTables


  public ServicesList(Hashtable st, EchoClientMIDlet ecm, Display d)
  {
    super("Service Selection", List.IMPLICIT);
    servicesTable = st;
    this.ecm = ecm;
    display = d;

    // GUI elements
    exitCmd = new Command("Exit", Command.EXIT, 1);
    startCmd = new Command("Start", Command.SCREEN, 2);
    addCommand(exitCmd);
    addCommand(startCmd);

    setCommandListener(this);

    buildList(servicesTable);   // show list of device names
  }  // end of ServicesList()


  private void buildList(Hashtable st)
  /* Build the list of device names, and also store the names
     in the deviceNames[] array. */
  {
    // load an icon image, used to decorate each list item
    Image icon = null;
    try {
      icon = Image.createImage("/Icon.png");
    } 
    catch (IOException e) {}

    deviceNames = new String[st.size()];
    Enumeration e = st.keys();
    int i=0;
    while (e.hasMoreElements()) {
      deviceNames[i] = (String) e.nextElement();  // add device name to array
      append(deviceNames[i], icon);   // add device name to list
      i++;
    }
  } // end of buildList()



  public void commandAction(Command c, Displayable d)
  /* Use deviceNames[] to map from a selected list element
     to a name. Use that name as a key to lookup the 
     corresponding service record in servicesTable. 
     The record is used by the ClientForm form to start
     interacting with the service.
  */
  {
    if ((c == startCmd) || (c == List.SELECT_COMMAND)) {   // list item selected
      int index = getSelectedIndex();
      String devName = deviceNames[index];   // get the device name
      System.out.println("Selected echo server: " + devName);

      ServiceRecord sr = (ServiceRecord) servicesTable.get(devName);
              // get the corresponding service record for the name

      ClientForm cf = new ClientForm(sr, ecm, display);
      display.setCurrent(cf);      
          /* Display the client form, which carries out the interaction
             with the service. */
    }
    else if (c == exitCmd)
      ecm.destroyApp(true);
  }  // end of commandAction()


}  // end of ServicesList class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -