Ali Elgazar
Ali Elgazar

Reputation: 777

BluetoothServerSocket hangs on accept

I've seen many other posts regarding this specific issue, all with results that did not help me or were relevant to my case.

Here is my problem, I'm trying to setup a bluetooth piconet, with one node as a server and 7 as clients, each given a number as a location representative starting from 0 ( the server) and going to 7 (the clients). Currently I'm trying to get this to work for just two devices, the server and one client. And I assume that they're already paired. In the following code uuid is

private UUID uuid=UUID.fromString("0000111f-0000-1000-8000-00805f9b34fb");

Here is my thread which accepts incoming connections, ad is just the bluetooth adapter

 private class BTServerThread implements Runnable{

    @Override
    public void run() {
        try {
            if (ad != null) {
                if (ad.isEnabled()) {
                    BluetoothServerSocket btss=ad.listenUsingInsecureRfcommWithServiceRecord("MyApp",uuid);
                    Location=0;
                    Integer loc=1;
                    Log.wtf("Server","Searching");
                    while(loc<=7){
                        Thread t=new Thread(new BTServerHandler(btss.accept(),loc));
                        t.start();
                        Log.wtf("BTS","Found");
                        loc++;
                    }

                } else {
                    Log.e("error", "Bluetooth is disabled.");
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

And here is my client thread which attempts to connect

 private class BTClientThread implements Runnable{

    @Override
    public void run() {
        try {
            if (ad != null) {
                if (ad.isEnabled()) {
                    Set<BluetoothDevice> bondedDevices = ad.getBondedDevices();
                    if (bondedDevices.size() > 0) {
                        Iterator<BluetoothDevice> iter = bondedDevices.iterator();
                        BluetoothDevice device = iter.next();
                        Log.wtf("dev", device.getName());
                        BluetoothSocket clientsocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
                        clientsocket.connect();
                        Log.wtf("Connected",device.getName());
                    }

                    Log.e("error", "No appropriate paired devices.");
                } else {
                    Log.e("error", "Bluetooth is disabled.");
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

Hold your breath, here comes the weird part, the call btss.accept() hangs forever (Doesn't even return once), while at the same time, the client device connects somehow. When I call

BluetoothSocket clientsocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
                 clientsocket.connect()

This pops the toast on the phone which is hanging on accept saying "DeviceName connected" then after a while it pops another toast saying "DeviceName disconnected" on its own, without me doing ANYTHING, and at the same time the serverphone is still hanging on accept.

Here are my questions, why is it hanging on accept when the toast popped saying connected? And how could it possibly connect when the other phone is still listening for a connection?

Thanks for the help.

Upvotes: 1

Views: 587

Answers (1)

Ali Elgazar
Ali Elgazar

Reputation: 777

As it turns out, using that specific UUID caused a problem from some reason I still don't understand? After trying many random things, I finally decided to try another random UUID and that magically caused it to work. Here is my new UUID

private UUID uuid = UUID.fromString("56e8a14a-80b3-11e5-8bcf-feff819cdc9f");

Upvotes: 1

Related Questions