[英]java.io.IOException: bt socket is not in listen state [Android Marshmallow]
我正在嘗試在nexus 5(Android 6+)和Google Glass之間建立連接。 要連接的客戶端代碼:
try {
bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(
BluetoothParametersHolder.uuids[0]);
} catch (Exception e) {
e.printStackTrace();
}
//In a thread
bluetoothSocket.connect();
服務器端代碼:
mmServerSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(
BluetoothParametersHolder.NAME, BluetoothParametersHolder.uuids[0]);
//In a thread
socket = mmServerSocket.accept();
使用https://github.com/vicmns/BluetoothGlass作為參考。 我面臨的問題是,在accept()返回后,連接會立即與以下錯誤斷開連接:
02-15 16:20:42.769 2163-2414 /? 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不處於listen狀態02-15 16:20:43.762 8345-8361 /? W / System.err:在android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:453)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:在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:在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:在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)
我是藍牙和玻璃的新手。 任何幫助都非常感謝。 沒有得到現有帖子的幫助。 我在這里嘗試的是從客戶端(Glass)向服務器(Mobile)發送“hello”消息。
查看框架代碼,我們可以找到以下內容:
if (mSocketState != SocketState.LISTENING)
throw new IOException("bt socket is not in listen state");
顯然,如果套接字不處於LISTENING
狀態,方法accept()
會拋出異常。
在大多數情況下,如果BluetoothServerSocket
的實例已經關閉 ,則會發生這種情況。 在嘗試接受連接之前,請確保BluetoothServerSocket
未關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.