基于Android蓝牙聊天示例的应用发现服务发现失败

我正在写一个基于android蓝牙聊天示例的蓝牙部分的蓝牙游戏。 我有两部手机可以测试。 问题是,当我将一部手机连接到另一部手机时,它有时会显示“无法连接设备”捆绑包,但是当我运行蓝牙聊天示例时,它从不显示此内容,所以我认为这不是设备的问题。 有没有人研究过蓝牙聊天样本,并且有同样的问题会给我一些帮助?

我尝试打印异常,就像“java.io.IOException:服务发现失败”。 这里是导致异常的代码。

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;
        BluetoothSocket tmp = null;

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "create() failed", e);
        }
        mmSocket = tmp;
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectThread");
        setName("ConnectThread");

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            mmSocket.connect();
        } catch (IOException e) {
            Log.e("error", e.toString());
            connectionFailed();
            // Close the socket
            try {
                mmSocket.close();
            } catch (IOException e2) {
                Log.e(TAG, "unable to close() socket during connection failure", e2);
            }
            // Start the service over to restart listening mode
            BluetoothChatService.this.start();
            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (BluetoothChatService.this) {
            mConnectThread = null;
        }

        // Start the connected thread
        connected(mmSocket, mmDevice);
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
} 

确切的位置是
mmSocket.connect();


我知道你会发现答案很奇怪,说实话我并不擅长UUID。 但我遇到了同样的问题,并使用它解决了问题。

回答
我想你使用的是在BluetoothChat应用程序中硬编码的UUID。 当我将这些UUID更改为众所周知的UUID时, “00001105-0000-1000-8000-00805F9B34FB”它解决了我的问题,并且不再发生IOException--Service Discovery Failed

所以,我建议你改变:

从:

private static final UUID MY_UUID_SECURE =
    UUID.fromString("ea87c0d0-afac-11de-8a39-0800200c9a66");
private static final UUID MY_UUID_INSECURE =
    UUID.fromString("7ce255c0-200a-11e0-ac64-0800200c9a66");


private static final UUID MY_UUID_SECURE = 
    UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");
private static final UUID MY_UUID_INSECURE = 
    UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");

来源到这个答案。

链接地址: http://www.djcxy.com/p/82015.html

上一篇: Service discovery failed with app based on android bluetooth chat sample

下一篇: Service Discovery Failed