我對android和java很陌生,所以要溫柔:) 我試圖通過blototh連接兩部手機。 我讓兩部手機都通過創建serversocket來監聽來電,然後從一部手機(作爲客戶端)初始化連接。有趣的是,當我試圖讓我的LG(Android版本2.3.4)連接宏達電(Android 2.2.1)一切工作正常,但是當我嘗試使HTc手機作爲客戶端連接我沒有得到任何結果。調試器顯示HTC在mmSocket.connect()失敗。並執行catch(IOException connectException)。我的代碼基本上是從Android藍牙教程複製/粘貼。任何建議爲什麼手機行爲不同?連接螺紋:藍牙只能使用一種方式
private class ConnectThread extends Thread {
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
BtAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect(); **HTC phones fails here and goes to CATCH block**
// make info message
Message msg = mainHandler.obtainMessage();
Bundle bundle = new Bundle();
String btnTxt = "Connected";
bundle.putString("myKey", btnTxt);
msg.setData(bundle);
mainHandler.sendMessage(msg);
} 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)
ConnectedThread conThread = new ConnectedThread(mmSocket);
conThread.start();
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}