btservicelist.java

来自「《开发Nokia S40应用程序》源码(8-10章) 《开发Nokia S40」· Java 代码 · 共 71 行

JAVA
71
字号
package com.Series40Book;

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

import java.util.*;

public class BTServiceList extends List implements CommandListener{

  private BTDiscovery midlet;
  private BTUtil btutil;

  private Command back;


  public BTServiceList(BTDiscovery midlet, BTUtil btutil) {

    super("BTServiceList", List.IMPLICIT);
    this.midlet  = midlet;
    this.btutil = btutil;

    back = new Command ("Back", Command.BACK,1);

    addCommand (back);
    setCommandListener( this );
  }

  public void commandAction(Command c, Displayable d) {
    if (c == back) {
     midlet.display.setCurrent(midlet.infoForm);
    }

  }

  /**
   * clear the screen and display RemoteDevice friendly names on screen.
   * The deivces are retrieve from DiscoveryMain.devices vector.
   */
  public void displayservices()
  {
    // clear the whole list
    int len = this.size();
    for (int i = 0; i < len; i++) {
      delete(0);
    }

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

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

        // name looks like "#1 SerialPort"
        String name = "#" + i + " " + btutil.BTUUIDToName( (UUID) v);
        append(name, null);  // append to List

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

  }

}

⌨️ 快捷键说明

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