首页 > 解决方案 > 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,以便在我的项目中使用蓝牙?

标签: javaclasspathbluecove

解决方案


前两个import语句通知 Java您在代码中引用的IOException和类的完整包路径。ArrayList

一旦您开始在代码中使用 bluecove 类,您将需要添加一个或多个 import 语句,告诉 Java 这些类的完整包路径。


推荐阅读