Android的蓝牙 - 无法连接了蓝牙、接了、Android

2023-09-03 22:46:05 作者:阳间幕宾

我开发它使用蓝牙连接的装置和发送/接收数据的应用程序。我做我所有的测试与Nexus One手机。

I am developing an application which uses Bluetooth to connect to a device and send/receive data. I am doing all of my testing with a Nexus One phone.

我从来没有能够建立从我的手机SPP(串行端口)连接到任何设备。不过,我有无能够从一个设备(我的笔记本电脑)连接到我的手机使用的是Mac相当于腻子的(唯一的例外是蓝牙文件传输从市场应用程序似乎工作,但我不认为使用RFCOM / SPP ...)。

I have never been able to establish a SPP (serial port) connection from my phone to any device. However, I have been able to connect from a device (my laptop) to my phone using a Mac equivalent of PuTTY (The only exception to this is the "Bluetooth File Transfer" app from the Marketplace seems to work, but I don't think that uses RFCOM/SPP...).

我总是看到这个消息我LogCat中的日志:

I keep seeing this message in my LogCat logs:

ERROR/BluetoothService.cpp(78): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)

以及这些

java.io.IOException: Operation Canceled
java.io.IOException: Software caused connection abort

我已经尝试使用00001101-0000-1000-8000-00805F9B34FB的UUID试过,我也尝试过使用的:

I have tried using the UUID of "00001101-0000-1000-8000-00805F9B34FB" and I have also tried using the:

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

方法,而不是 device.createRfcommSocketToServiceRecord(UUID); ,以及 - 没有运气

method instead of device.createRfcommSocketToServiceRecord(UUID); as well--with no luck.

我使用的是 BluetoothChat例如和该$ C变化$ C做我所有的测试...

I am using the BluetoothChat example and variations of that code to do all of my testing...

解决方案或建议将是巨大的......或者一些测试code,我可以在手机上运行,​​或者python脚本什么的,我可以在我的电脑上运行,以帮助调试甚至更好/更简单的例子吗?

Solutions or suggestions would be great...or even a better/less complex example of some testing code I can run on the phone, or a python script or something I can run on my computer to help debug?

谢谢!我希望这不是与Android操作系统的一个错误,但如果是我希望能找到一个解决办法。

Thanks! I hope this isn't a bug with the Android OS, but if it is I hope to find a workaround.

编辑:我还应该注意到,大多数设备显示为在蓝牙设置配对,但没有连接

I should also note that most devices show up as "paired, but not connected" in the Bluetooth settings.

编辑2:解决的办法似乎是简单地禁用任何蓝牙听。见我的回答后以了解更多信息。

EDIT 2: The solution seems to be simply disabling any Bluetooth listening. See my answer post for more information.

推荐答案

该解决方案,事实证明,是禁用蓝牙服务的服务器功能。只用 createRfcommSocketToServiceRecord ,从不叫 listenUsingRfcommWithServiceRecord (在BluetoothChat例如,这意味着永远不会启动AcceptThread)问题是固定的。

The solution, as it turns out, was to disable the server functionality of the Bluetooth service. By only using createRfcommSocketToServiceRecord and never calling listenUsingRfcommWithServiceRecord (in the BluetoothChat example this means never starting the "AcceptThread") the problem was fixed.

尽管这两个电话都应该是完全分离,(根据Android的文档)彼此不会产生任何影响,只是注释掉 listenUsingRfcommWithServiceRecord 固定我理应无关问题。

Even though these two calls are supposed to be totally separated and have no affect on each other (according to the Android docs), simply commenting out listenUsingRfcommWithServiceRecord fixed my supposedly unrelated issue.

我可以带蓝牙聊天程序未编辑,它将无法建立与任何蓝牙设备我已经测试(笔记本,台式机,耳机等)的出站连接,但如果我删除的一件事它完美的作品作为客户端

I can take the Bluetooth Chat program unedited and it will not be able to establish an outgoing connection to ANY bluetooth device I have tested (laptops, desktops, headsets, etc.), but if I remove that one thing it works flawlessly as a client.

无论如何,我希望这会帮助别人,如果他们遇到了同样的问题。这必须与Android操作系统的一个错误,或者可能是固件在Nexus One上。

Anyway, I hope this will help someone else if they come across the same issue. This must be a bug with the Android OS, or possibly the firmware on the Nexus One.