首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >蓝牙连接抛出错误

蓝牙连接抛出错误
EN

Stack Overflow用户
提问于 2012-09-12 07:55:56
回答 1查看 1.2K关注 0票数 1

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

私有类ConnectThread扩展线程{ private final BluetoothSocket mmSocket;private final BluetoothDevice mmDevice;

代码语言:javascript
复制
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.

**

代码语言:javascript
复制
    } 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作为测试设备。

提前谢谢你。

EN

回答 1

Stack Overflow用户

发布于 2012-09-12 11:42:06

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

代码语言:javascript
复制
createRfcommSocketToServiceRecord(MY_UUID);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12379508

复制
相关文章

相似问题

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