这是我在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,以便可以在项目中使用蓝牙?
答案 0 :(得分:0)
前两个import
语句通知Java您在代码中引用的IOException
和ArrayList
类的完整包路径。
一旦开始在代码中使用bluecove类,则需要添加一个或多个import语句,告诉Java这些类的完整软件包路径。
答案 1 :(得分:0)
以防万一有些急躁的人来这里进行快速复制和粘贴,这是我的进口清单:
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.UUID;