繁体   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