BluetoothSocket.connect()抛出异常"读取失败" [英] BluetoothSocket.connect() throwing exception "read failed"

查看:816
本文介绍了BluetoothSocket.connect()抛出异常"读取失败"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到设备,通过蓝牙的项目。它曾经工作相当可靠,但现在它失败了 BluetoothSocket.connect()调用每次。 (好吧,我得到它的过程中成千上万尝试连接一次在4小时内)。大部分的code已被取自标准样品聊天code API中,除常见的修改在获得的BluetoothSocket 设备本身:

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. (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:

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

下面是感兴趣的方法,该方法被运行一次的BluetoothSocket 获得:

Here's the method of interest, which gets run once a BluetoothSocket is obtained:

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);
}

相关的日志条目(打印时同时呼吁异常被捕获连接())是这样的:

10月11号至30日:23:51.685:E / BluetoothChatService(2870):连接到   ZYNO-700091在00:06:66:42:8E:01失败:读取失败,插座有可能   封闭的,读RET:-1

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

此错误用于在偶尔上来。我有一个积极的重新连接系统 - 它基本上锻连接一遍又一遍,直到它连接,如果它是有史以来断开,它再次开始锤打。因此,它杀死的连接线,并开始从无到有不断。我认为有可能是一个问题出现 - 可能是多线程的,或者在处理插座清理/初始化。但是,如果是这样的话,我还是会想到第一次连接尝试成功,因为该系统不踢,直到有一个失败的连接尝试。

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.

我看着的<一个href="https://android.googlesource.com/platform/frameworks/base/+/9a7debe5857ffc7c71cfc4082b1b6d72a5cf81cd/core/java/android/bluetooth/BluetoothSocket.java">source code 抛出异常。这个问题似乎是,底层的InputStream 没有任何数据。当然,这不是一个真正的答案,朝它只是一个步骤。为什么会流有没有数据?

I looked into the source code throwing the exception. The issue seems to be that the underlying InputStream has no data. Of course, that's not really an answer, just a step towards it. Why would the stream have no data?

我试图保持开放的心态对潜在的问题。我收到了的BluetoothSocket 是否正确?这曾经是一个间歇性问题,现在是几乎不变的事实使我怀疑多线程,但是这是一个相对简单的话题中的Java相比,C ++ - 很难搞砸了,如果你知道自己在做什么。此外,大多数这种code(尤其是处理同步线程的部分)是直出的样品code。

I'm trying to keep an open mind about the potential issue. Am I getting the BluetoothSocket properly? 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. 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 ===========================

有发生,我认为这可能是由于操作系统升级(我对Galaxy Nexus的手机上运行 - 我有几个来测试)。所以,我打开一个新的手机4.0.4,它的工作!于是回去和测试上的两个原始的测试手机,无论是运行4.2,预期的失败我已经看到这一切的时候。奇怪的是,现在它适用于这些手机了。我想说我做了一些工作,使这项工作了,但我没有。我仍然迷惑,到现在还怀疑这件事是去工作时,我真的很需要它。

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). So I unpacked a new phone with 4.0.4 and it worked! 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. 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.

我不知道是否有某种方式连接使用4.0.4可能正确地设置服务器模块的状态的可能性,使得它容易接受4.2设备?在黑暗中只是一个镜头,我想...

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? Just a shot in the dark, I suppose...

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

UPDATE 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. Could it be to do with pairing? If it happens again, try unpairing from the device and pairing again.

这篇关于BluetoothSocket.connect()抛出异常&QUOT;读取失败&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆