第一个程序可以获得周围设备地址,但第二种程序获取不了,不知道什么原因,希望哪位给解答下,小弟不胜感激,我实在没辙了,上帝。。。第二个程序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();
}
}
}
第一个程序如下
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();
}
}
}