蓝牙 RFCOMM/SDP 连接到 android 中的 RS232 适配器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1953888/
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
Bluetooth RFCOMM / SDP connection to a RS232 adapter in android
提问by ThePosey
I am trying to use the Bluetooth Chat sample API app that google provides to connect to a bluetooth RS232 adapter hooked up to another device. Here is the app for reference:
我正在尝试使用谷歌提供的蓝牙聊天示例 API 应用程序连接到连接到另一台设备的蓝牙 RS232 适配器。这是应用程序供参考:
http://developer.android.com/resources/samples/BluetoothChat/index.html
http://developer.android.com/resources/samples/BluetoothChat/index.html
And here is the spec sheet for the RS232 connector just for reference:
这是 RS232 连接器的规格表,仅供参考:
http://serialio.com/download/Docs/BlueSnap-guide-4.77_Commands.pdf
http://serialio.com/download/Docs/BlueSnap-guide-4.77_Commands.pdf
Well the problem is that when I go to connect to the device with:
那么问题是,当我去连接到设备时:
mmSocket.connect(); (BluetoothSocket::connect())
I always get an IOException
error thrown by the connect()
method. When I do a toString
on the exception I get "Service discovery failed". My question is mostly what are the cases that would cause an IOException
to get thrown in the connect method? I know those are in the source somewhere but I don't know exactly how the java layer that you write apps in and the C/C++ layer that contains the actual stacks interface. I know that it uses the bluez bluetooth stack which is written in C/C++ but not sure how that ties into the java layer which is what I would think is throwing the exception. Any help on pointing me to where I can try to dissect this issue would be incredible.
我总是收到IOException
该connect()
方法抛出的错误。当我toString
对异常进行处理时,我得到“服务发现失败”。我的问题主要是哪些情况会导致IOException
在 connect 方法中抛出an ?我知道那些在源代码中的某个地方,但我不知道您编写应用程序的 java 层和包含实际堆栈接口的 C/C++ 层。我知道它使用了用 C/C++ 编写的 bluez 蓝牙堆栈,但不确定它与 java 层的关系,我认为这是抛出异常的原因。任何帮助我指出我可以尝试剖析这个问题的地方都会令人难以置信。
Also just to note I am able to pair with the RS232 adapter just fine but I am never able to actually connect. Here is the logcat output for more reference:
还要注意的是,我可以很好地与 RS232 适配器配对,但我永远无法实际连接。这是 logcat 输出以供更多参考:
I/ActivityManager( 1018): Displayed activity com.example.android.BluetoothChat/.DeviceListActivity: 326 ms (total 326 ms) E/BluetoothService.cpp( 1018): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session) D/BluetoothChat( 1729): onActivityResult -1 D/BluetoothChatService( 1729): connect to: 00:06:66:03:0C:51 D/BluetoothChatService( 1729): setState() STATE_LISTEN -> STATE_CONNECTING E/BluetoothChat( 1729): + ON RESUME + I/BluetoothChat( 1729): MESSAGE_STATE_CHANGE: STATE_CONNECTING I/BluetoothChatService( 1729): BEGIN mConnectThread E/BluetoothService.cpp( 1018): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session) E/BluetoothEventLoop.cpp( 1018): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/1498/hci0/dev_00_06_66_03_0C_51 I/BluetoothChatService( 1729): CONNECTION FAIL TOSTRING: java.io.IOException: Service discovery failed D/BluetoothChatService( 1729): setState() STATE_CONNECTING -> STATE_LISTEN D/BluetoothChatService( 1729): start D/BluetoothChatService( 1729): setState() STATE_LISTEN -> STATE_LISTEN I/BluetoothChat( 1729): MESSAGE_STATE_CHANGE: STATE_LISTEN V/BluetoothEventRedirector( 1080): Received android.bleutooth.device.action.UUID I/NotificationService( 1018): enqueueToast pkg=com.example.android.BluetoothChat callback=android.app.ITransientNotification$Stub$Proxy@446327c8 duration=0 I/BluetoothChat( 1729): MESSAGE_STATE_CHANGE: STATE_LISTEN E/BluetoothEventLoop.cpp( 1018): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/1498/hci0/dev_00_06_66_03_0C_51 V/BluetoothEventRedirector( 1080): Received android.bleutooth.device.action.UUID
The device I'm trying to connect to is the 00:06:66:03:0C:51
which I can scan for and apparently pair with just fine.
我尝试连接的设备是00:06:66:03:0C:51
我可以扫描的设备,并且显然可以很好地配对。
The below is merged from a similar question which was successfully resolved by the selected answer here:
以下是从一个类似的问题合并而来,该问题已由此处选择的答案成功解决:
How can one connect to an rfcomm device other than another phone in Android?
如何连接到 Android 中的另一部手机以外的 rfcomm 设备?
The Android API provides examples of using listenUsingRfcommWithServiceRecord()to set up a socket and createRfcommSocketToServiceRecord()to connect to that socket.
Android API 提供了使用listenUsingRfcommWithServiceRecord()设置套接字并使用createRfcommSocketToServiceRecord()连接到该套接字的示例。
I'm trying to connect to an embedded device with a BlueSMiRF Goldchip. My working Python code (using the PyBluezlibrary), which I'd like to port to Android, is as follows:
我正在尝试使用BlueSMiRF Gold芯片连接到嵌入式设备。我想移植到 Android 的工作 Python 代码(使用PyBluez库)如下:
sock = bluetooth.BluetoothSocket(proto=bluetooth.RFCOMM)
sock.connect((device_addr, 1))
return sock.makefile()
...so the service to connect to is simply defined as channel 1, without any SDP lookup.
...所以要连接的服务被简单地定义为通道 1,没有任何 SDP 查找。
As the only documented mechanism I see in the Android API does SDP lookup of a UUID, I'm slightly at a loss. Using "sdptool browse" from my Linux host comes up empty, so I surmise that the chip in question simply lacks SDP support.
由于我在 Android API 中看到的唯一记录机制是对 UUID 进行 SDP 查找,我有点不知所措。在我的 Linux 主机上使用“sdptool browse”是空的,所以我推测有问题的芯片只是缺乏 SDP 支持。
回答by ThePosey
Ok the short answer is I had to use this UUID in order to connect to my SPP device:
好吧,简短的回答是我必须使用这个 UUID 才能连接到我的 SPP 设备:
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
I tried to change it since I thought that only the "1101" part was important since I see that mentioned with SPP stuff all over the place on the intertubes but that made it not connect again. Apparently that specific UUID is what is supposed to be used to connect to generic SPP devices. Anyway just figured I'd post it up here so anyone who this sort of problem has an answer. Took me about 3 days to find it LOL!
我试图改变它,因为我认为只有“1101”部分很重要,因为我看到中间管上到处都是 SPP 的东西,但这使它无法再次连接。显然,特定的 UUID 应该用于连接到通用 SPP 设备。无论如何,只是想我会把它张贴在这里,这样任何有这种问题的人都有答案。我花了大约 3 天才找到它,哈哈!
回答by cheng81
I guess it is related to a sony-ericsson phones bug (see here).
我猜这与索尼爱立信手机的错误有关(请参阅此处)。
I was able to connect from/to an android 2.0 device and my mac using bluetooth and a totally made-up UUID. Trying to make the same thing with a j2me device (a sony ericsson w910i) was working only if the android was the server, otherwise I get the same exception as you.
我能够使用蓝牙和完全虚构的 UUID 从/连接到 android 2.0 设备和我的 mac。试图用 j2me 设备(索尼爱立信 w910i)做同样的事情只有当 android 是服务器时才有效,否则我会遇到和你一样的异常。
The UUID you are using, as far as I know, is a "base address" for the spp profile, and in the ServiceClassIDList field of the ServiceRecord returned by the server device when issuing a service discovery, it should be listed AFTER the UUID you decided to use..apparentely this is not the case in some situations (e.g. my phone first listed the generic UUID and then my custom UUID).
据我所知,您正在使用的 UUID 是 spp 配置文件的“基地址”,并且在发出服务发现时服务器设备返回的 ServiceRecord 的 ServiceClassIDList 字段中,它应该列在您的 UUID 之后决定使用..显然在某些情况下并非如此(例如,我的手机首先列出了通用 UUID,然后列出了我的自定义 UUID)。
Looks like it's the same situation here. You can try to manually change the ServiceRecord and return the proper ServiceClassIDList. Maybe it will work for you..unfortunately, my stupid cell phone refuse to change it :(
看起来这里的情况是一样的。您可以尝试手动更改 ServiceRecord 并返回正确的 ServiceClassIDList。也许它对你有用..不幸的是,我愚蠢的手机拒绝改变它:(
PS. a strange thing is that my mac is indeed able to see the service, even if the ServiceRecord is "broken", I guess that android just bother to see the first UUID in the ServiceClassIDList, while my pc go through the list searching every element. But this is just my supposition :)
附注。奇怪的是,我的 mac 确实能够看到该服务,即使 ServiceRecord 被“破坏”,我猜 android 只是懒得看到 ServiceClassIDList 中的第一个 UUID,而我的电脑则通过列表搜索每个元素。但这只是我的假设:)
回答by John
The baud rate you set must match the device it's hooked to. They have the default 115200 or switch to 9600 but if you need other (1200 in my case for a survey instrument) you need to set that up through hyperterminal* and a null modem cable.
您设置的波特率必须与其连接的设备相匹配。他们有默认的 115200 或切换到 9600,但如果您需要其他(在我的情况下是 1200 用于测量仪器),您需要通过超级终端 * 和空调制解调器电缆进行设置。
*Though the docs for the bluesnap device suggest using hyperterminal there are issues with it. After a few calls to bluesnap, they suggested:
*尽管 bluesnap 设备的文档建议使用超级终端,但它存在问题。在给 bluesnap 打了几次电话后,他们建议:
First, when connecting the device to a PC, try putting the Jumper settings back to their original positions using 115200 8, N, 1 and X off. When DTE enabled, a terminal connection cannot be established without using special software specifically designed for DTR/DTE connections.
首先,将设备连接到 PC 时,尝试使用 115200 8、N、1 和 X off 将跳线设置恢复到原始位置。启用 DTE 后,如果不使用专为 DTR/DTE 连接设计的特殊软件,则无法建立终端连接。
Second, HyperTerminal has known issues with the BlueSnap. I would recommend trying TeraTerm or PuTTY.
其次,超级终端在 BlueSnap 上存在已知问题。我建议尝试 TeraTerm 或 PuTTY。
took me 4 days to find this out!
我花了 4 天的时间才发现这一点!
回答by John
What baud rate do you have your bluetooth device set at? I'm connected, but my data is showing up as the typical garbled mush you get with mixed baud rates. I have mine set to 57600, this is what I've seen other folks use. Oh, thanks for posting your results the UUID had me working for a few days as well.
你的蓝牙设备设置的波特率是多少?我已连接,但我的数据显示为典型的混合波特率乱码。我的设置为 57600,这是我看到其他人使用的。哦,感谢您发布结果,UUID 也让我工作了几天。
回答by Gilead
If 'sdptool browse' reports no information about the device try 'sdptool records [device-mac-here]'
如果“sdptool browse”没有报告有关设备的信息,请尝试“sdptool记录[device-mac-here]”