【问题标题】:j2me bluetooth client. Function startInquiry nothing foundj2me 蓝牙客户端。函数 startInquiry 没有找到
【发布时间】:2011-10-07 23:56:03
【问题描述】:

我开发了简单的 j2me 蓝牙客户端,但蓝牙设备搜索有问题。 函数 startInquiry 没有找到。

客户:诺基亚 5220 服务器:我的电脑带蓝牙适配器

所有蓝牙设备均已开启。

/* * 要更改此模板,请选择工具 |模板 * 并在编辑器中打开模板。 */

import javax.microedition.midlet.*;
import javax.bluetooth.*;
import java.util.Vector;
import javax.microedition.lcdui.*;

/**
 * @author Администратор
 */
public class Midlet extends MIDlet implements DiscoveryListener
{
    private static Vector vecDevices=new Vector();
    private static String connectionURL=null;
    private LocalDevice localDevice;
    private DiscoveryAgent agent;
    private RemoteDevice remoteDevice;
    private RemoteDevice[] devList;
    private Display display;
    private Form form;

    public void startApp() {
        display = Display.getDisplay(this);
        form = new Form( "Client" );
        try {
            localDevice = LocalDevice.getLocalDevice();
        } catch( BluetoothStateException e ) {
            e.printStackTrace();
        }

        form.append("Address: "+localDevice.getBluetoothAddress()+"\n\n");
        form.append("Name: "+localDevice.getFriendlyName()+"\n\n");

        try {
            agent = localDevice.getLocalDevice().getDiscoveryAgent();

            form.append("Starting device inquiry... \n\n");

            boolean si = agent.startInquiry(DiscoveryAgent.GIAC,  this);
            if ( si ) {
                form.append("true");
            } else {
                form.append("false");
            }
        } catch( BluetoothStateException e ) {
        }

        int deviceCount = vecDevices.size();
        if(deviceCount <= 0){
            form.append("No Devices Found .");
        }
        else{
            //print bluetooth device addresses and names in the format [ No. address (name) ]
            form.append("Bluetooth Devices: ");
            for (int i = 0; i < deviceCount; i++) {
                remoteDevice=(RemoteDevice)vecDevices.elementAt(i);
                form.append( remoteDevice.getBluetoothAddress() );
            }
        }

            display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

        public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
              //add the device to the vector
              if(!vecDevices.contains(btDevice)){
                  vecDevices.addElement(btDevice);
              }
        }
        public void inquiryCompleted(int discType)
        {
        }


          //implement this method since services are not being discovered
          public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
              if(servRecord!=null && servRecord.length>0){
                  connectionURL=servRecord[0].getConnectionURL(0,false);
              }
          }
          //implement this method since services are not being discovered
          public void serviceSearchCompleted(int transID, int respCode) {
          }
}

【问题讨论】:

    标签: java-me bluetooth midp


    【解决方案1】:

    不确定确切的问题是什么,但您绝对不想在 midlet 的 startApp() 方法中执行此操作。这是一种系统生命周期方法,应该会很快返回,但是扫描蓝牙设备会阻塞很长时间。您的 startApp() 方法占用了设备的资源,而这些资源可能需要进行实际扫描!

    重构,让您的设备扫描在一个新线程中完成,然后看看会发生什么。

    【讨论】:

      【解决方案2】:

      您似乎误解了蓝牙 API 的工作原理。 startInquiry 方法仅启动设备发现过程并在之后立即返回,让发现在后台运行。当发现设备时,您会收到每个设备的deviceDiscovered 方法的回调,当发现过程完成时,您会收到inquiryCompleted 方法的回调。所以你需要将vecDevices成员的访问和表单操作从startApp移动到inquiryCompleted才能真正显示发现的信息。

      【讨论】:

        【解决方案3】:

        您说所有设备都开启 - 但还要检查所有设备是否可发现

        我之前犯过这个错误!

        如果您想以编程方式在模式之间切换,请查找方法 LocalDevice.setDiscoverable()。

        【讨论】:

          猜你喜欢
          • 2012-07-21
          • 1970-01-01
          • 2018-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-16
          • 2015-10-21
          • 1970-01-01
          • 2011-08-08
          相关资源
          最近更新 更多