【问题标题】:Android : Bluetooth accept() failed errorAndroid:蓝牙accept()失败错误
【发布时间】:2012-04-07 09:40:24
【问题描述】:

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

我收到此错误 接受()失败 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;
    }

// 连接

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

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    已解决:必须使用反射

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

    【讨论】:

    • 这不是正确的答案...问题是 Windows 7 蓝牙堆栈不提供 spp 驱动程序。所以我们将不得不使用驱动程序附带的蓝牙加密狗。 ..
    猜你喜欢
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2011-12-04
    • 1970-01-01
    相关资源
    最近更新 更多