Java-Android蓝牙IOException:读取失败,套接字可能关闭或超时,读取ret:-1

我在Android蓝牙API中连接客户端套接字时遇到问题.
我已经读过这些帖子:this onethis onethis onethis one
根据这些帖子,我使代码如下所示,但仍然无法正常工作:

class MainActivity : AppCompatActivity()
{
    private val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
    private var connectThread: ConnectThread? = null

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (bluetoothAdapter != null)
        {
            if (!bluetoothAdapter.isEnabled)
                bluetoothAdapter.enable()

            val devices = bluetoothAdapter.bondedDevices.toTypedArray()


            if (devices.isNotEmpty())
            {
                connectThread = ConnectThread(devices[0])
                connectThread!!.run()
            }
        }
        else
            Log.i("BluetoothTest", "No bluetooth supported")
    }

    override fun onDestroy()
    {
        super.onDestroy()

        connectThread!!.cancel()
    }

    private inner class ConnectThread(device: BluetoothDevice) : Thread()
    {
        //private val socket = device.createRfcommSocketToServiceRecord(UUID.fromString ("00001101-0000-1000-8000-00805F9B34FB"))
        private val socket = device.javaClass.getMethod("createRfcommSocket", (Int::class
            .javaPrimitiveType)).invoke(device, 2) as BluetoothSocket

        override fun run()
        {
            bluetoothAdapter.cancelDiscovery()
            socket.connect()
        }

        fun cancel()
        {
            socket.close()
        }

    }
}

这是socket.connect()上带有返回错误的日志:

--------- beginning of crash
05-15 15:59:19.553 24176-24176/com.bluetoothtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bluetoothtest, PID: 24176
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bluetoothtest/com.bluetoothtest.MainActivity}: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:685)
    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:697)
    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:374)
    at com.bluetoothtest.MainActivity.onCreate(MainActivity.kt:36)
    at android.app.Activity.performCreate(Activity.java:6999)
    at android.app.Activity.performCreate(Activity.java:6990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)

似乎由于插槽的端口值,Android 4.3之后出现了问题
我在Android 7.1和8.1上
有人知道如何在Android上使蓝牙工作吗?

最佳答案
最后,根本原因是因为无法读取套接字的inputStream.这是因为服务器端没有具有相同UUID的打开套接字.

查看https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r30/core/java/android/bluetooth/BluetoothSocket.java

因此,要解决此问题,我们需要在另一侧使用具有相同UUID的侦听套接字,对我来说,这是python中的树莓派:

from bluetooth import *

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

uuid = "YOUR UUID"

advertise_service( server_sock, "SampleServer", service_id = uuid, service_classes = [ uuid, SERIAL_PORT_CLASS ], profiles = [ SERIAL_PORT_PROFILE ])

print("Waiting for connection on RFCOMM channel %d" % port)

client_sock, client_info = server_sock.accept()
print("Accepted connection from ", client_info)

try:
    while True:
        data = client_sock.recv(1024)
        if len(data) == 0: break
        print("received [%s]" % data)
except IOError:
    pass

print("disconnected")

client_sock.close()
server_sock.close()
print("all done")

https://github.com/karulis/pybluez/blob/master/examples/simple/rfcomm-server.py

转载注明原文:Java-Android蓝牙IOException:读取失败,套接字可能关闭或超时,读取ret:-1 - 代码日志