gpt4 book ai didi

Java - 蓝牙 API Bluecove 问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:58:31 24 4
gpt4 key购买 nike

我在使用 Java 中的 Bluecove API 时遇到问题。我计划创建一个基本的 BT 监听器,以便从自制设备(Arduino 供电)接收一些数据。我有以下很容易理解的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.bluetooth.*;
import javax.microedition.io.*;

public class Bluetooth
{
private void startServer() throws IOException
{
//Create a UUID for SPP
UUID uuid = new UUID("1101", true);
//Create the servicve url
String connectionString = "btspp://localhost:" + uuid +";name=Magic Merlin Server";

//open server url
StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open(connectionString);

//Wait for client connection
System.out.println("\nServer Started. Waiting for clients to connect...");
StreamConnection connection = streamConnNotifier.acceptAndOpen();

RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: "+dev.getBluetoothAddress());
System.out.println("Remote device name: "+dev.getFriendlyName(true));

//read string from spp client
InputStream inStream=connection.openInputStream();
BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
String lineRead=bReader.readLine();
System.out.println(lineRead);

//send response to spp client
OutputStream outStream = connection.openOutputStream();
PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
pWriter.write("Response String from SPP Server\r\n");
pWriter.flush();
pWriter.close();

streamConnNotifier.close();
}

public static void main(String[] args) throws IOException
{
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: "+localDevice.getBluetoothAddress());
System.out.println("Name: "+localDevice.getFriendlyName());

Bluetooth sampleSPPServer = new Bluetooth();
sampleSPPServer.startServer();
}
}

当我尝试执行我的代码时,出现了这个奇怪的错误:

Exception in thread "main" javax.bluetooth.BluetoothStateException: BlueCove com.intel.bluetooth.BluetoothStackBlueZ not available
at com.intel.bluetooth.BlueCoveImpl.loadStackClass(BlueCoveImpl.java:342)
at com.intel.bluetooth.BlueCoveImpl.detectStack(BlueCoveImpl.java:427)
at com.intel.bluetooth.BlueCoveImpl.access$500(BlueCoveImpl.java:65)
at com.intel.bluetooth.BlueCoveImpl$1.run(BlueCoveImpl.java:1020)
at java.security.AccessController.doPrivileged(Native Method)
at com.intel.bluetooth.BlueCoveImpl.detectStackPrivileged(BlueCoveImpl.java:1018)
at com.intel.bluetooth.BlueCoveImpl.getBluetoothStack(BlueCoveImpl.java:1011)
at javax.bluetooth.LocalDevice.getLocalDeviceInstance(LocalDevice.java:75)
at javax.bluetooth.LocalDevice.getLocalDevice(LocalDevice.java:95)
at Bluetooth.main(Bluetooth.java:50)

第 50 行是 LocalDevice localDevice = LocalDevice.getLocalDevice();

我的计算机上有一个启用的 BT 加密狗,所以我对此非常失望。如果您对如何解决这个问题有任何想法,那就太好了!

无论如何感谢您阅读本主题。问候。

最佳答案

如果这是在 Linux 上,您可以检查两件事:

  • 你的类路径中有 net.sf.bluecove:bluecove-gpl:2.1.0 吗?参见 this了解更多信息。
  • 你有安装libbluetooth-dev包吗?不过,这可能是 Xubuntu 特有的。这个包里有 BlueZ,输入 apt-cache show libbluetooth-dev 查看说明。其他 Linux 发行版可能有等效的库

希望这对您有所帮助。

关于Java - 蓝牙 API Bluecove 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13047048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com