首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >设置Bluecove

设置Bluecove
EN

Stack Overflow用户
提问于 2018-10-18 06:32:21
回答 2查看 369关注 0票数 0

这是我使用bluecove的第一个项目,我甚至在开始之前就遇到了问题!

下面是我的代码:

代码语言:javascript
复制
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,以便在项目中使用蓝牙?

EN

回答 2

Stack Overflow用户

发布于 2018-10-18 06:41:04

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

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

票数 0
EN

Stack Overflow用户

发布于 2020-09-08 21:16:23

为了防止一些不耐烦的人来这里快速复制和粘贴,这里是我的导入列表:

代码语言:javascript
复制
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.UUID;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52864505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档