我写了一个正常的代码来连接标准的蓝牙设备(一个wocket)。我正在做的是连接到wocket(它只是一块蓝牙芯片),作为客户端。我使用随机UUID连接到wocket。但是连接方法抛出一个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作为测试设备。
提前谢谢。
答案 0 :(得分:0)
尽管使用随机UUID,但使用串行端口配置文件的UUID,即“00001101-0000-1000-8000-00805f9b34fb”,因为您使用的随机UUID可能不受wocket支持。当你做的时候
createRfcommSocketToServiceRecord(MY_UUID);
它在您要连接的设备上执行服务发现,并查看您提供的UUID是否在设备中,如果存在则连接到设备。