Hello, Im trying to develop a simple bluetooth server. For now, Im trying to connect another android device to my own device using BlueTerm as a client and my own application as a server. The problem is that I allways recibe de message "Unable to connect" in Blueterm. The accept() method never return the BluetothSocket. This is my source code:
Code:
public class BluetoothServerMonitorActivity extends Activity {
private static final UUID MY_UUID = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
public BluetoothAdapter mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AcceptThread th=new AcceptThread();
th.start();
}
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
// Use a temporary object that is later assigned to mmServerSocket,
// because mmServerSocket is final
String NAME="BluetoothServerMonitor";
//UUID MY_UUID=UUID.randomUUID();
BluetoothServerSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) { }
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
Log.w("BluetoothServer:", "Starting");
socket = mmServerSocket.accept();
Log.w("BluetoothServer:", "Socket recived");
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
//manageConnectedSocket(socket);
try {
mmServerSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
/** Will cancel the listening socket, and cause the thread to finish */
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) { }
}
}
}