错误:java.io. IO异常:读取失败,套接字可能已关闭或超时,读取ret:连接到BLE模块时(Kotlin)

f87krz0w  于 2022-12-23  发布在  Kotlin
关注(0)|答案(1)|浏览(166)

我尝试使用Kotlin应用程序连接RN 4870 BLE模块,但在尝试连接插座时不断收到错误。我可以将手机和BLE模块配对,并在Kotlin中验证它们是否正确绑定,但当我尝试运行这两条线路时。

btSocket = RN4780Device.createInsecureRfcommSocketToServiceRecord(RN4870_UUID)
btSocket.connect()

我总是得到这个错误:java.io.IOException:读取失败,套接字可能已关闭或超时,读取ret:-1
注:我能够使用另一个应用程序(SmartData iOS应用程序)发送和接收数据,因此我知道蓝牙固件工作正常。
在做了大量的研究之后,我只能假设我的UUID有问题,我不确定我是否应该使用我现在使用的通用UUID,或者以某种方式获得RN 4870模块正在使用的UUID。

主要活动:

class MainActivity : AppCompatActivity() {

    lateinit var btAdapter: BluetoothAdapter
    lateinit var RN4780Device: BluetoothDevice
    lateinit var btSocket: BluetoothSocket
    lateinit var btService: MyBluetoothService

    @SuppressLint("MissingPermission")
    @RequiresApi(Build.VERSION_CODES.S)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialize bluetooth adapter
        val btManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        btAdapter = btManager.adapter

        // Start home activity if connected to Bluetooth module
        btn_cont.setOnClickListener {

            if (checkPaired()) {
                // Connect client socket
                try {
                    btSocket = RN4780Device.createInsecureRfcommSocketToServiceRecord(RN4870_UUID)
                    btSocket.connect()
                } catch (e: IOException) {
                    e.printStackTrace()
                }

            }
        }
    }

    @SuppressLint("MissingPermission")
    private fun checkPaired() : Boolean {
        // Find RN4780 device and check bond state
        val device: BluetoothDevice = btAdapter.getRemoteDevice(RN4780_ADDRESS)
        if (device.bondState != BluetoothDevice.BOND_BONDED) {
            Toast.makeText(this, "RN4870 BLE Module is not Connected", Toast.LENGTH_LONG).show()
            return false
        }
        RN4780Device = device
        return true
    }

}

常数

object Constants {
    var RN4780_ADDRESS = "04:91:62:A1:7D:0D"
    var RN4870_UUID = UUID.fromString("49535343-8841-43F4-A8D4-ECBE34729BB3")
}

完整堆栈跟踪

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.geoboard, PID: 1294
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958) 
     Caused by: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
        at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:758)
        at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:772)
        at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:404)
        at com.example.geoboard.MainActivity.onCreate$lambda-0(MainActivity.kt:54)
        at com.example.geoboard.MainActivity.$r8$lambda$z_JWuogCboqpr2BmqKXIU9Cwb9g(Unknown Source:0)
        at com.example.geoboard.MainActivity$$ExternalSyntheticLambda2.onClick(Unknown Source:2)
        at android.view.View.performClick(View.java:7471)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
        at android.view.View.performClickInternal(View.java:7448)
        at android.view.View.access$3600(View.java:813)
        at android.view.View$PerformClick.run(View.java:28408)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:250)
        at android.app.ActivityThread.main(ActivityThread.java:7755)
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)

我已经看了几个小时的不同堆栈溢出的帖子,但无法找到解决方案。感谢任何帮助!

相关问题