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

jul*_*eam 7 java sockets android bluetooth ioexception

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

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

这是我的线程代码的一部分:

     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;
        }
Run Code Online (Sandbox Code Playgroud)

无论我尝试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)

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

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

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

Android ICS上的蓝牙连接无法实现

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

顺便说一句,我正在研究android ics 4.0.4.

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

jul*_*eam 5

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


san*_*ram 5

它为我工作

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();
Run Code Online (Sandbox Code Playgroud)