簡體   English   中英

藍牙連接拋出錯誤

[英]Bluetooth connection throwing error

我寫了一個普通的代碼來連接到標准的藍牙設備(wocket)。 我正在做的是作為客戶端連接到wocket(這只是一塊藍牙芯片)。 我正在使用隨機UUID連接到wocket。 但是connect方法引發IO異常,告訴我它無法連接到藍牙設備。 我使用的代碼來自Android開發者論壇, 網址為http : //developer.android.com/guide/topics/connectivity/bluetooth.html

私有類ConnectThread擴展了Thread {私有最終BluetoothSocket mmSocket; 私有最終BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {
    // Use a temporary object that is later assigned to mmSocket,
    // because mmSocket is final
    BluetoothSocket tmp = null;
    mmDevice = device;

    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
        // MY_UUID is the app's UUID string, also used by the server code
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) { }
    mmSocket = tmp;
}

public void run() {
    // Cancel discovery because it will slow down the connection
    mBluetoothAdapter.cancelDiscovery();

    try {
        // Connect the device through the socket. This will block
        // until it succeeds or throws an exception
        mmSocket.connect();
        **This call is throwing and IOException saying:
        java.io.IOException Discovery Failed.

**

    } catch (IOException connectException) {
        // Unable to connect; close the socket and get out
        try {
            mmSocket.close();
        } catch (IOException closeException) { }
        return;
    }

    // Do work to manage the connection (in a separate thread)
    manageConnectedSocket(mmSocket);
}

/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
    try {
        mmSocket.close();
    } catch (IOException e) { }
}

}

wocket拋出IOException的原因可能是什么。 我認為將它作為服務器連接到服務器並沒有任何意義,就像您建立一個UUID的服務器,然后您希望客戶端具有相同的UUID一樣,在我的情況下這是不可能的,因為我無法對wocket進行編程。

我正在將HTC one x與Android 4.0用作測試設備。

先感謝您。

盡管使用隨機UUID,但請使用串行端口配置文件的UUID,即“ 00001101-0000-1000-8000-00805f9b34fb”,因為您的隨身聽可能不支持您使用的隨機UUID。 當你做

createRfcommSocketToServiceRecord(MY_UUID);

它會在您要連接的設備上進行服務發現,並查看給定的UUID是否存在於設備中(如果存在),它會連接到設備。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM