Пример #1
0
void RemoteSelector::serviceDiscovered(const QBluetoothServiceInfo &serviceInfo)
{
#if 0
    qDebug() << "Discovered service on"
             << serviceInfo.device().name() << serviceInfo.device().address().toString();
    qDebug() << "\tService name:" << serviceInfo.serviceName();
    qDebug() << "\tDescription:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceDescription).toString();
    qDebug() << "\tProvider:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceProvider).toString();
    qDebug() << "\tL2CAP protocol service multiplexer:"
             << serviceInfo.protocolServiceMultiplexer();
    qDebug() << "\tRFCOMM server channel:" << serviceInfo.serverChannel();
#endif
    QMapIterator<QListWidgetItem *, QBluetoothServiceInfo> i(m_discoveredServices);
    while (i.hasNext()){
        i.next();
        if (serviceInfo.device().address() == i.value().device().address()){
            return;
        }
    }

    QString remoteName;
    if (serviceInfo.device().name().isEmpty())
        remoteName = serviceInfo.device().address().toString();
    else
        remoteName = serviceInfo.device().name();

    QListWidgetItem *item =
        new QListWidgetItem(QString::fromLatin1("%1 %2").arg(remoteName,
                                                             serviceInfo.serviceName()));

    m_discoveredServices.insert(item, serviceInfo);
    ui->remoteDevices->addItem(item);
}
Пример #2
0
void Tennis::serviceDiscovered(const QBluetoothServiceInfo &serviceInfo)
{
    qDebug() << "***** Discovered! " << serviceInfo.device().name() << serviceInfo.serviceName() << serviceInfo.serviceUuid();
    qDebug() << "Found one!" << serviceInfo.protocolServiceMultiplexer();
    m_discoveryAgent->stop();
    client->startClient(serviceInfo);
    QSettings settings("QtDF", "bttennis");
    settings.setValue("lastclient", serviceInfo.device().address().toString());
}
Пример #3
0
/*!
    Attempts to connect to the service described by \a service.

    The socket is opened in the given \a openMode. The \l socketType() may change
    depending on the protocol required by \a service.

    The socket first enters ConnectingState and attempts to connect to the device providing
    \a service. If a connection is established, QBluetoothSocket enters ConnectedState and
    emits connected().

    At any point, the socket can emit error() to signal that an error occurred.

    Note that most platforms require a pairing prior to connecting to the remote device. Otherwise
    the connection process may fail.

    \sa state(), disconnectFromService()
*/
void QBluetoothSocket::connectToService(const QBluetoothServiceInfo &service, OpenMode openMode)
{
    Q_D(QBluetoothSocket);

    if (state() != QBluetoothSocket::UnconnectedState && state() != QBluetoothSocket::ServiceLookupState) {
        qCWarning(QT_BT)  << "QBluetoothSocket::connectToService called on busy socket";
        d->errorString = QBluetoothSocket::tr("Trying to connect while connection is in progress");
        setSocketError(QBluetoothSocket::OperationError);
        return;
    }
#if defined(QT_QNX_BLUETOOTH) || defined(QT_ANDROID_BLUETOOTH)
    if (!d->ensureNativeSocket(service.socketProtocol())) {
        d->errorString = tr("Socket type not supported");
        setSocketError(QBluetoothSocket::UnsupportedProtocolError);
        return;
    }
    d->connectToService(service.device().address(), service.serviceUuid(), openMode);
#else
    if (service.protocolServiceMultiplexer() > 0) {
        if (!d->ensureNativeSocket(QBluetoothServiceInfo::L2capProtocol)) {
            d->errorString = tr("Unknown socket error");
            setSocketError(UnknownSocketError);
            return;
        }
        d->connectToService(service.device().address(), service.protocolServiceMultiplexer(), openMode);
    } else if (service.serverChannel() > 0) {
        if (!d->ensureNativeSocket(QBluetoothServiceInfo::RfcommProtocol)) {
            d->errorString = tr("Unknown socket error");
            setSocketError(UnknownSocketError);
            return;
        }
        d->connectToService(service.device().address(), service.serverChannel(), openMode);
    } else {
        // try doing service discovery to see if we can find the socket
        if(service.serviceUuid().isNull()){
            qCWarning(QT_BT) << "No port, no PSM, and no UUID provided, unable to connect";
            return;
        }
        qCDebug(QT_BT) << "Need a port/psm, doing discovery";
        doDeviceDiscovery(service, openMode);
    }
#endif
}
Пример #4
0
void QBluetoothSocket::serviceDiscovered(const QBluetoothServiceInfo &service)
{
    Q_D(QBluetoothSocket);
    qCDebug(QT_BT) << "FOUND SERVICE!" << service;
    if (service.protocolServiceMultiplexer() > 0 || service.serverChannel() > 0) {
        connectToService(service, d->openMode);
        d->discoveryAgent->deleteLater();
        d->discoveryAgent = 0;
    } else {
        qCDebug(QT_BT) << "Could not find port/psm for potential remote service";
    }
}
Пример #5
0
void iControlPadClient::serviceDiscovered(const QBluetoothServiceInfo &serviceInfo)
{
    qDebug() << "Discovered service on"
             << serviceInfo.device().name() << serviceInfo.device().address().toString();
    qDebug() << "\tService name:" << serviceInfo.serviceName();
    qDebug() << "\tDescription:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceDescription).toString();
    qDebug() << "\tProvider:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceProvider).toString();
    qDebug() << "\tL2CAP protocol service multiplexer:"
             << serviceInfo.protocolServiceMultiplexer();
    qDebug() << "\tRFCOMM server channel:" << serviceInfo.serverChannel();

    if(  QString::compare ( serviceInfo.serviceName(), KiCPServiceName, Qt::CaseInsensitive ) == 0 )
        connectToService( serviceInfo );
}
Пример #6
0
void tst_QBluetoothServiceInfo::tst_assignment()
{
    QFETCH(QUuid, uuid);
    QFETCH(QBluetoothUuid::ProtocolUuid, protocolUuid);
    QFETCH(QBluetoothServiceInfo::Protocol, serviceInfoProtocol);

    const QString serviceName("My Service");
    const QBluetoothDeviceInfo deviceInfo(QBluetoothAddress("001122334455"), "Test Device", 0);

    QBluetoothServiceInfo serviceInfo;
    serviceInfo.setServiceName(serviceName);
    serviceInfo.setDevice(deviceInfo);

    QVERIFY(serviceInfo.isValid());

    {
        QBluetoothServiceInfo copyInfo = serviceInfo;

        QVERIFY(copyInfo.isValid());

        QCOMPARE(copyInfo.serviceName(), serviceName);
        QCOMPARE(copyInfo.device().address(), deviceInfo.address());
    }

    {
        QBluetoothServiceInfo copyInfo;

        QVERIFY(!copyInfo.isValid());

        copyInfo = serviceInfo;

        QVERIFY(copyInfo.isValid());

        QCOMPARE(copyInfo.serviceName(), serviceName);
        QCOMPARE(copyInfo.device().address(), deviceInfo.address());
    }

    {
        QBluetoothServiceInfo copyInfo1;
        QBluetoothServiceInfo copyInfo2;

        QVERIFY(!copyInfo1.isValid());
        QVERIFY(!copyInfo2.isValid());

        copyInfo1 = copyInfo2 = serviceInfo;

        QVERIFY(copyInfo1.isValid());
        QVERIFY(copyInfo2.isValid());

        QCOMPARE(copyInfo1.serviceName(), serviceName);
        QCOMPARE(copyInfo2.serviceName(), serviceName);
        QCOMPARE(copyInfo1.device().address(), deviceInfo.address());
        QCOMPARE(copyInfo2.device().address(), deviceInfo.address());
    }

    {
        QBluetoothServiceInfo copyInfo;
        QVERIFY(!copyInfo.isValid());
        copyInfo = serviceInfo;

        copyInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, uuid);
        QVERIFY(copyInfo.contains(QBluetoothServiceInfo::ProtocolDescriptorList));
        QVERIFY(copyInfo.isComplete());
        QVERIFY(copyInfo.attributes().count() > 0);

        copyInfo.removeAttribute(QBluetoothServiceInfo::ProtocolDescriptorList);
        QVERIFY(!copyInfo.contains(QBluetoothServiceInfo::ProtocolDescriptorList));
        QVERIFY(!copyInfo.isComplete());
    }

    {
        QBluetoothServiceInfo copyInfo;
        QVERIFY(!copyInfo.isValid());
        copyInfo = serviceInfo;

        QVERIFY(copyInfo.serverChannel() == -1);
        QVERIFY(copyInfo.protocolServiceMultiplexer() == -1);

        QBluetoothServiceInfo::Sequence protocolDescriptorList;
        QBluetoothServiceInfo::Sequence protocol;
        protocol << QVariant::fromValue(QBluetoothUuid(protocolUuid));
        protocolDescriptorList.append(QVariant::fromValue(protocol));
        protocol.clear();

        protocolDescriptorList.append(QVariant::fromValue(protocol));
        copyInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
                                 protocolDescriptorList);
        QVERIFY(copyInfo.serverChannel() == -1);
        QVERIFY(copyInfo.protocolServiceMultiplexer() != -1);
        QVERIFY(copyInfo.socketProtocol() == serviceInfoProtocol);
    }

    {
        QBluetoothServiceInfo copyInfo;
        QVERIFY(!copyInfo.isValid());
        copyInfo = serviceInfo;
        QVERIFY(!copyInfo.isRegistered());

        QVERIFY(copyInfo.registerService());
        QVERIFY(copyInfo.isRegistered());

        QVERIFY(copyInfo.unregisterService());
        QVERIFY(!copyInfo.isRegistered());
    }
}
Пример #7
0
void RemoteSelector::serviceDiscovered(const QBluetoothServiceInfo& serviceInfo) {
#if DEBUG_OUTPUT
    qDebug() << "Discovered service on"
             << serviceInfo.device().name() << serviceInfo.device().address().toString();
    qDebug() << "\tService name:" << serviceInfo.serviceName();
    qDebug() << "\tDescription:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceDescription).toString();
    qDebug() << "\tProvider:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceProvider).toString();
    qDebug() << "\tL2CAP protocol service multiplexer:"
             << serviceInfo.protocolServiceMultiplexer();
    qDebug() << "\tRFCOMM server channel:" << serviceInfo.serverChannel();
#endif

    QString remoteName;
    if (serviceInfo.device().name().isEmpty())
        remoteName = serviceInfo.device().address().toString();
    else
        remoteName = serviceInfo.device().name();

    QMutableMapIterator<int, QBluetoothServiceInfo> i(discoveredServices_);
    while (i.hasNext()){
        i.next();
        if (serviceInfo.device().address() == i.value().device().address()){
            i.setValue(serviceInfo);
            return;
        }
    }

    QString tooltip = QString("address: %1\nname: %2\nis trusted: %3\nis valid: %4\nservice: %5").arg(
                serviceInfo.device().address().toString(),
                serviceInfo.device().name(),
                trustedDevicelist.isTrusted(serviceInfo.device().address().toString()) ? "yes" : "no",
                serviceInfo.device().isValid() ? "yes" : "no",
                serviceInfo.serviceDescription());
    if (!trustedDevicelist.isTrusted(serviceInfo.device().address().toString()))
    {
        int row = ui->remoteDevices->rowCount();
        ui->remoteDevices->insertRow(row);
        QTableWidgetItem *item = new QTableWidgetItem(serviceInfo.device().address().toString());
        item->setToolTip(tooltip);
        ui->remoteDevices->setItem(row, 0, item);
        item = new QTableWidgetItem(serviceInfo.device().name());
        ui->remoteDevices->setItem(row, 1, item);
        item = new QTableWidgetItem(serviceInfo.serviceName());

        ui->remoteDevices->setItem(row, 2, item);

        QBluetoothLocalDevice::Pairing p;

        p = localDevice_->pairingStatus(serviceInfo.device().address());

        ui->remoteDevices->blockSignals(true);

        item = new QTableWidgetItem();
        if ((p&QBluetoothLocalDevice::Paired) || (p&QBluetoothLocalDevice::AuthorizedPaired))
            item->setCheckState(Qt::Checked);
        else
            item->setCheckState(Qt::Unchecked);
        ui->remoteDevices->setItem(row, 3, item);

        item = new QTableWidgetItem();
        if (p&QBluetoothLocalDevice::AuthorizedPaired)
            item->setCheckState(Qt::Checked);
        else
            item->setCheckState(Qt::Unchecked);
        ui->remoteDevices->setItem(row, 4, item);

        item = new QTableWidgetItem();
        if (trustedDevicelist.isTrusted(serviceInfo.device().address().toString()))
            item->setCheckState(Qt::Checked);
        else
            item->setCheckState(Qt::Unchecked);
        ui->remoteDevices->setItem(row, 5, item);

        ui->remoteDevices->blockSignals(false);


        discoveredServices_.insert(row, serviceInfo);
    }
}
Пример #8
0
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);    
    RfCommClient client;
    QBluetoothLocalDevice localDevice;
    MyThread mythread;
    
    QObject::connect(&client, SIGNAL(done()), &app, SLOT(quit()));

    QString address;
    QString port;
    QStringList args = QCoreApplication::arguments();
    
    if(args.length() >= 2){
        address = args.at(1);
        if(args.length() >= 3){
            port = args.at(2);
        }
    }

       // use previous value for client, stored earlier
//    if(address.isEmpty()){
//        QSettings settings("QtDF", "bttennis");
//        address = settings.value("lastclient").toString();
//    }

    // hard-code address and port number if not provided
    if(address.isEmpty()){
        address = "6C:9B:02:0C:91:D3";  // "J C7-2"
        port = QString("20");
    }
            
    if(!address.isEmpty()){
        qDebug() << "Connecting to" << address << port;
        QBluetoothDeviceInfo device = QBluetoothDeviceInfo(QBluetoothAddress(address), "", 
                QBluetoothDeviceInfo::MiscellaneousDevice);
        QBluetoothServiceInfo service;
        if (!port.isEmpty()) {
            QBluetoothServiceInfo::Sequence protocolDescriptorList;
            QBluetoothServiceInfo::Sequence protocol;
            protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
                     << QVariant::fromValue(port.toUShort());
            protocolDescriptorList.append(QVariant::fromValue(protocol));
            service.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
                                 protocolDescriptorList);
            qDebug() << "port" << port.toUShort() << service.protocolServiceMultiplexer();
        }
        else {
            service.setServiceUuid(QBluetoothUuid(serviceUuid));
        }
        service.setDevice(device);
        // delay so that server is in waiting state
        qDebug() << "Starting sleep";
        mythread.sleep(10);  // seconds
        qDebug() << "Finished sleeping";
        client.startClient(service);
    } else {
        qDebug() << "failed because address and/or port is missing " << address << port;
    }

    
    return app.exec();
}
void tst_QBluetoothServiceInfo::tst_construction()
{
    const QString serviceName("My Service");
    const QString alternateServiceName("Another ServiceName");
    const QBluetoothDeviceInfo deviceInfo(QBluetoothAddress("001122334455"), "Test Device", 0);
    const QBluetoothDeviceInfo alternatedeviceInfo(QBluetoothAddress("554433221100"), "Test Device2", 0);

    QList<QBluetoothUuid::ProtocolUuid> protUuids;
    //list taken from qbluetoothuuid.h
    protUuids << QBluetoothUuid::Sdp;
    protUuids << QBluetoothUuid::Udp;
    protUuids << QBluetoothUuid::Rfcomm;
    protUuids << QBluetoothUuid::Tcp;
    protUuids << QBluetoothUuid::TcsBin;
    protUuids << QBluetoothUuid::TcsAt;
    protUuids << QBluetoothUuid::Att;
    protUuids << QBluetoothUuid::Obex;
    protUuids << QBluetoothUuid::Ip;
    protUuids << QBluetoothUuid::Ftp;
    protUuids << QBluetoothUuid::Http;
    protUuids << QBluetoothUuid::Wsp;
    protUuids << QBluetoothUuid::Bnep;
    protUuids << QBluetoothUuid::Upnp;
    protUuids << QBluetoothUuid::Hidp;
    protUuids << QBluetoothUuid::HardcopyControlChannel;
    protUuids << QBluetoothUuid::HardcopyDataChannel;
    protUuids << QBluetoothUuid::HardcopyNotification;
    protUuids << QBluetoothUuid::Avctp;
    protUuids << QBluetoothUuid::Avdtp;
    protUuids << QBluetoothUuid::Cmtp;
    protUuids << QBluetoothUuid::UdiCPlain;
    protUuids << QBluetoothUuid::McapControlChannel;
    protUuids << QBluetoothUuid::McapDataChannel;
    protUuids << QBluetoothUuid::L2cap;

    {
        QBluetoothServiceInfo serviceInfo;

        QVERIFY(!serviceInfo.isValid());
        QVERIFY(!serviceInfo.isComplete());
        QVERIFY(!serviceInfo.isRegistered());
        QCOMPARE(serviceInfo.serviceName(), QString());
        QCOMPARE(serviceInfo.serviceDescription(), QString());
        QCOMPARE(serviceInfo.serviceProvider(), QString());
        QCOMPARE(serviceInfo.serviceUuid(), QBluetoothUuid());
        QCOMPARE(serviceInfo.serviceClassUuids().count(), 0);
        QCOMPARE(serviceInfo.attributes().count(), 0);
        QCOMPARE(serviceInfo.serverChannel(), -1);
        QCOMPARE(serviceInfo.protocolServiceMultiplexer(), -1);

        foreach (QBluetoothUuid::ProtocolUuid u, protUuids)
            QCOMPARE(serviceInfo.protocolDescriptor(u).count(), 0);
    }

    {
        QBluetoothServiceInfo serviceInfo;
        serviceInfo.setServiceName(serviceName);
        serviceInfo.setDevice(deviceInfo);

        QVERIFY(serviceInfo.isValid());
        QVERIFY(!serviceInfo.isComplete());
        QVERIFY(!serviceInfo.isRegistered());

        QCOMPARE(serviceInfo.serviceName(), serviceName);
        QCOMPARE(serviceInfo.device().address(), deviceInfo.address());

        QBluetoothServiceInfo copyInfo(serviceInfo);

        QVERIFY(copyInfo.isValid());
        QVERIFY(!copyInfo.isComplete());
        QVERIFY(!copyInfo.isRegistered());

        QCOMPARE(copyInfo.serviceName(), serviceName);
        QCOMPARE(copyInfo.device().address(), deviceInfo.address());


        copyInfo.setAttribute(QBluetoothServiceInfo::ServiceName, alternateServiceName);
        copyInfo.setDevice(alternatedeviceInfo);
        QCOMPARE(copyInfo.serviceName(), alternateServiceName);
        QCOMPARE(copyInfo.attribute(QBluetoothServiceInfo::ServiceName).toString(), alternateServiceName);
        QCOMPARE(serviceInfo.serviceName(), alternateServiceName);
        QCOMPARE(copyInfo.device().address(), alternatedeviceInfo.address());
        QCOMPARE(serviceInfo.device().address(), alternatedeviceInfo.address());

        foreach (QBluetoothUuid::ProtocolUuid u, protUuids)
            QCOMPARE(serviceInfo.protocolDescriptor(u).count(), 0);
        foreach (QBluetoothUuid::ProtocolUuid u, protUuids)
            QCOMPARE(copyInfo.protocolDescriptor(u).count(), 0);
    }
}
void tst_QBluetoothServiceInfo::tst_assignment()
{
    QFETCH(QUuid, uuid);
    QFETCH(QBluetoothUuid::ProtocolUuid, protocolUuid);
    QFETCH(QBluetoothServiceInfo::Protocol, serviceInfoProtocol);
    QFETCH(bool, protocolSupported);

    const QString serviceName("My Service");
    const QBluetoothDeviceInfo deviceInfo(QBluetoothAddress("001122334455"), "Test Device", 0);

    QBluetoothServiceInfo serviceInfo;
    serviceInfo.setServiceName(serviceName);
    serviceInfo.setDevice(deviceInfo);

    QVERIFY(serviceInfo.isValid());
    QVERIFY(!serviceInfo.isRegistered());
    QVERIFY(!serviceInfo.isComplete());

    {
        QBluetoothServiceInfo copyInfo = serviceInfo;

        QVERIFY(copyInfo.isValid());
        QVERIFY(!copyInfo.isRegistered());
        QVERIFY(!copyInfo.isComplete());

        QCOMPARE(copyInfo.serviceName(), serviceName);
        QCOMPARE(copyInfo.device().address(), deviceInfo.address());
    }

    {
        QBluetoothServiceInfo copyInfo;

        QVERIFY(!copyInfo.isValid());
        QVERIFY(!copyInfo.isRegistered());
        QVERIFY(!copyInfo.isComplete());

        copyInfo = serviceInfo;

        QVERIFY(copyInfo.isValid());
        QVERIFY(!copyInfo.isRegistered());
        QVERIFY(!copyInfo.isComplete());

        QCOMPARE(copyInfo.serviceName(), serviceName);
        QCOMPARE(copyInfo.device().address(), deviceInfo.address());
    }

    {
        QBluetoothServiceInfo copyInfo1;
        QBluetoothServiceInfo copyInfo2;

        QVERIFY(!copyInfo1.isValid());
        QVERIFY(!copyInfo1.isRegistered());
        QVERIFY(!copyInfo1.isComplete());
        QVERIFY(!copyInfo2.isValid());
        QVERIFY(!copyInfo2.isRegistered());
        QVERIFY(!copyInfo2.isComplete());

        copyInfo1 = copyInfo2 = serviceInfo;

        QVERIFY(copyInfo1.isValid());
        QVERIFY(!copyInfo1.isRegistered());
        QVERIFY(!copyInfo1.isComplete());
        QVERIFY(copyInfo2.isValid());
        QVERIFY(!copyInfo2.isRegistered());
        QVERIFY(!copyInfo2.isComplete());

        QCOMPARE(copyInfo1.serviceName(), serviceName);
        QCOMPARE(copyInfo2.serviceName(), serviceName);
        QCOMPARE(copyInfo1.device().address(), deviceInfo.address());
        QCOMPARE(copyInfo2.device().address(), deviceInfo.address());
    }

    {
        QBluetoothServiceInfo copyInfo;
        QVERIFY(!copyInfo.isValid());
        QVERIFY(!copyInfo.isRegistered());
        QVERIFY(!copyInfo.isComplete());
        copyInfo = serviceInfo;
        QVERIFY(copyInfo.contains(QBluetoothServiceInfo::ServiceName));

        copyInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, QBluetoothUuid(uuid));
        QVERIFY(copyInfo.contains(QBluetoothServiceInfo::ProtocolDescriptorList));
        QVERIFY(copyInfo.isComplete());
        QVERIFY(copyInfo.attributes().count() > 0);

        copyInfo.removeAttribute(QBluetoothServiceInfo::ProtocolDescriptorList);
        QVERIFY(!copyInfo.contains(QBluetoothServiceInfo::ProtocolDescriptorList));
        QVERIFY(!copyInfo.isComplete());
    }

    {
        QBluetoothServiceInfo copyInfo;
        QVERIFY(!copyInfo.isValid());
        copyInfo = serviceInfo;

        QVERIFY(copyInfo.serverChannel() == -1);
        QVERIFY(copyInfo.protocolServiceMultiplexer() == -1);

        QBluetoothServiceInfo::Sequence protocolDescriptorList;
        QBluetoothServiceInfo::Sequence protocol;
        protocol << QVariant::fromValue(QBluetoothUuid(protocolUuid));
        protocolDescriptorList.append(QVariant::fromValue(protocol));
        protocol.clear();

        protocolDescriptorList.append(QVariant::fromValue(protocol));
        copyInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
                                 protocolDescriptorList);
        if (serviceInfoProtocol == QBluetoothServiceInfo::L2capProtocol) {
            QVERIFY(copyInfo.serverChannel() == -1);
            QVERIFY(copyInfo.protocolServiceMultiplexer() != -1);
        } else if (serviceInfoProtocol == QBluetoothServiceInfo::RfcommProtocol) {
            QVERIFY(copyInfo.serverChannel() != -1);
            QVERIFY(copyInfo.protocolServiceMultiplexer() == -1);
        }

        QVERIFY(copyInfo.socketProtocol() == serviceInfoProtocol);
    }

    {
        QBluetoothServiceInfo copyInfo;

        QVERIFY(!copyInfo.isValid());
        copyInfo = serviceInfo;
        copyInfo.setServiceUuid(QBluetoothUuid::SerialPort);
        QVERIFY(!copyInfo.isRegistered());

        if (!QBluetoothLocalDevice::allDevices().count()) {
            QSKIP("Skipping test due to missing Bluetooth device");
        } else if (protocolSupported) {
            QBluetoothServer server(serviceInfoProtocol);
            QVERIFY(server.listen());
            QTRY_VERIFY_WITH_TIMEOUT(server.isListening(), 5000);
            QVERIFY(server.serverPort() > 0);

            QBluetoothServiceInfo::Sequence protocolDescriptorList;
            QBluetoothServiceInfo::Sequence protocol;
            protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap));

            if (serviceInfoProtocol == QBluetoothServiceInfo::L2capProtocol) {
                protocol << QVariant::fromValue(server.serverPort());
                protocolDescriptorList.append(QVariant::fromValue(protocol));
            } else if (serviceInfoProtocol == QBluetoothServiceInfo::RfcommProtocol) {
                protocolDescriptorList.append(QVariant::fromValue(protocol));
                protocol.clear();
                protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
                         << QVariant::fromValue(quint8(server.serverPort()));
                protocolDescriptorList.append(QVariant::fromValue(protocol));
            }

            serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
                                     protocolDescriptorList);

            QVERIFY(copyInfo.registerService());
            QVERIFY(copyInfo.isRegistered());
            QVERIFY(serviceInfo.isRegistered());
            QBluetoothServiceInfo secondCopy;
            secondCopy = copyInfo;
            QVERIFY(secondCopy.isRegistered());

            QVERIFY(secondCopy.unregisterService());
            QVERIFY(!copyInfo.isRegistered());
            QVERIFY(!secondCopy.isRegistered());
            QVERIFY(!serviceInfo.isRegistered());
            QVERIFY(server.isListening());
            server.close();
            QVERIFY(!server.isListening());
        }
    }
}
Пример #11
0
void RemoteSelector::serviceDiscovered(const QBluetoothServiceInfo &serviceInfo)
{
#if 0
    qDebug() << "Discovered service on"
             << serviceInfo.device().name() << serviceInfo.device().address().toString();
    qDebug() << "\tService name:" << serviceInfo.serviceName();
    qDebug() << "\tDescription:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceDescription).toString();
    qDebug() << "\tProvider:"
             << serviceInfo.attribute(QBluetoothServiceInfo::ServiceProvider).toString();
    qDebug() << "\tL2CAP protocol service multiplexer:"
             << serviceInfo.protocolServiceMultiplexer();
    qDebug() << "\tRFCOMM server channel:" << serviceInfo.serverChannel();
#endif

    QString remoteName;
    if (serviceInfo.device().name().isEmpty())
        remoteName = serviceInfo.device().address().toString();
    else
        remoteName = serviceInfo.device().name();

//    QListWidgetItem *item =
//        new QListWidgetItem(QString::fromLatin1("%1\t%2\t%3").arg(serviceInfo.device().address().toString(),
//                                                             serviceInfo.device().name(), serviceInfo.serviceName()));

    QMutableMapIterator<int, QBluetoothServiceInfo> i(m_discoveredServices);
    while(i.hasNext()){
        i.next();
        if(serviceInfo.device().address() == i.value().device().address()){
            i.setValue(serviceInfo);
            return;
        }
    }

    int row = ui->remoteDevices->rowCount();
    ui->remoteDevices->insertRow(row);
    QTableWidgetItem *item = new QTableWidgetItem(serviceInfo.device().address().toString());
    ui->remoteDevices->setItem(row, 0, item);
    item = new QTableWidgetItem(serviceInfo.device().name());
    ui->remoteDevices->setItem(row, 1, item);
    item = new QTableWidgetItem(serviceInfo.serviceName());

    ui->remoteDevices->setItem(row, 2, item);

    QBluetoothLocalDevice::Pairing p;

    p = m_localDevice->pairingStatus(serviceInfo.device().address());

    ui->remoteDevices->blockSignals(true);

    item = new QTableWidgetItem();
    if((p&QBluetoothLocalDevice::Paired) || (p&QBluetoothLocalDevice::AuthorizedPaired))
        item->setCheckState(Qt::Checked);
    else
        item->setCheckState(Qt::Unchecked);
    ui->remoteDevices->setItem(row, 3, item);

    item = new QTableWidgetItem();
    if(p&QBluetoothLocalDevice::AuthorizedPaired)
        item->setCheckState(Qt::Checked);
    else
        item->setCheckState(Qt::Unchecked);

    ui->remoteDevices->setItem(row, 4, item);

    ui->remoteDevices->blockSignals(false);


    m_discoveredServices.insert(row, serviceInfo);
}
Пример #12
0
Tennis::Tennis(QWidget *parent)
: QDialog(parent), ui(new Ui_Tennis), board(new Board), controller(new Controller), socket(0),
  m_discoveryAgent(new QBluetoothServiceDiscoveryAgent), m_handover(0)
{
    // start Bluetooth if not started
    QBluetoothLocalDevice *device = new QBluetoothLocalDevice();
    device->powerOn();
    delete device;
    device = 0;

    //! [Construct UI]
    ui->setupUi(this);

    isClient = false;
    isConnected = false;
    quickDiscovery = true;

#if defined (Q_OS_SYMBIAN) || defined(Q_OS_WINCE) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
    setWindowState(Qt::WindowMaximized);
#endif

    ui->pongView->setScene(board->getScene());    

    connect(ui->pongView, SIGNAL(mouseMove(int, int)), this, SLOT(mouseMove(int, int)));
    ui->pongView->setMouseTracking(false);

    connect(board, SIGNAL(ballCollision(Board::Edge)), controller, SLOT(ballCollision(Board::Edge)));
    connect(board, SIGNAL(scored(Board::Edge)), controller, SLOT(scored(Board::Edge)));
    connect(controller, SIGNAL(moveBall(int,int)), board, SLOT(setBall(int,int)));
    connect(this, SIGNAL(moveLeftPaddle(int)), board, SLOT(setLeftPaddle(int)));
    connect(this, SIGNAL(moveRightPaddle(int)), board, SLOT(setRightPaddle(int)));
    connect(controller, SIGNAL(score(int,int)), board, SLOT(setScore(int,int)));
    connect(controller, SIGNAL(fps(const QString&)), this, SLOT(fps(const QString&)));

    setFocusPolicy(Qt::WheelFocus);

    paddle_pos = (Board::Height-12)/2-Board::Paddle/2;
    endPaddlePos = paddle_pos;

    emit moveLeftPaddle(paddle_pos);
    board->setRightPaddle(paddle_pos);

    server = new TennisServer(this);

    connect(controller, SIGNAL(moveBall(int,int)), server, SLOT(moveBall(int,int)));
    connect(controller, SIGNAL(score(int,int)), server, SLOT(score(int,int)));
    connect(this, SIGNAL(moveLeftPaddle(int)), server, SLOT(moveLeftPaddle(int)));
    connect(server, SIGNAL(clientConnected(QString)), this, SLOT(serverConnected(QString)));
    connect(server, SIGNAL(clientDisconnected(QString)), this, SLOT(serverDisconnected()));
    connect(server, SIGNAL(moveRightPaddle(int)), board, SLOT(setRightPaddle(int)));
    connect(server, SIGNAL(lag(int)), this, SLOT(lagReport(int)));

    connect(server, SIGNAL(clientConnected(QString)), controller, SLOT(refresh()));

    server->startServer();

    client = new TennisClient(this);

    connect(client, SIGNAL(moveBall(int,int)), board, SLOT(setBall(int,int)));
    connect(client, SIGNAL(moveLeftPaddle(int)), board, SLOT(setLeftPaddle(int)));
    connect(client, SIGNAL(connected(QString)), this, SLOT(clientConnected(QString)));
    connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    connect(this, SIGNAL(moveRightPaddle(int)), client, SLOT(moveRightPaddle(int)));
    connect(client, SIGNAL(score(int,int)), board, SLOT(setScore(int,int)));
    connect(client, SIGNAL(lag(int)), this, SLOT(lagReport(int)));

    connect(this, SIGNAL(moveLeftPaddle(int)), controller, SLOT(moveLeftPaddle(int)));
    connect(this, SIGNAL(moveRightPaddle(int)), controller, SLOT(moveRightPaddle(int)));
    connect(server, SIGNAL(moveRightPaddle(int)), controller, SLOT(moveRightPaddle(int)));


//    ui->pongView->setBackgroundBrush(QBrush(Qt::white));
    ui->pongView->setCacheMode(QGraphicsView::CacheBackground);

    QNearFieldManager nearFieldManager;
    if (nearFieldManager.isAvailable()) {
        m_handover = new Handover(server->serverPort(), this);
        connect(m_handover, SIGNAL(bluetoothServiceChanged()), this, SLOT(nearFieldHandover()));

        connect(m_discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
                this, SLOT(serviceDiscovered(QBluetoothServiceInfo)));
        connect(m_discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished()));
    }


    m_discoveryAgent->setUuidFilter(QBluetoothUuid(serviceUuid));


    QString address;
    QString port;
    QStringList args = QCoreApplication::arguments();
    if(args.length() >= 2){
        address = args.at(1);
        if(args.length() >= 3){
            port = args.at(2);
        }
    }

    if(address.isEmpty()){
        QSettings settings("QtDF", "bttennis");
        address = settings.value("lastclient").toString();
    }

    if(!address.isEmpty()){
        qDebug() << "Connect to" << address << port;
        QBluetoothDeviceInfo device = QBluetoothDeviceInfo(QBluetoothAddress(address), "", QBluetoothDeviceInfo::ComputerDevice);
        QBluetoothServiceInfo service;
        if (!port.isEmpty()) {
            QBluetoothServiceInfo::Sequence protocolDescriptorList;
            QBluetoothServiceInfo::Sequence protocol;
            protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap))
                     << QVariant::fromValue(port.toUShort());
            protocolDescriptorList.append(QVariant::fromValue(protocol));
            service.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
                                 protocolDescriptorList);
            qDebug() << "port" << port.toUShort() << service.protocolServiceMultiplexer();
        }
        else {
            service.setServiceUuid(QBluetoothUuid(serviceUuid));
        }
        service.setDevice(device);
        client->startClient(service);
        board->setStatus("Connecting", 100, 25);
    } else if (nearFieldManager.isAvailable()) {
        board->setStatus(tr("Touch to play"), 100, 25);
    }

    setEnabled(true);

    paddleAnimation = new QPropertyAnimation(this, "paddlePos", this);
    paddleAnimation->setEasingCurve(QEasingCurve::InOutQuad);

    ui->pongView->installEventFilter(this);

}