java - Bluecove 的设置
问题描述
这是我的第一个 bluecove 项目,甚至在我开始之前就遇到了问题!
这是我的代码:
import java.io.IOException;
import java.util.ArrayList;
import bluecove;
/**
* Class that discovers all bluetooth devices in the neighbourhood and
displays their name and bluetooth address.
*/
public class BluetoothDeviceDiscovery implements DiscoveryListener {
// object used for waiting
private static Object lock = new Object();
// vector containing the devices discovered
public static ArrayList<RemoteDevice> devices;
public BluetoothDeviceDiscovery() {
devices = new ArrayList<RemoteDevice>();
}
// main method of the application
public static void main(String[] args) throws IOException {
//create an instance of this class
BluetoothDeviceDiscovery bluetoothDeviceDiscovery=new
BluetoothDeviceDiscovery();
//display local device address and name
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: " + localDevice.getBluetoothAddress());
System.out.println("Name: " + localDevice.getFriendlyName());
//find devices
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
System.out.println("Starting device inquiry…");
agent.startInquiry(DiscoveryAgent.GIAC, bluetoothDeviceDiscovery);
try {
synchronized(lock) {
lock.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Device Inquiry Completed. ");
//print all devices in devices
int deviceCount=devices.size();
System.out.println(deviceCount);
if(deviceCount <= 0) {
System.out.println("No Devices Found .");
} else {
//print bluetooth device addresses and names in the format [ No. address (name) ]
System.out.println("Bluetooth Devices:" );
for (int i = 0; i < deviceCount; i++) {
RemoteDevice remoteDevice=(RemoteDevice)devices.get(i);
System.out.println(i +". "+remoteDevice.getBluetoothAddress() );
//("+remoteDevice.getFriendlyName(true)+")");
}
}
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{
System.out.println("Device discovered: "+btDevice.getBluetoothAddress());
//add the device to the vector
if(!devices.contains(btDevice))
{
devices.add(btDevice);
}
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
}
public void serviceSearchCompleted(int transID, int respCode) {
}
public void inquiryCompleted(int discType)
{
synchronized(lock){
lock.notify();
}
switch (discType)
{
case DiscoveryListener.INQUIRY_COMPLETED :
System.out.println("INQUIRY_COMPLETED");
break;
case DiscoveryListener.INQUIRY_TERMINATED :
System.out.println("INQUIRY_TERMINATED");
break;
case DiscoveryListener.INQUIRY_ERROR :
System.out.println("INQUIRY_ERROR");
break;
default :
System.out.println("Unknown Response Code");
break;
}
}
}
我在第 3 行收到一个错误(惊喜,惊喜)告诉我一个 ';' 预料之中。
我知道我收到错误的真正原因是我没有正确导入 bluecove。
我下载了 bluecove jar,将其重命名并将文件添加到我的类路径中,我认为我现在可以在我的 java 项目中导入和使用 bluecove。
我是否以错误的方式设置/导入 bluecove?我如何实际导入 bluecove,以便在我的项目中使用蓝牙?
解决方案
前两个import
语句通知 Java您在代码中引用的IOException
和类的完整包路径。ArrayList
一旦您开始在代码中使用 bluecove 类,您将需要添加一个或多个 import 语句,告诉 Java 这些类的完整包路径。
推荐阅读
- mysql - 为什么这个框架的默认 OVER () 行为?
- react-native - 如何在 redux 状态更改时做出反应以重新呈现页面
- java - MapStruct - 找不到 IntelliJ 的实现
- performance-testing - 我们如何在 gatling 中发送来自同一用户的多个请求?
- go - 通过强制转换或类型断言改变结构
- c# - 如何在 C sharp api 中获取 OpenID 连接流中的令牌?
- php - 如何在codeigniter中插入多个复选框值
- php - 如何使日期格式在所有环境中保持一致
- objective-c - popViewControllerAnimated 后 NSString 为 nil
- python - 仅从 BeautifulSoup 获取数字而不是整个 div