我在Android4.1中有两部Android手机可能我可以称它们为手机A和手机B。然后我知道手机A的MAC地址和uuid。所以我想做的是,我打算给出MAC地址和手机A的uuid到手机B,并让B通过蓝牙连接到A。所以A是蓝牙服务器,B是蓝牙客户端。但是当我这样做时。电话 B 说
W/System.err(18404): java.io.IOException: Host is down
电话A中的代码作为服务器是这样的:
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
try {
Method m = mBluetoothAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[] {int.class});
tmp = (BluetoothServerSocket)m.invoke(mBluetoothAdapter, new Object[]{25});
Log.i("liyufei","in server we begin at uuid = "+selfUuid);
} catch (Exception e) {
e.printStackTrace();
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
Log.i("liyufei","listening is begin");
while (true) {
try {
Log.i("liyufei","wait...............");
socket = mmServerSocket.accept();
} catch (IOException e) {
e.printStackTrace();
break;
}
Log.i("liyufei","keep going socket is try to create");
if (socket != null) {
Log.i("liyufei","server socket accept!!!");
mHandler.obtainMessage(MESSAGE_ACCEPT).sendToTarget();
try {
mmServerSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
Log.i("liyufei","socket is null");
}
Log.i("liyufei","while is stop");
}
/** Will cancel the listening socket, and cause the thread to finish */
public void cancel() {
try {
mmServerSocket.close();
Log.i("liyufei","we try to Close server!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
电话B中的代码作为客户端:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
Log.i("liyufei","new connect thread......");
try {
Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(mmDevice,Integer.valueOf(25));
} catch (Exception e) {
e.printStackTrace();
}
mmSocket = tmp;
}
public void run() {
mBluetoothAdapter.cancelDiscovery();
try {
Log.i("liyufei","we try to connect.....");
mmSocket.connect();
} catch (Exception connectException) {
Log.i("liyufei","connect to server failed...");
connectException.printStackTrace();
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
Log.i("liyufei","client has success connect!!!");
mHandler.obtainMessage(MESSAGE_CONNECT).sendToTarget();
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
然后我就可以得到 IOException:Host is down after mmSocket.connect(); 在电话B 谁能帮帮我???非常感谢!!!!!!来自@Waqas 的测试后日志建议
09-25 14:31:26.587: I/liyufei(12453): new connect thread......
09-25 14:31:26.587: I/liyufei(12453): we begin in connect uuid = 66d77030-9982-42cf-88a7-272954408361
09-25 14:31:26.595: E/BluetoothService.cpp(308): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
09-25 14:31:26.595: I/liyufei(12453): we try to connect.....
09-25 14:31:26.900: D/PhoneStatusBar(389): disable: < expand* icons alerts ticker system_info back home recent clock >
09-25 14:31:26.970: D/dalvikvm(520): GC_FOR_ALLOC freed 140K, 44% free 14337K/25415K, paused 20ms, total 20ms
09-25 14:31:26.986: I/dalvikvm-heap(520): Grow heap (frag case) to 17.297MB for 3409936-byte allocation
09-25 14:31:27.017: D/dalvikvm(520): GC_CONCURRENT freed <1K, 31% free 17667K/25415K, paused 13ms+3ms, total 32ms
09-25 14:31:27.017: D/dalvikvm(520): WAIT_FOR_CONCURRENT_GC blocked 19ms
09-25 14:31:31.720: I/liyufei(12453): connect to server failed...1
09-25 14:31:31.720: W/System.err(12453): java.io.IOException: Host is down
09-25 14:31:31.720: W/System.err(12453): at android.bluetooth.BluetoothSocket.connectNative(Native Method)
09-25 14:31:31.720: W/System.err(12453): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:216)
09-25 14:31:31.720: W/System.err(12453): at com.example.android.beam.BeamBlueTooth$ConnectThread.run(BeamBlueTooth.java:297)
09-25 14:31:31.720: I/liyufei(12453): we try to connect.....
09-25 14:31:31.720: I/liyufei(12453): connect to server failed...2
09-25 14:31:31.720: W/System.err(12453): java.io.IOException: File descriptor in bad state
09-25 14:31:31.720: W/System.err(12453): at android.bluetooth.BluetoothSocket.connectNative(Native Method)
09-25 14:31:31.720: W/System.err(12453): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:216)
09-25 14:31:31.720: W/System.err(12453): at com.example.android.beam.BeamBlueTooth$ConnectThread.run(BeamBlueTooth.java:297)
09-25 14:31:31.720: I/liyufei(12453): we try to connect.....
09-25 14:31:31.728: I/liyufei(12453): connect to server failed...3
09-25 14:31:31.728: W/System.err(12453): java.io.IOException: File descriptor in bad state
09-25 14:31:31.728: W/System.err(12453): at android.bluetooth.BluetoothSocket.connectNative(Native Method)
09-25 14:31:31.728: W/System.err(12453): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:216)
09-25 14:31:31.728: W/System.err(12453): at com.example.android.beam.BeamBlueTooth$ConnectThread.run(BeamBlueTooth.java:297)