java Android 蓝牙 - 无法连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3072776/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Bluetooth - Can't connect out
提问by idolize
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.
我正在开发一个应用程序,它使用蓝牙连接到设备并发送/接收数据。我正在使用 Nexus One 手机进行所有测试。
I have never been able to establish a SPP (serial port) connection from my phone to any device. However, I havebeen 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...).
我从来没有能够建立从我的手机到任何设备的 SPP(串行端口)连接。不过,我已经能够从一个设备(我的笔记本电脑)连接到使用Mac相当于腻子我的手机(唯一的例外是从市场上的“蓝牙文件传输”应用程序似乎是工作,但我不认为使用 RFCOM/SPP ......)。
I keep seeing this message in my LogCat logs:
我一直在 LogCat 日志中看到此消息:
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:
我试过使用“00001101-0000-1000-8000-00805F9B34FB”的UUID,我也试过使用:
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.
方法而不是device.createRfcommSocketToServiceRecord(UUID);- 没有运气。
I am using the BluetoothChat exampleand variations of that code to do all of my testing...
我正在使用BluetoothChat 示例和该代码的变体来进行所有测试...
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?
解决方案或建议会很棒……或者甚至是更好/更简单的一些测试代码示例,我可以在手机上运行,或者 python 脚本或我可以在我的计算机上运行以帮助调试的东西?
Thanks! I hope this isn't a bug with the Android OS, but if it is I hope to find a workaround.
谢谢!我希望这不是 Android 操作系统的错误,但如果是,我希望找到解决方法。
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.
编辑 2:解决方案似乎只是禁用任何蓝牙监听。有关更多信息,请参阅我的回答帖子。
回答by idolize
The solution, as it turns out, was to disable the server functionality of the Bluetooth service. By only using createRfcommSocketToServiceRecordand never calling listenUsingRfcommWithServiceRecord(in the BluetoothChat example this means never starting the "AcceptThread") the problem was fixed.
事实证明,解决方案是禁用蓝牙服务的服务器功能。通过只使用createRfcommSocketToServiceRecord而从不调用listenUsingRfcommWithServiceRecord(在 BluetoothChat 示例中,这意味着从不启动“AcceptThread”)问题得到解决。
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 listenUsingRfcommWithServiceRecordfixed my supposedly unrelated issue.
尽管这两个调用应该完全分开并且彼此没有影响(根据 Android 文档),只需注释掉就可以listenUsingRfcommWithServiceRecord解决我认为不相关的问题。
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.
无论如何,如果其他人遇到同样的问题,我希望这会对其有所帮助。这一定是 Android 操作系统的错误,或者可能是 Nexus One 上的固件。
回答by Brad Hein
I would ignore the stopDiscovery error - its good that you're cancelling discovery before making your connection. Per the SDK docs:
我会忽略 stopDiscovery 错误 - 在建立连接之前取消发现是件好事。根据 SDK 文档:
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.
因为发现是蓝牙适配器的一个重量级过程,所以在尝试使用 connect() 连接到远程设备之前,应始终调用此方法。Discovery 不是由 Activity 管理的,而是作为系统服务运行的,因此应用程序应该始终调用取消发现,即使它没有直接请求发现,只是为了确定。
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:
您想要的 SPP/RFCOMM 的 UUID 是:
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");

