Android 蓝牙 - 无法连接 [英] Android Bluetooth - Can't connect out

查看:44
本文介绍了Android 蓝牙 - 无法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它使用蓝牙连接到设备并发送/接收数据.我正在使用 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(串行端口)连接.但是,我已经能够使用相当于 PuTTY 的 Mac 从设备(我的笔记本电脑)连接到我的手机(唯一的例外是 Marketplace 中的蓝牙文件传输"应用程序似乎可以工作,但我不认为使用 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));

method 而不是 device.createRfcommSocketToServiceRecord(UUID); - 没有运气.

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

我正在使用 BluetoothChat 示例 和该代码的变体做我所有的测试...

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

解决方案或建议会很棒……或者甚至是更好/更简单的一些测试代码示例,我可以在手机上运行,​​或者 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.

这篇关于Android 蓝牙 - 无法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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