Android BluetoothServerSocket принимает () Mathos бросает IOException
Я использую образец кода чата Bluetooth Bluetooth с изменениями для разработки приложения чата Bluetooth. Я собираюсьIOException
от BluetoothServerSocket'saccept()
метод. Я не знаю, почему это происходит. Может кто-нибудь мне помочь?. Я публикую часть своего кода .. Каждый раз, когда поток собирается в часть catch. Я прочитал некоторые ответы, но не могу получить помощь.
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)