【问题标题】:Setup for BluecoveBluecove 的设置
【发布时间】:2020-09-08 21:16:23
【问题描述】:

这是我第一个使用 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,以便在我的项目中使用蓝牙?

【问题讨论】:

    标签: java classpath bluecove


    【解决方案1】:

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

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

    【讨论】:

    • 哦,好的,bluecove 文件的包名是什么?这是一个罐子,所以我看不到包声明。
    • 如果您决定使用DiscoveryAgent,则需要import javax.bluetooth.DiscoveryAgent;
    • 您可以在 API 文档中查找您可能需要的任何其他导入,或者您可以将 bluecove jar 添加到您的 IDE 并让它帮助您添加导入。
    【解决方案2】:

    以防万一有些不耐烦的人来这里快速复制和粘贴,这是我的进口清单:

    import javax.bluetooth.BluetoothStateException;
    import javax.bluetooth.DiscoveryAgent;
    import javax.bluetooth.DiscoveryListener;
    import javax.bluetooth.LocalDevice;
    import javax.bluetooth.RemoteDevice;
    import javax.bluetooth.UUID;
    

    【讨论】:

      猜你喜欢
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多