QLluetoothSocket :: connectToService挂在ServiceLookupState上 - 为什么?

时间:2015-02-26 22:25:39

标签: c++ qt sockets bluetooth

我有一个应用程序使用Qt Bluetooth API在Android设备上将客户端连接(或应该连接)到蓝牙服务。这样做的代码并不复杂,并且与Qt Bluetooth Overview中给出的示例没有太大差别,尽管它是分布式的,因为它是类的一部分。

在构造函数中:

mBluetoothSocket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);

在“waitForServiceConnection”方法中:

    mBluetoothSocket->connectToService(address, uuid);

    for (unsigned i = 0; i < timeout; ++i)
    {
         QTime delayLimit = QTime::currentTime().addSecs(1);
         while( QTime::currentTime() < delayLimit ) QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
            qDebug() << "Current socket state is " << mBluetoothSocket->state() << endl;
            if (mServiceConnected)
            {
                    return;
            }
         }
     ...
     }

对于for循环的每次迭代(对于最多60秒的超时),我得到以下输出:

  

当前套接字状态是QBluetoothSocket :: ServiceLookupState

有一个socketStateChanged插槽,它接收来自套接字的stateChanged信号并包含:

qDebug() << "Socket state changed to " << socketState << endl;

输出:

  

套接字状态已更改为QBluetoothSocket :: ServiceLookupState

只有一次。如果我手动检查循环中的套接字状态,它也总是ServiceLookupState。

我很遗憾不是100%确定完全 ServiceLookupState需要什么。 Qt's documentation on the QBluetoothSocket class报告

  

Socket正在查询连接参数。

我怀疑这意味着要查找端口或其他东西。

桌面客户端使用QBluetoothServiceDiscoveryAgent成功找到在设备上注册的服务。我已经针对已知良好的蓝牙服务(Android BluetoothChat示例)对此进行了测试,结果相同,因此我非常确定问题出在桌面客户端上。我做错了什么?

不抛出任何异常,QBluetoothSocket :: error()始终表示没有错误。

1 个答案:

答案 0 :(得分:0)

代码本身没有任何问题 - 问题是它存在于某个单元测试中调用的库中。从标准的Qt控制台应用程序调用它时工作正常。

由于单元测试无法正确处理蓝牙组件,因此这是可以接受的。

相关问题