2012-10-24 100 views
2

我在使用Java中的Bluecove API時遇到問題。我計劃創建一個基本的BT監聽器,以便從自制設備(Arduino驅動)接收一些數據。我有下面的代碼是很容易理解:Java - 藍牙API Bluecove問題

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加密狗,因此我對此非常失望。 如果你對如何解決這個問題有任何想法,那就太好了!

非常感謝您閱讀本主題。 此致敬禮。

回答

0

最後,我找到了一個使用名爲「GlovePie」的應用程序的更簡單方法。它提供了一個非常簡單的界面來檢索傳感器值並通過網絡協議發送。然後,一個java程序聽取該protrocol並獲取值。

上述錯誤(在我的第一篇文章中)是Bluecove跟蹤器上報告的常見問題。

5

如果這是在Linux上,兩件事情,你可以檢查:

  • 你有你的類路徑net.sf.bluecove:bluecove-gpl:2.1.0?有關更多信息,請參見this
  • 您是否已安裝libbluetooth-dev軟件包?不過,這可能是Xubuntu特有的。該軟件包包含BlueZ,請輸入apt-cache show libbluetooth-dev以查看說明。其他Linux發行版可能有一個等效庫

希望這有助於。

+0

爲我解決。什麼是downvote爲? –