1

我已经编写了一个普通代码来连接到标准蓝牙设备(一个 wocket)。我正在做的是作为客户端连接到 wocket(它只是一块蓝牙芯片)。我正在使用随机 UUID 连接到 wocket。但是 connect 方法抛出一个 IO 异常,告诉我它无法连接到蓝牙设备。我使用的代码来自 Android 开发者论坛:http: //developer.android.com/guide/topics/connectivity/bluetooth.html

私有类 ConnectThread 扩展线程 { 私有最终 BluetoothSocket mmSocket; 私有最终蓝牙设备 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 进行编程。

我正在使用带有 Android 4.0 的 HTC one x 作为测试设备。

先感谢您。

4

1 回答 1

0

尽管使用随机 UUID,但请使用串行端口配置文件的 UUID,即“00001101-0000-1000-8000-00805f9b34fb”,因为 wocket 可能不支持您使用的随机 UUID。当你这样做时

createRfcommSocketToServiceRecord(MY_UUID);

它会在您连接的设备上进行服务发现,并查看您提供的 UUID 是否存在于设备中,如果存在则连接到设备。

于 2012-09-12T03:42:06.083 回答