j2me蓝牙客户端。功能startInquiry没找到

时间:2010-03-10 11:04:59

标签: java-me bluetooth midp

我开发简单的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) {
          }
}

3 个答案:

答案 0 :(得分:1)

不确定确切的问题是什么,但您绝对不希望在midlet的startApp()方法中执行此操作。这是一个系统生命周期方法,应该快速返回,但扫描蓝牙设备会长时间阻止它。您的startApp()方法正在占用设备执行实际扫描所需的资源!

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

答案 1 :(得分:1)

您似乎误解了蓝牙API的工作原理。只有startInquiry方法启动设备发现过程,然后立即返回,让发现在后台运行。当发现设备时,您会为每个设备获得deviceDiscovered方法的回调,并且当发现过程完成后,您将获得inquiryCompleted方法的回调。因此,您需要将vecDevices成员的访问权限和表单操作从startApp移至inquiryCompleted,以便能够实际显示已发现的信息。

答案 2 :(得分:0)

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

我自己犯了这个错误!

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