简体   繁体   English

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

[英]Android Bluetooth Connection - Service Discovery Failed

I'm trying to create a basic bluetooth application, for testing the device. 我正在尝试创建一个基本的蓝牙应用程序,用于测试设备。

I got the code from developer.android. 我从developer.android获得了代码。 Here is the link : http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingDevices 这是链接: 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(); 无论我尝试过什么mmSocket.connect(); never works. 永远不会奏效 Always throws an IOException and I get that log from my logcat: 总是抛出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)

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. 我知道这不是设备问题,因为我在不同的设备上尝试过这个应用程序。

I don't know and I still don't understand the UUID stuff but the problem was the UUID. 我不知道,我仍然不明白UUID的东西,但问题是UUID。 I'm using the UUID which I got from the kernel logs and it is 00001105-0000-1000-8000-00805F9B34FB . 我使用的是从内核日志中获取的UUID,它是00001105-0000-1000-8000-00805F9B34FB

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();

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);

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. 它用于通过蓝牙在Android设备和PC之间建立简单连接。 The examples contain the Android files as well as an SPP server in java and one in perl for the PC. 这些示例包含Android文件以及java中的SPP服务器和PC中的一个perl。

Hope this helps. 希望这可以帮助。

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解决同样的问题

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 看这里

Try using the Bluetooth Chat sample project provided as a part of the SDK if you are just trying to test the device. 如果您只是尝试测试设备,请尝试使用作为SDK一部分提供的蓝牙聊天示例项目。 That code you're trying to use and the one provided on developer.android.com are included in the Bluetooth Chat example. 您尝试使用的代码和developer.android.com上提供的代码包含在蓝牙聊天示例中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM