Android 蓝牙 - 无法连接

发布于 2024-09-06 08:59:59 字数 1278 浏览 4 评论 0原文

我正在开发一个使用蓝牙连接到设备并发送/接收数据的应用程序。我正在使用 Nexus One 手机进行所有测试。

我从未能够建立从我的手机到任何设备的 SPP(串行端口)连接。但是,我已经能够使用相当于 PuTTY 的 Mac 从设备(我的笔记本电脑)连接到我的手机(唯一的例外是 Marketplace 中的“蓝牙文件传输”应用程序似乎工作,但我不认为使用 RFCOM/SPP...)。

我不断在 LogCat 日志中看到此消息:

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,并且还尝试使用:

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

方法而不是device.createRfcommSocketToServiceRecord(UUID); - 但没有运气。

我正在使用 BluetoothChat 示例 以及该代码的变体来完成所有操作我的测试...

解决方案或建议会很棒...或者甚至是我可以在手机上运行的一些测试代码的更好/不太复杂的示例,或者 python 脚本或我可以在计算机上运行以帮助调试的东西?

谢谢!我希望这不是 Android 操作系统的错误,但如果是的话,我希望找到解决方法。


编辑:我还应该注意到,大多数设备在蓝牙设置中显示为“已配对,但未连接”。


编辑2:解决方案似乎只是禁用任何蓝牙监听。请参阅我的回答帖子以获取更多信息。

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.

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...).

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)

as well as these:

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

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

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

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

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?

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


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


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相权↑美人 2024-09-13 08:59:59

事实证明,解决方案是禁用蓝牙服务的服务器功能。通过仅使用createRfcommSocketToServiceRecord并且从不调用listenUsingRfcommWithServiceRecord(在BluetoothChat示例中,这意味着从不启动“AcceptThread”),问题得到了解决。

尽管这两个调用应该是完全分开的并且彼此没有影响(根据 Android 文档),但只需注释掉 listenUsingRfcommWithServiceRecord 就可以解决我认为不相关的问题。

我可以使用未经编辑的蓝牙聊天程序,它将无法与我测试过的任何蓝牙设备(笔记本电脑、台式机、耳机等)建立传出连接,但如果我删除这一点,它作为客户端可以完美地工作。

无论如何,我希望这能帮助遇到同样问题的其他人。这肯定是 Android 操作系统的错误,也可能是 Nexus One 固件的错误。

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.

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.

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.

半世蒼涼 2024-09-13 08:59:59

我会忽略 stopDiscovery 错误 - 在建立连接之前取消发现是件好事。根据 SDK 文档:

因为发现是重量级的
蓝牙适配器的程序,
应始终调用此方法
在尝试连接到之前
使用 connect() 的远程设备。
发现不由
活动,但作为系统运行
服务,因此应用程序应该
始终调用取消发现,即使
它没有直接请求
发现,只是为了确定。

话虽如此,在对代码进行任何修改之前,您是否能够让蓝牙聊天示例正常工作?

您想要的 SPP/RFCOMM 的 UUID 是:

static UUID UUID_RFCOMM_GENERIC = new UUID(0x0000110100001000L,0x800000805F9B34FBL);

或以另一种方式定义(两者完成相同的事情)。

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

I would ignore the stopDiscovery error - its good that you're cancelling discovery before making your connection. Per the SDK docs:

Because discovery is a heavyweight
precedure for the Bluetooth adapter,
this method should always be called
before attempting to connect to a
remote device with connect().
Discovery is not managed by the
Activity, but is run as a system
service, so an application should
always call cancel discovery even if
it did not directly request a
discovery, just to be sure.

So with that said, were you able to get the Bluetooth Chat example to work before you made any modifications to the code?

The UUID you want for SPP/RFCOMM is:

static UUID UUID_RFCOMM_GENERIC = new UUID(0x0000110100001000L,0x800000805F9B34FBL);

or defined another way (both accomplish the same thing).

static final UUID UUID_RFCOMM_GENERIC = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文