Android BluetoothServerSocket's accept () mathos lanza IOException
Estoy usando el código de chat Bluetooth de muestra de Google con modificaciones para desarrollar una aplicación de chat Bluetooth. Me estoy poniendoIOException
de BluetoothServerSocket'saccept()
método. No sé por qué sucede esto. ¿Puede alguien ayudarme?. Estoy publicando parte de mi código ... Cada vez que el flujo entra en la parte de captura. Leí algunas respuestas pero no puedo obtener ninguna ayuda.
private class AcceptThread extends Thread {
// The local server socket
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
if(tmp != null)
Log.d("listening", this.getId()+" listening");
} catch (IOException e) {
Log.e(TAG, "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
setName("AcceptThread");
BluetoothSocket socket = null;
// Listen to the server socket if we're not connected
while (mState != STATE_CONNECTED) {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
} catch (IOException e) {
Log.d("errormsg", e.getMessage());
// Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(HomepageActivity.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(HomepageActivity.TOAST, "Remote device didn't accept the connection request");
msg.setData(bundle);
mHandler.sendMessage(msg);
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothChatService.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
Log.d("msg", "socket not null");
// Situation normal. Start the connected thread.
connected(socket, socket.getRemoteDevice());
break;
case STATE_NONE:
case STATE_CONNECTED:
// Either not ready or already connected. Terminate new socket.
try {
socket.close(),;
} catch (IOException e) {
Log.e(TAG, "Could not close unwanted socket", e);
}
break;
}
}
}
}
if (D) Log.i(TAG, this.getId()+"END mAcceptThread");
}
Logcat
08-28 19:34:04.161: E/BluetoothChatService(6449): accept() failed 08-28 19:34:04.161:
E/BluetoothChatService(6449): java.io.IOException: read failed,
socket might closed or timeout, read ret: -1 08-28 19:34:04.161:
E/BluetoothChatService(6449): at android.bluetooth.BluetoothSocket.
readAll(BluetoothSocket.java:553) 08-28 19:34:04.161:
E/BluetoothChatService(6449): at android.bluetooth.BluetoothSocket.
waitSocketSignal(BluetoothSocket.java:530) 08-28 19:34:04.161:
E/BluetoothChatService(6449): at
android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:440)