简体   繁体   English

java.io.IOException:bt socket不在listen状态[Android Marshmallow]

[英]java.io.IOException: bt socket is not in listen state [Android Marshmallow]

I am trying to establish a connection between nexus 5(Android 6+) and Google Glass. 我正在尝试在nexus 5(Android 6+)和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. 使用https://github.com/vicmns/BluetoothGlass作为参考。 The problem I'm facing is that immediately after the accept() returns, connection gets disconnect with the following error: 我面临的问题是,在accept()返回后,连接会立即与以下错误断开连接:

02-15 16:20:42.769 2163-2414/? 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/? E / bt_btif_sock_rfcomm:find_rfc_slot_by_id无法找到RFCOMM插槽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:java.io.IOException:bt socket不处于listen状态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:在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:在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:在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: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 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:在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 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:在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.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.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) 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). 我在这里尝试的是从客户端(Glass)向服务器(Mobile)发送“hello”消息。

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 . 显然,如果套接字不处于LISTENING状态,方法accept()会抛出异常。

In most of the cases this happens if the instance of BluetoothServerSocket is already closed . 在大多数情况下,如果BluetoothServerSocket的实例已经关闭 ,则会发生这种情况。 Make sure the BluetoothServerSocket is not closed before trying to accept connections. 在尝试接受连接之前,请确保BluetoothServerSocket未关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM