Android:蓝牙接受()失败错误

时间:2012-04-07 09:40:25

标签: android bluetooth

我有一个连接电脑的Android应用程序。它适用于win xp,但不适用于Win 7.而在Windows 7中,如果我使用蓝牙适配器,它可以工作,但它不适用于我的笔记本电脑中的Bluetooth2.1软件。

我收到此错误 accept()失败了 04-07 14:58:56.354:E / BluetoothService.cpp(196):stopDiscoveryNative:StopDiscovery中的D-Bus错误:org.bluez.Error.Failed(无效的发现会话)

04-07 14:58:56.362:E / BluetoothEventLoop.cpp(196):onCreateDeviceResult:D-Bus错误:org.bluez.Error.AlreadyExists(已经存在)

代码:

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

//听

    public AcceptThread() {
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        try {
            tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
            //tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID);

        } catch (IOException e) {
            Log.e(TAG, "listen() failed", e);
        }
        mmServerSocket = tmp;
    }

// connect

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

            //tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
            Log.e(TAG, "create() failed", e);
        }
        mmSocket = tmp;
    }

    public void run() {
        if (D) Log.d(TAG, "BEGIN mAcceptThread" + this);
        setName("AcceptThread");
        BluetoothSocket socket = null;

        // Listen to the server socket if we're not connected
        while (mState != STATE_CONNECTED) {
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                socket = mmServerSocket.accept();
            } catch (IOException e) {
                Log.e(TAG, "accept() failed", e);
                break;
            }

            // If a connection was accepted
            if (socket != null) {
                synchronized (BluetoothService.this) {
                    switch (mState) {
                    case STATE_LISTEN:
                    case STATE_CONNECTING:
                        // Situation normal. Start the connected thread.
                        connected(socket, socket.getRemoteDevice());
                        break;
                    case STATE_NONE:
                    case STATE_CONNECTED:
                        // Either not ready or already connected. Terminate new socket.
                        try {
                            socket.close();
                        } catch (IOException e) {
                            Log.e(TAG, "Could not close unwanted socket", e);
                        }
                        break;
                    }
                }
            }
        }
        if (D) Log.i(TAG, "END mAcceptThread");
    }

1 个答案:

答案 0 :(得分:0)

解决:必须使用反射

m = device.getClass()。getMethod(“createRfcommSocket”,new Class [] {int.class});