如果设备配对BluetoothServerSocket.accept()不返回连 [英] BluetoothServerSocket.accept() does not return even if device paired

查看:1972
本文介绍了如果设备配对BluetoothServerSocket.accept()不返回连的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android的蓝牙应用程序的基础上 BluetoothChat为例 。我开始一个蓝牙服务器和监听设备(不是手机)连接到我的应用程序在一个不安全的RFCOMM连接。

I am developing an Android bluetooth application based on the BluetoothChat exemple. i am starting a bluetooth server and listening for a device(not a phone) to connect to mine app on an insecure rfcomm connection.

private class AcceptThread extends Thread {
    // The local server socket
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread(boolean secure) {
        BluetoothServerSocket tmp = null;

        // Create a new listening server socket
        try {
            tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(mServiceName, MY_UUID_INSECURE);
        } catch (Exception e) {
            Log.e(TAG, ".AcceptThread # listen() failed", e);
        } 
        mmServerSocket = tmp;
    }

    public void run() {
        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
                Log.d(TAG, ".AcceptThread.run # ...accepting server socket conn");

                socket = mmServerSocket.accept(); //FIXME: it blocks here

                Log.d(TAG, ".AcceptThread.run # server socket connection accepted");
            } catch (Exception e) {
                MMLog.e(TAG, ".run # accept() failed: "+e);
                connectionFailed();
                break;
            }

            // If a connection was accepted
            if (socket != null) {
                synchronized (BluetoothService.this) {
                    switch (mState) {
                    case STATE_LISTEN:
                    case STATE_CONNECTING:
                        // starting the thread where i will receive input
                        // streams from the other device
                        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;
                    }
                }
            }
        }

    }

    public void cancel() {
        try {
            if(mmServerSocket != null) {
                mmServerSocket.close();
            }
        } catch (IOException e) {
            Log.e(TAG, ".cancel # Could not close server socket: ", e);
        }
    }
}

我使用的是HTC Desire S的,安卓2.3.5。该装置被配对,但我不接收数据,因为连接被挡在了.accept()方法。它只是不断的等待。

I am using a HTC Desire S, android 2.3.5. The device gets paired, but i don't receive data, because the connection gets blocked in the '.accept()' method. It just keeps on waiting.

插座= mmServerSocket.accept(); //...and等待

socket = mmServerSocket.accept(); //...and waiting

  • 为何仍等待,如果该设备配对?
  • 如何建立连接,因为我也尝试过思考,仍然没有结果
  • 是否与宏达电的蓝牙堆栈的问题吗?有没有人建立的连接可能使用其他的Andr​​oid手机?

推荐答案

有机会,这必须与你的其他设备(这是发生在我身上)。您的Andr​​oid已经做了这个工作是传入连接上市。有许多原因,您的特殊设备将不会启动正确的连接到​​你的Andr​​oid手机:

Chances are, this has to do with your other device (this is happening to me). Your Android already does its job which is listing for incoming connections. There are many reason why your special device won't initiate connections properly to your Android phone:

  • 在该装置神秘切换到其他的蓝牙配置文件如: HDP而不是SPP
  • 在该设备莫名其妙地记得有一个不同的Andr​​oid手机在内存中(上次连接或类似的东西),并不断尝试连接到手机,但不是你正在使用的是现在的人。

我想你最好的机会是要查询的详细规格和/或软件/驱动器的特殊设备的制造商/卖家给配置/测试。

I suppose your best chance is to query the manufacturer/ seller of the special device for detailed specifications and/or software/ driver to configure/ test it.

这篇关于如果设备配对BluetoothServerSocket.accept()不返回连的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆