简体   繁体   English

BluetoothSocket.connect()引发异常“读取失败”

[英]BluetoothSocket.connect() throwing exception “read failed”

I have a project that connects to a device over Bluetooth. 我有一个通过蓝牙连接到设备的项目。 It used to work fairly reliably, but now it fails the BluetoothSocket.connect() call every time. 它曾经可以相当可靠地工作,但现在每次都失败了BluetoothSocket.connect()调用。 (Well, I got it to connect once during the thousands of attempts over a 4 hour period.) Most of the code has been taken from the standard sample chat code in the API, with the exception of the common modification in getting the BluetoothSocket device itself: (好吧,我在4小时的时间内进行了数千次尝试,使其连接一次。)大多数代码均取自API中的标准示例聊天代码,但在获取BluetoothSocket设备方面进行了常见修改本身:

Method m = device.getClass().getMethod(
              "createRfcommSocket", new Class[] { int.class });
tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));

Here's the method of interest, which gets run once a BluetoothSocket is obtained: 这是您感兴趣的方法,一旦获得BluetoothSocket ,它将立即运行:

public void run() {
  setName("ConnectThread" + mSocketType);

  // Always cancel discovery because it will slow down a connection
  mAdapter.cancelDiscovery();

  // Make a connection to the BluetoothSocket
  try {
    mmSocket.connect();
  } catch (Exception e) {
    Log.e(TAG, "Connection to " + mmDevice.getName() + " at "
                + mmDevice.getAddress() + " failed:" + e.getMessage());
    // Close the socket
    try {
        mmSocket.close();
    } catch (Exception e2) {
        Log.e(TAG, "unable to close() " + mSocketType
                + " socket during connection failure", e2);
    }
    connectionFailed(e.getMessage());
    return;
  }

  // Reset the ConnectThread because we're done
  synchronized (BluetoothChatService.this) {
    mConnectThread = null;
  }

      // Start the connected thread
  connected(mmSocket, mmDevice, mSocketType);
}

The relevant log entry (printed when the exception is caught while calling connect() ) is this: 相关的日志条目(在调用connect()时捕获异常时打印)是这样的:

11-30 10:23:51.685: E/BluetoothChatService(2870): Connection to ZYNO-700091 at 00:06:66:42:8E:01 failed:read failed, socket might closed, read ret: -1 11-30 10:23:51.685:E / BluetoothChatService(2870):在00:06:66:42:8E:01连接到ZYNO-700091失败:读取失败,套接字可能关闭,读取ret:-1

This error used to come up once in a while. 该错误曾经不时出现。 I have an aggressive reconnect system - it basically hammers the connection over and over until it connects, and if it were ever to disconnect, it starts hammering it again. 我有一个主动的重新连接系统-它基本上会反复敲打连接直到连接,如果断开连接,它将再次开始敲打。 So, it kills the connection thread and starts from scratch constantly. 因此,它会杀死连接线程并不断从头开始。 I had considered that there might be an issue there - maybe a multithreading one, or maybe in handling the socket cleanup/initialization. 我曾考虑过那里可能存在问题-可能是多线程问题,或者可能是在处理套接字清理/初始化过程中。 However, if that were the case, I'd still expect the first connection attempt to succeed, since that system doesn't kick in until there's a failed connection attempt. 但是,如果是这种情况,我仍然希望第一次连接尝试能够成功,因为该系统只有在连接尝试失败后才会启动。

I looked into the source code throwing the exception. 我调查了引发异常的源代码 The issue seems to be that the underlying InputStream has no data. 问题似乎是基础InputStream没有数据。 Of course, that's not really an answer, just a step towards it. 当然,这并不是真正的答案,只是迈出了一步。 Why would the stream have no data? 为什么流中没有数据?

I'm trying to keep an open mind about the potential issue. 我正在尝试对潜在问题持开放态度。 Am I getting the BluetoothSocket properly? 我可以正确获取BluetoothSocket吗? The fact that it was once an intermittent issue and is now nearly constant makes me suspect multithreading, but that's a relatively simple topic in Java compared to C++ - hard to screw up if you know what you're doing. 它曾经是一个间歇性问题,现在几乎恒定,这一事实使我怀疑多线程,但是与C ++相比,这在Java中是一个相对简单的话题-如果您知道自己在做什么,就很难搞清楚。 Plus, the majority of this code (in particular, the parts dealing with synchronizing the threads) is straight out of the sample code. 另外,此代码的大部分(特别是处理线程同步的部分)直接来自示例代码。

The device on the other end is an embedded Bluetooth device, so there's not much hope of debugging the problem from that end. 另一端的设备是嵌入式蓝牙设备,因此从那端调试问题的希望不大。

UPDATE =========================== 更新==========================

It occurred to me that it might be due to an OS upgrade (I'm running on Galaxy Nexus phones - I have several to test with). 在我看来,这可能是由于操作系统升级造成的(我在Galaxy Nexus手机上运行-我要测试一些手机)。 So I unpacked a new phone with 4.0.4 and it worked! 因此,我用4.0.4拆开了一部新手机的包装,它开始工作了! So then went back and tested on the two original test phones, both running 4.2, expecting the failure I've been seeing all this time. 因此,然后返回并在两台都运行4.2的原始测试电话上进行了测试,并期望我一直都在看到这种故障。 Strangely, now it works on those phones too. 奇怪的是,现在它也可以在那些手机上使用。 I'd like to say I did something to make this work again, but I didn't. 我想说我做了一些事情来再次使这项工作生效,但我没有。 I'm still mystified, and now also suspicious that this thing is going to work when I really need it to. 我仍然很迷惑,现在也怀疑当我真的需要它时,它会起作用。

I wonder if there's a possibility that somehow connecting using 4.0.4 could have properly set the state of the server module, making it receptive to the 4.2 devices? 我想知道是否有可能以某种方式使用4.0.4进行连接可以正确设置服务器模块的状态,从而使其能够接受4.2设备? Just a shot in the dark, I suppose... 我想只是在黑暗中开枪...

UPDATE 2 =========================== 更新2 ===========================

I've found that unpairing and re-pairing will allow the devices to connect. 我发现取消配对和重新配对将允许设备连接。 It's a workaround, but it's better than nothing. 这是一种解决方法,但总比没有好。

Jellybean has a completely different Bluetooth stack, so version differences could certainly be triggering something, but that in itself wouldn't explain why it stays working or not-working after connecting with an older device. Jellybean具有完全不同的蓝牙协议栈,因此版本差异肯定会触发某些事情,但这本身并不能解释为什么在连接较旧的设备后它仍然可以工作还是不能工作。 Could it be to do with pairing? 可能与配对有关吗? If it happens again, try unpairing from the device and pairing again. 如果再次发生,请尝试与设备解除配对并再次配对。

I know this is kind of an old question. 我知道这是一个老问题。 But as I was not able to find any solution on the web, here is a workaround I recently have created: IOException: read failed, socket might closed - Bluetooth on Android 4.3 但是由于我无法在网络上找到任何解决方案,因此这是我最近创建的解决方法: IOException:读取失败,套接字可能已关闭-Android 4.3上的蓝牙

I had the same problem while connecting to the arduino via bluetooth module.The problem only arised while connecting with arduino as it connected smoothly with another android phone bluetooth. 我通过蓝牙模块连接到arduino时遇到了同样的问题。问题仅在与arduino连接时出现,因为它与另一个android手机的蓝牙连接顺利。 What worked for me was changing the UUID string.. 对我有用的是更改UUID字符串。

In my case it was due to bad UUID in createRfcommSocketToServiceRecord() function. 就我而言,这是由于createRfcommSocketToServiceRecord()函数中的UUID错误造成的。 I want to connect to SPP serial profile in raspberry pi 3 and I used this UUID: 我想连接到树莓派3中的SPP串行配置文件,并且使用了以下UUID:

private static final UUID MY_UUID_SECURE =
            UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

I found somewhere in android documents' page for SPP. 我在Android文档的SPP页面中找到了某处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 bluetoothsocket.connect()在第二次运行时引发异常 - bluetoothsocket.connect() throws exception in the second run BluetoothSocket.connect()抛出IOException - BluetoothSocket.connect() throws IOException 应用程序挂在BluetoothSocket.connect()上,挂在InputStream.read()上,connect()后无法断开连接 - Application hangs on BluetoothSocket.connect(), hangs on InputStream.read(), can't disconnect after connect() Android BluetoothSocket.connect()引发IOExceptions“连接被拒绝”和“服务发现失败” - Android BluetoothSocket.connect() throws IOExceptions “Connection Refused” and “Service discovery failed” 我从来没有从BluetoothSocket.connect()中脱身 - I never get out from BluetoothSocket.connect() 带有Android 9的Samsung S9上BluetoothSocket.connect()失败 - BluetoothSocket.connect() fails on Samsung S9 with Android 9 Android AlertDialog 直到 bluetoothsocket.connect() 之后才会显示 - Android AlertDialog won't show until after bluetoothsocket.connect() 为什么在Toast.makeText()。show()之前调用BluetoothSocket.connect()? - Why is BluetoothSocket.connect() called before Toast.makeText().show()? BluetoothSocket连接失败 - BluetoothSocket connect failed Android BluetoothSocket :: connect()引发异常 - Android BluetoothSocket::connect() throws an exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM