蓝牙设备连接被对等方重置

时间:2019-10-23 10:05:58

标签: android kotlin android-bluetooth

我的覆盆子pi零w,并且我正在尝试使用蓝牙进行连接。我上面有用kotlin编写的蓝牙“服务器”代码。

如果我使用第三方应用程序(串行蓝牙终端),则可以轻松连接到该应用程序并进行通信。但是,如果我尝试在自己的应用程序中复制它,则会得到Connection reset by peer

电话与覆盆子配对。

连接代码:

private class DeviceConnector : AsyncTask<Void, Void, Void>() {

    override fun doInBackground(vararg params: Void?): Void? {
        if(bluetoothSocket == null) {
            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

            val device : BluetoothDevice = bluetoothAdapter.getRemoteDevice(address)
            bluetoothSocket = device.createRfcommSocketToServiceRecord(UUID.randomUUID())
            bluetoothAdapter.cancelDiscovery()
            bluetoothSocket!!.connect()  //  <--- Exception happens here
        }
        return null
    }

}

完整堆栈跟踪:

Process: cz.abc.def, PID: 25157
java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:330)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:255)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:776)
 Caused by: java.io.IOException: Connection reset by peer
    at android.net.LocalSocketImpl.readba_native(Native Method)
    at android.net.LocalSocketImpl.-wrap1(LocalSocketImpl.java)
    at android.net.LocalSocketImpl$SocketInputStream.read(LocalSocketImpl.java:110)
    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:700)
    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:714)
    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:383)
    at cz.abc.def.ControlsActivity$DeviceConnector.doInBackground(ControlsActivity.kt:53)
    at cz.abc.def.ControlsActivity$DeviceConnector.doInBackground(ControlsActivity.kt:44)
    at android.os.AsyncTask$2.call(AsyncTask.java:316)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)

有人知道是什么问题吗?

编辑 现在由于某种原因,它给了我read failed, socket might closed or timeout, read ret: -1。我什么都没改变

    Process: cz.abc.def, PID: 32255
java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:330)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:255)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:776)
 Caused by: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:702)
    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:714)
    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:383)
    at cz.abc.def.ControlsActivity$DeviceConnector.doInBackground(ControlsActivity.kt:60)
    at cz.abc.def.ControlsActivity$DeviceConnector.doInBackground(ControlsActivity.kt:51)
    at android.os.AsyncTask$2.call(AsyncTask.java:316)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)

0 个答案:

没有答案