蓝牙StartInquiry返回不了周围设备地址

第一个程序可以获得周围设备地址,但第二种程序获取不了,不知道什么原因,希望哪位给解答下,小弟不胜感激,我实在没辙了,上帝。。。第二个程序startinquiry一启动后就输出INQUIRY_ERROR,百思不得其解,另外第一个程序为什么要加synchronized,不加就获取不了地址
第一个程序如下
import java.io.IOException;
import java.util.Vector;
import javax.bluetooth.*;

/**
 * Minimal Device Discovery example.
 */
public class RemoteDeviceDiscovery {

    public static final Vector/*<RemoteDevice>*/ devicesDiscovered = new Vector();

    public static void main(String[] args) throws IOException, InterruptedException {

        final Object inquiryCompletedEvent = new Object();

        devicesDiscovered.clear();

        DiscoveryListener listener = new DiscoveryListener() {

            public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
                System.out.println("Device " + btDevice.getBluetoothAddress() + " found");
                devicesDiscovered.addElement(btDevice);
                try {
                    System.out.println("     name " + btDevice.getFriendlyName(false));
                } catch (IOException cantGetDeviceName) {
                }
            }

            public void inquiryCompleted(int discType) {
                System.out.println("Device Inquiry completed!");
               synchronized(inquiryCompletedEvent){
                    inquiryCompletedEvent.notifyAll();
                }
            }

            public void serviceSearchCompleted(int transID, int respCode) {
            }

            public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
            }
        };

        synchronized(inquiryCompletedEvent) {
            boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
            if (started) {
                System.out.println("wait for device inquiry to complete...");
                inquiryCompletedEvent.wait();
                System.out.println(devicesDiscovered.size() +  " device(s) found");
            }
        }
    }

}


第二个程序如下
package RemoteClient;



import javax.bluetooth.*;
import java.util.Vector;


public class DeviceSearch implements DiscoveryListener,Runnable {
    private Vector DeviceList;
    private boolean SearchComplete=false;
    final Object lock=new Object();
    public void run(){
        try {
            LocalDevice local = LocalDevice.getLocalDevice();
            DiscoveryAgent agent=local.getDiscoveryAgent();
            agent.startInquiry(DiscoveryAgent.GIAC, this);
            synchronized(lock){
             try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
            }
        } catch (BluetoothStateException ex) {

        }
    }
    public  void deviceDiscovered(RemoteDevice btDevice,
            DeviceClass cod) {
    
        DeviceList.addElement(btDevice);
        System.out.println(btDevice.getBluetoothAddress());
      

    }

    public void servicesDiscovered(int transID,
            ServiceRecord[] servRecord) {
    }

    public void serviceSearchCompleted(int transID,
            int respCode) {
    }

    public void inquiryCompleted(int discType) {
      System.out.println("complete");
         System.out.println(discType);
       SearchComplete=true;
     synchronized(lock){
       lock.notify();
       }
    }
    public boolean isSearchComplete(){
        return SearchComplete;
    }
    public Vector GetDevice(){
        return DeviceList;
    }
}



package RemoteClient;

public class Test {
public static void main(String[] args){
DeviceSearch ds=new DeviceSearch();
Thread t=new Thread(ds);

t.start();

try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值