java Android 蓝牙连接 - 服务发现失败

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

Android Bluetooth Connection - Service Discovery Failed

javaandroidsocketsbluetoothioexception

提问by juliadream

I'm trying to create a basic bluetooth application, for testing the device.

我正在尝试创建一个基本的蓝牙应用程序,用于测试设备。

I got the code from developer.android. Here is the link : http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingDevices

我从 developer.android 得到了代码。这是链接:http: //developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingDevices

Here is run part of my thread code:

这是我的线程代码的运行部分:

     public void run() {

        mBluetoothAdapter.cancelDiscovery();
        Log.i(TAG, "Discovery Cancel!"); 

        try {
            Log.i(TAG, "Connection Started");
            mmSocket.connect();
            Log.i(TAG, "Connection Ended");
        } catch (IOException e) {
            try {
                Log.e(TAG, "Connection Failed", e);
                mmSocket.close();
            } catch (IOException e2) {
                Log.e(TAG, "Connection Close Failed", e2);
            }
            return;
        }

Whatever I have tried mmSocket.connect();never works. Always throws an IOException and I get that log from my logcat:

我尝试mmSocket.connect();过的任何方法都行不通。总是抛出一个 IOException 并且我从我的 logcat 得到那个日志:

java.io.IOException: Service discovery failed
at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:403)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:213)

java.io.IOException: Service discovery failed
at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:403)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:213)

I've looked at these articles, and tried the written things, none of them has solved my problem.

我看了这些文章,尝试了写的东西,没有一个解决了我的问题。

Android Bluetooth: Service Discovery Failed, connection to Desktop/Laptop

Android 蓝牙:服务发现失败,连接到台式机/笔记本电脑

Service discovery failed exception using Bluetooth on Android

在 Android 上使用蓝牙服务发现失败异常

Bluetooth connection on Android ICS not possible

无法在 Android ICS 上进行蓝牙连接

Android Bluetooth java.io.IOException: Connection refused?

Android 蓝牙 java.io.IOException:连接被拒绝?

Btw I'm working on android ics 4.0.4.

顺便说一句,我正在开发 android ics 4.0.4。

I know that is not device problem, cause I've tried this app on different devices.

我知道这不是设备问题,因为我已经在不同的设备上尝试过这个应用程序。

采纳答案by juliadream

I don't know and I still don't understand the UUID stuff but the problem was the UUID. I'm using the UUID which I got from the kernel logs and it is 00001105-0000-1000-8000-00805F9B34FB.

我不知道,我仍然不明白 UUID 的东西,但问题是 UUID。我正在使用从内核日志中获得的 UUID,它是00001105-0000-1000-8000-00805F9B34FB.

回答by sandeepmaaram

Its working for me

它对我有用

BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
bluetoothAdapter.cancelDiscovery();
socket.connect();

回答by Prasanth

The following code snippet works for me. Try it...

以下代码片段对我有用。试试看...

BluetoothDevice mmDevice;
boolean temp = mmDevice.fetchUuidsWithSdp();
UUID uuid = null;
if( temp ){
uuid = mmDevice.getUuids()[0].getUuid();
}
tmp = device.createRfcommSocketToServiceRecord(uuid);

回答by digitalhack

I worked through a similar learning process. I have tried to document what I learned in a series of examples.

我经历了类似的学习过程。我试图记录我在一系列例子中学到的东西。

This one might be of help:

这个可能有帮助:

http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-simple-spp.html

http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-simple-spp.html

It is for setting up a simple connection between an Android device and a PC via bluetooth. The examples contain the Android files as well as an SPP server in java and one in perl for the PC.

它用于通过蓝牙在 Android 设备和 PC 之间建立简单的连接。这些示例包含 Android 文件以及一个 java 格式的 SPP 服务器和一个用于 PC 的 perl 服务器。

Hope this helps.

希望这可以帮助。

回答by user1202714

Make sure that your app is not trying to connect while the adapter is busy with discovery: It appears the problem was that before I called

确保您的应用程序在适配器忙于发现时没有尝试连接:看来问题是在我打电话之前

clientSocket.connect()

I needed to call

我需要打电话

btAdapter.cancelDiscovery()

This helped solve the same problem for me Matts Reco

这帮助我解决了同样的问题Matts Reco

回答by nullpotent

You'll have to provide a valid UUID for the service discovery.

您必须为服务发现提供有效的 UUID。

BluetoothSocket sock = bdevice.createRfcommSocketToServiceRecord(VALID_UUID);

There are several common UUIDs for various standard (default) bluetooth services (Handsfree, File transfer, etc).

各种标准(默认)蓝牙服务(免提、文件传输等)有几种常见的 UUID。

See here

看这里

回答by Vishwa Patel

Try using the Bluetooth Chat sample project provided as a part of the SDK if you are just trying to test the device. That code you're trying to use and the one provided on developer.android.com are included in the Bluetooth Chat example.

如果您只是想测试设备,请尝试使用作为 SDK 一部分提供的蓝牙聊天示例项目。您尝试使用的代码和 developer.android.com 上提供的代码包含在蓝牙聊天示例中。