4

I am trying to establish a connection between nexus 5(Android 6+) and Google Glass. Client side code to connect:

try {
    bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(
                BluetoothParametersHolder.uuids[0]);
} catch (Exception e) {
    e.printStackTrace();
}

//In a thread
bluetoothSocket.connect();

Server side code:

 mmServerSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(
                BluetoothParametersHolder.NAME, BluetoothParametersHolder.uuids[0]);

//In a thread
socket = mmServerSocket.accept();

Using https://github.com/vicmns/BluetoothGlass as a reference. The problem I'm facing is that immediately after the accept() returns, connection gets disconnect with the following error:

02-15 16:20:42.769 2163-2414/? E/bt_btif_sock_rfcomm: find_rfc_slot_by_id unable to find RFCOMM slot id: 51 02-15 16:20:43.761 8345-8361/? W/System.err: java.io.IOException: bt socket is not in listen state 02-15 16:20:43.762 8345-8361/? W/System.err: at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:453) 02-15 16:20:43.763 8345-8361/? W/System.err: at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:158) 02-15 16:20:43.763 8345-8361/? W/System.err: at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:144) 02-15 16:20:43.763 8345-8361/? W/System.err: at knowles.com.serverapp.handlers.BluetoothConnectionHandler.doInBackground(BluetoothConnectionHandler.java:41) 02-15 16:20:43.763 8345-8361/? W/System.err: at knowles.com.serverapp.handlers.BluetoothConnectionHandler.doInBackground(BluetoothConnectionHandler.java:16) 02-15 16:20:43.763 8345-8361/? W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295) 02-15 16:20:43.764 8345-8361/? W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) 02-15 16:20:43.764 8345-8361/? W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 02-15 16:20:43.764 8345-8361/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 02-15 16:20:43.764 8345-8361/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 02-15 16:20:43.782 8345-8361/? W/System.err: at java.lang.Thread.run(Thread.java:818)

I am new to Bluetooth and Glass. Any help is really appreciated. Didn't get much help from the existing posts. All I'm trying here is to send a 'hello' message from client(Glass) to server(Mobile).

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
Amit
  • 3,422
  • 3
  • 28
  • 39

1 Answers1

0

Looking inside the framework code, we can find following:

if (mSocketState != SocketState.LISTENING)
    throw new IOException("bt socket is not in listen state");

Clearly the method accept() throws exception if the socket is not in the state LISTENING.

In most of the cases this happens if the instance of BluetoothServerSocket is already closed. Make sure the BluetoothServerSocket is not closed before trying to accept connections.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59