socket.connect()上的Android Bluetooth读取失败异常

socket.connect()上的Android Bluetooth读取失败异常,android,kotlin,bluetooth,elm327,Android,Kotlin,Bluetooth,Elm327,我正在尝试连接到android应用程序中的ELM327。遗憾的是,当尝试连接到从已配对设备创建的套接字时,我得到了一个“java.io.IOException:read failed,socket可能关闭或超时,read ret:-1”。我尝试了众所周知的修复方法,通过反射创建套接字,但仍然得到相同的异常。我还尝试了取消配对并再次配对设备,但没有成功。我有一个sdk目标29 API package com.example.gooddriving import android.app.Dial

我正在尝试连接到android应用程序中的ELM327。遗憾的是,当尝试连接到从已配对设备创建的套接字时,我得到了一个“java.io.IOException:read failed,socket可能关闭或超时,read ret:-1”。我尝试了众所周知的修复方法,通过反射创建套接字,但仍然得到相同的异常。我还尝试了取消配对并再次配对设备,但没有成功。我有一个sdk目标29 API

package com.example.gooddriving


import android.app.Dialog
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.github.pires.obd.commands.engine.RPMCommand
import com.google.android.material.bottomnavigation.BottomNavigationView


class DashboardActivity : BasicLayoutActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dashboard)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)

        textMessage = findViewById(R.id.message)
        navView.selectedItemId = R.id.navigation_dashboard
        navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)

        val bluetoothButton : Button = findViewById(R.id.bluetooth_button)
        bluetoothButton.setOnClickListener(onBluetoothButtonSelectedListener)
    }

    protected val onBluetoothButtonSelectedListener = View.OnClickListener {

        val bAdapter = BluetoothAdapter.getDefaultAdapter()
        val intVal : Int = 10
        if (!bAdapter.isEnabled) {
            val eintent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
            startActivityForResult(eintent, intVal)
        }

        // Get paired devices.
        val list : ArrayList<BluetoothDevice> = ArrayList()
        for( device: BluetoothDevice in bAdapter.bondedDevices) {
            list.add(device)
        }
        var dialog = Dialog(this)
        dialog.setContentView(R.layout.choose_bluetooth_device_dialog)
        val adapter = BluetoothDeviceAdapter(this, android.R.layout.simple_list_item_1, list)
        var listView = dialog.findViewById<ListView>(R.id.select_device_list)
        listView.adapter = adapter
        listView.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ ->
            val device: BluetoothDevice = list[position]
            handleDevice(device)
        }
        dialog.show()
    }

    protected fun handleDevice(device: BluetoothDevice) {
        val clazz: Class<*> = device.javaClass
        val paramTypes =
            arrayOf<Class<*>>(Integer.TYPE)

        val m = clazz.getMethod("createRfcommSocket", *paramTypes)
        val params = arrayOf<Any>(Integer.valueOf(1))

        var fallbackSocket = m.invoke(device, *params) as BluetoothSocket
        fallbackSocket.connect()
        val command = RPMCommand()
        command.run(fallbackSocket.inputStream, fallbackSocket.outputStream)
        Toast.makeText(this, command.result, command.result.length).show()

        fallbackSocket.close()
    }

    protected class BluetoothDeviceAdapter(context: Context, layout: Int, devices:MutableList<BluetoothDevice>) : ArrayAdapter<BluetoothDevice>(context, layout, devices) {
        override fun getView(pos: Int, convertView: View?, parent: ViewGroup): View {
            var listItem = convertView
            if (listItem == null) listItem =
                LayoutInflater.from(context).inflate(R.layout.device_layout, parent, false)
            val currentDevice = getItem(pos)

            val name = listItem!!.findViewById<View>(R.id.textView_name) as TextView
            name.text = currentDevice!!.name

            val address = listItem!!.findViewById<View>(R.id.textView_address) as TextView
            address.text = currentDevice!!.address

            return listItem!!
        }
    }

}
package com.example.gooddriving
导入android.app.Dialog
导入android.bluetooth.BluetoothAdapter
导入android.bluetooth.bluetooth设备
导入android.bluetooth.BluetoothSocket
导入android.content.Context
导入android.content.Intent
导入android.os.Bundle
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入android.widget*
导入com.github.pires.obd.commands.engine.RPMCommand
导入com.google.android.material.bottomnavigation.BottomNavigationView
类DashboardActivity:BasicLayoutActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_仪表板)
val navView:BottomNavigationView=findViewById(R.id.nav_视图)
textMessage=findviewbyd(R.id.message)
navView.selectedItemId=R.id.navigation\u仪表板
setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
val bluetoothButton:Button=findViewById(R.id.bluetooth_按钮)
setOnClickListener(onBluetoothButtonSelectedListener)
}
受保护的val onBluetoothButtonSelectedListener=View.OnClickListener{
val bAdapter=BluetoothAdapter.getDefaultAdapter()
val intVal:Int=10
如果(!bAdapter.isEnabled){
val eintent=Intent(BluetoothAdapter.ACTION\u REQUEST\u ENABLE)
startActivityForResult(电子内容,国际语言)
}
//获取配对设备。
val列表:ArrayList=ArrayList()
用于(设备:bAdapter.bondedDevices中的蓝牙设备){
列表。添加(设备)
}
var dialog=对话框(此)
setContentView(R.layout.choose\u bluetooth\u device\u对话框)
val adapter=BluetoothDeviceAdapter(这个,android.R.layout.simple_list_item_1,list)
var listView=dialog.findviewbyd(R.id.select\u device\u list)
listView.adapter=适配器
listView.onItemClickListener=AdapterView.onItemClickListener{{},{},位置,{->
val设备:蓝牙设备=列表[位置]
手持设备(装置)
}
dialog.show()
}
受保护的趣味手持设备(设备:蓝牙设备){
val clazz:Class=device.javaClass
val参数类型=
arrayOf(Integer.TYPE)
val m=clazz.getMethod(“createRfcommSocket”,*paramTypes)
val params=arrayOf(整数.valueOf(1))
var fallbackSocket=m.invoke(设备,*params)作为BluetoothSocket
fallbackset.connect()
val命令=RPMCommand()
运行(fallbackSocket.inputStream,fallbackSocket.outputStream)
Toast.makeText(this,command.result,command.result.length).show()
后备锁关闭()
}
受保护类BluetoothDeviceAdapter(上下文:上下文,布局:Int,设备:可变列表):ArrayAdapter(上下文,布局,设备){
覆盖视图(pos:Int,convertView:View?,父级:ViewGroup):视图{
var listItem=convertView
如果(listItem==null)listItem=
LayoutFlater.from(上下文)。充气(R.layout.device_布局,父级,false)
val currentDevice=getItem(位置)
val name=listItem!!.findViewById(R.id.textView\u name)作为textView
name.text=currentDevice!!.name
val address=listItem!!.findViewById(R.id.textView\u地址)作为textView
address.text=currentDevice!!.address
返回列表项!!
}
}
}