I am using code from the Google Bluetooth Chat Example to set up a
bluetooth listening server. I want the app to listen always for an
incoming connection. When the app sees a connection request it will
accept the connection, read in the string that the remote device will
send, and then respond by sending a file.
The problem is that the app never accepts the connection. It blocks at
socket = mmServerSocket.accept(); and never moves on. See the code
below after the line if(D) System.out.println("Waiting to
connect************"); To test it I start the activity that starts the
thread and then attempt to connect with my laptop and send a file.
When I do this the overall Android bluetooth manager sees the file and
downloads it effectively bypassing my Android device. Is this the only
way to test it? I cant figure out if its a testing or coding problem.
Code:
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);
} 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
if(D) System.out.println("Waiting to
connect************");
socket = mmServerSocket.accept();
if(D) System.out.println("We have accepted
connection and are connected***************");
} catch (IOException e) {
Log.e(TAG, "accept() failed", e);
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothServer.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// 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, "END mAcceptThread");
}
Any help would be greatly appreciated.
Thanks
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en