我想在我的 Raspberry Pi 中看到配对的蓝牙设备。我将 Bluecove 库与 Java 结合使用,有代码:
public class bluetooth {
public static final Vector/*<RemoteDevice>*/ devicesDiscovered = new Vector();
public static void main(String[] args) throws BluetoothStateException, InterruptedException {
String result= showDevices();
System.out.println(result);
}
public static String showDevices() throws BluetoothStateException, InterruptedException {
final Object inquiryCompletedEvent = new Object();
String message = null;
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();
message = devicesDiscovered.size() + " device(s) found";
System.out.println(devicesDiscovered.size() + " device(s) found");
}
}
return message;
}
}
没有更多已配对的设备:
BlueCove version 2.1.1-SNAPSHOT on bluez
wait for device inquiry to complete...
Device Inquiry completed!
0 device(s) found
0 device(s) found
BlueCove stack shutdown completed
但是我已经配对了三台设备。我对此很确定。互联网上也没有其他类似的例外。如果解决了这个问题,它可能是一个很好的来源。
注意::该代码可在Windows上成功运行。
重要说明:此问题已解决! 解决方案:
而不是:java -jar try.jar
这应该使用:sudo java -jar try.jar
我写信是为了使那些从事这项工作的人容易上手。