Android 蓝牙 -> 服务发现失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2207975/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:58:09  来源:igfitidea点击:

Bluetooth -> service discovery failed

androidservicebluetootharduinodiscovery

提问by Kaiser

I'm writing an application that is able to communicate with my PC. I have used the Bluetooth functionalities of the SDK 2.1.

我正在编写一个能够与我的 PC 通信的应用程序。我使用了 SDK 2.1 的蓝牙功能。

I can find devices, get their MAC address, create an RFCOMMsocket, but when I start the connection, I get the following error message.

我可以找到设备,获取它们的 MAC 地址,创建一个RFCOMM套接字,但是当我开始连接时,我收到以下错误消息。

Service discovery failed.

服务发现失败。

  1. Is it because of the UUID, which is not the same on my application and on my PC?
  2. How can I get the correct UUID on my PC?
  1. 是不是因为UUID在我的应用程序和我的 PC 上不一样?
  2. 如何在我的 PC 上获得正确的 UUID?

If I write a such application, is my Nexus Onethe client or the server?

如果我编写了一个这样的应用程序,我的Nexus One是客户端还是服务器?

回答by philDev

OK, I got the same problem, but I think I can answer a few of your questions. If your PC opened an SPP Port, for example, by using "sdptool add --channel=3 SP" your Android phone would be the client opening a socket and connecting with this socket to the server (your PC or any other Bluetooth device, for example, GPS, mouse, etc.).

好的,我遇到了同样的问题,但我想我可以回答你的一些问题。如果您的 PC 打开了一个 SPP 端口,例如,通过使用“sdptool add --channel=3 SP”,您的 Android 手机将成为客户端打开一个套接字并使用该套接字连接到服务器(您的 PC 或任何其他蓝牙设备,例如,GPS、鼠标等)。

You can get this information by reading the article Bluetooth, the definition of a client did I take out of this article.

您可以通过阅读文章Bluetooth来获取此信息,我从这篇文章中取出了客户端的定义。

If you are opening an SPP Port on the server (your PC, etc.) you are offering a standard service and there is a special UUID assigned to this service. This would be: 00001101-0000-1000-8000-00805F9B34FBif I'm not mistaken.

如果您在服务器(您的 PC 等)上打开 SPP 端口,则您提供的是标准服务,并且会为该服务分配一个特殊的 UUID。这将是:00001101-0000-1000-8000-00805F9B34FB如果我没有记错的话。

So I think we should get a qualified answer from an Android developer. Or ask this question on Thursday during the IRC office hours.

所以我认为我们应该从 Android 开发者那里得到一个合格的答案。或者在周四 IRC 办公时间问这个问题。

回答by Dev Perfecular

Hint: If you are connecting to a Bluetooth serial board then try using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. However if you are connecting to an Android peer then please generate your own unique UUID.

提示:如果您连接到蓝牙串行板,请尝试使用众所周知的 SPP UUID 00001101-0000-1000-8000-00805F9B34FB。但是,如果您要连接到 Android 对等点,请生成您自己的唯一 UUID。

The Link

链接

回答by Yoann

I found a way to be sure that the UUID is found on the device. On the client side, before device.createRfcommSocketToServiceRecord(uuid);add:

我找到了一种方法来确保在设备上找到 UUID。在客户端,在device.createRfcommSocketToServiceRecord(uuid);添加之前:

Process process = Runtime.getRuntime().exec("su -c 'sdptool records " + device.getAddress() + "'");
process.waitFor();

The process is frozen during 20-30 seconds, because Android is fetching services. But after, if the server is in discovery mode (if the server is an Android phone), the connection succeeds every time!

该过程在 20-30 秒内被冻结,因为 Android 正在获取服务。但是之后,如果服务器处于发现模式(如果服务器是安卓手机),则每次都连接成功!

回答by Brad Hein

Although it could be your UUID, I would also suggest you make sure the Bluetooth MAC is accurate and upper-case.

虽然它可能是您的 UUID,但我还建议您确保蓝牙 MAC 是准确的和大写的。

I would also suggest running hcidump -Xin a terminal on the Linux box, and see if you can see any two-way traffic between your Linux machine and phone throughout the process.

我还建议hcidump -X在 Linux 机器上的终端中运行,看看在整个过程中是否可以看到 Linux 机器和手机之间的双向流量。

回答by chihying

I had the same problem when reconnecting SPP after the first connect.

在第一次连接后重新连接 SPP 时,我遇到了同样的问题。

This is because rfcomm channel=-1in BluetoothService.java. I added updateDeviceServiceChannelCache()to the end of fetchRemoteUuids(), and then it worked!

这是因为rfcomm channel=-1BluetoothService.java. 我添加updateDeviceServiceChannelCache()到 的末尾fetchRemoteUuids(),然后它起作用了!

回答by AbdiT

This thing happens mostly to HTC. I faced the same problem. If you first connected successfully but fail afterwards, just restart the phone and the service that failed will restart in doing so.

这件事主要发生在 HTC 身上。我遇到了同样的问题。如果您第一次连接成功但后来失败,只需重新启动手机,失败的服务就会重新启动。