我已经写了一个普通的代码来连接一个标准的蓝牙设备(一个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;
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作为测试设备。
提前谢谢你。
发布于 2012-09-12 11:42:06
尽管使用随机UUID,但请使用串行端口配置文件的UUID,即"00001101-0000-1000-8000-00805f9b34fb“,因为wocket可能不支持您正在使用的随机UUID。当你这样做的时候
createRfcommSocketToServiceRecord(MY_UUID);它在您连接到的设备上执行服务发现,并查看您给定的UUID是否存在于设备中,如果存在,则连接到设备。
https://stackoverflow.com/questions/12379508
复制相似问题