void QDeclarativeBluetoothService::setRegistered(bool registered) { if (!d->m_componentComplete) { return; } delete d->m_server; d->m_server = 0; if (!registered) { d->m_service->unregisterService(); emit registeredChanged(); return; } d->listen(); connect(d->m_server, SIGNAL(newConnection()), this, SLOT(new_connection())); d->m_service->setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010); QBluetoothServiceInfo::Sequence classId; classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); d->m_service->setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); //qDebug() << "name/uuid" << d->m_name << d->m_uuid << d->m_port; d->m_service->setAttribute(QBluetoothServiceInfo::BrowseGroupList, QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; if (d->m_protocol == L2CapProtocol) { protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)) << QVariant::fromValue(quint16(d->m_server->serverPort())); protocolDescriptorList.append(QVariant::fromValue(protocol)); } else if (d->m_protocol == RfcommProtocol) { protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) << QVariant::fromValue(quint8(d->m_server->serverPort())); protocolDescriptorList.append(QVariant::fromValue(protocol)); } else { qWarning() << "No protocol specified for bluetooth service"; } d->m_service->setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); if (d->m_service->registerService()) { emit registeredChanged(); } else { qWarning() << "Register service failed"; //TODO propaget this error to the user } }
/*! This is a convenience function. Returns the protocol/service multiplexer for services which support the L2CAP protocol, otherwise returns -1. This function is equivalent to extracting the information from QBluetoothServiceInfo::Sequence returned by QBluetoothServiceInfo::attribute(QBluetoothServiceInfo::ProtocolDescriptorList). */ int QBluetoothServiceInfo::protocolServiceMultiplexer() const { QBluetoothServiceInfo::Sequence parameters = protocolDescriptor(QBluetoothUuid::L2cap); if (parameters.isEmpty()) return -1; else if (parameters.count() == 1) return 0; else return parameters.at(1).toUInt(); }
void TennisServer::startServer() { if (l2capServer) return; //! [Create the server] l2capServer = new QL2capServer(this); connect(l2capServer, SIGNAL(newConnection()), this, SLOT(clientConnected())); l2capServer->listen(); //! [Create the server] serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010); //! [Class ServiceClass must contain at least 1 entry] QBluetoothServiceInfo::Sequence classId; // classId << QVariant::fromValue(QBluetoothUuid(serviceUuid)); classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); //! [Class ServiceClass must contain at least 1 entry] //! [Service name, description and provider] serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Example Tennis Server")); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription, tr("Example bluetooth tennis server")); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("Nokia, QtDF")); //! [Service name, description and provider] //! [Service UUID set] serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid)); //! [Service UUID set] //! [Service Discoverability] serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); //! [Service Discoverability] //! [Protocol descriptor list] QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)) << QVariant::fromValue(quint16(l2capServer->serverPort())); protocolDescriptorList.append(QVariant::fromValue(protocol)); serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); //! [Protocol descriptor list] //! [Register service] serviceInfo.registerService(); //! [Register service] }
/*! Returns the protocol that the QBluetoothServiceInfo object uses. */ QBluetoothServiceInfo::Protocol QBluetoothServiceInfo::socketProtocol() const { QBluetoothServiceInfo::Sequence parameters = protocolDescriptor(QBluetoothUuid::Rfcomm); if (!parameters.isEmpty()) return RfcommProtocol; parameters = protocolDescriptor(QBluetoothUuid::L2cap); if (!parameters.isEmpty()) return L2capProtocol; return UnknownProtocol; }
/*! Returns a list of UUIDs describing the service classes that this service conforms to. This is a convenience function. It is equivalent to calling attribute(QBluetoothServiceInfo::ServiceClassIds).value<QBluetoothServiceInfo::Sequence>() and subsequently iterating over its QBluetoothUuid entries. \sa attribute() */ QList<QBluetoothUuid> QBluetoothServiceInfo::serviceClassUuids() const { QList<QBluetoothUuid> results; const QVariant var = attribute(QBluetoothServiceInfo::ServiceClassIds); if (!var.isValid()) return results; const QBluetoothServiceInfo::Sequence seq = var.value<QBluetoothServiceInfo::Sequence>(); for (int i = 0; i < seq.count(); i++) results.append(seq.at(i).value<QBluetoothUuid>()); return results; }
void QBluetoothServiceDiscoveryAgentPrivate::EndListL() { if (m_stack.size() > 1) { // finished a sequence or alternative add it to the parent sequence or alternative QVariant var = m_stack.pop(); if (m_stack.top().canConvert<QBluetoothServiceInfo::Sequence>()) { QBluetoothServiceInfo::Sequence *sequence = static_cast<QBluetoothServiceInfo::Sequence *>(m_stack.top().data()); sequence->append(var); } else if (m_stack.top().canConvert<QBluetoothServiceInfo::Alternative>()) { QBluetoothServiceInfo::Alternative *alternative = static_cast<QBluetoothServiceInfo::Alternative *>(m_stack.top().data()); alternative->append(var); } else { qWarning("Unknown type in the QVariant, should be either a QBluetoothServiceInfo::Sequence or an QBluetoothServiceInfo::Alternative"); } } }
void tst_QBluetoothServiceInfo::tst_serviceClassUuids() { QBluetoothServiceInfo info; QCOMPARE(info.serviceClassUuids().count(), 0); QBluetoothServiceInfo::Sequence classIds; classIds << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); QCOMPARE(classIds.count(), 1); QBluetoothUuid uuid(QString("e8e10f95-1a70-4b27-9ccf-02010264e9c8")); classIds.prepend(QVariant::fromValue(uuid)); QCOMPARE(classIds.count(), 2); QCOMPARE(classIds.at(0).value<QBluetoothUuid>(), uuid); info.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classIds); QList<QBluetoothUuid> svclids = info.serviceClassUuids(); QCOMPARE(svclids.count(), 2); QCOMPARE(svclids.at(0), uuid); QCOMPARE(svclids.at(1), QBluetoothUuid(QBluetoothUuid::SerialPort)); }
void Tennis::nearFieldHandover() { qDebug() << "Connecting to NFC provided address" << m_handover->bluetoothAddress().toString(); QBluetoothDeviceInfo device = QBluetoothDeviceInfo(m_handover->bluetoothAddress(), QString(), QBluetoothDeviceInfo::ComputerDevice); QBluetoothServiceInfo service; service.setServiceUuid(QBluetoothUuid(serviceUuid)); service.setDevice(device); QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)) << QVariant::fromValue(m_handover->serverPort()); protocolDescriptorList.append(QVariant::fromValue(protocol)); service.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); client->startClient(service); board->setStatus(tr("Connecting: %1 %2").arg(m_handover->bluetoothAddress().toString()).arg(m_handover->serverPort()), 100, 25); }
/** * @brief startServer * @param _localAdapter */ void server_unit::startServer( const QBluetoothAddress &_localAdapter ) { if( mRfcommServer ) { return; } // Create the server mRfcommServer = new QBluetoothServer( QBluetoothServiceInfo::RfcommProtocol, this ); this->connect( mRfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected_slot()) ); bool result = mRfcommServer->listen( _localAdapter ); if( !result ) { emit dbgMsg_signal("[SU] Cannot bind RFCOMM server to local adapter \n"); } // Set service info mServiceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Crichton Server")); mServiceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription, tr("Control machine service")); mServiceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("golems.org")); QBluetoothServiceInfo::Sequence publicBrowse; publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); mServiceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, publicBrowse); QBluetoothServiceInfo::Sequence classId; classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); mServiceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, classId); classId.prepend(QVariant::fromValue(QBluetoothUuid(serviceUuid))); mServiceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); mServiceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,classId); mServiceInfo.setServiceUuid(QBluetoothUuid(serviceUuid)); QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)); protocolDescriptorList.append(QVariant::fromValue(protocol)); protocol.clear(); protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) << QVariant::fromValue(quint8(mRfcommServer->serverPort())); protocolDescriptorList.append(QVariant::fromValue(protocol)); mServiceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); mServiceInfo.registerService( _localAdapter ); }
void ChatServer::startServer(const QBluetoothAddress& localAdapter) { if (rfcommServer) return; rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this); connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected())); bool result = rfcommServer->listen(localAdapter); if (!result) { qWarning() << "Cannot bind chat server to" << localAdapter.toString(); return; } //serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010); QBluetoothServiceInfo::Sequence classId; classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, classId); classId.prepend(QVariant::fromValue(QBluetoothUuid(serviceUuid))); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,classId); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Bt Chat Server")); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription, tr("Example bluetooth chat server")); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("qt-project.org")); serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid)); QBluetoothServiceInfo::Sequence publicBrowse; publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, publicBrowse); QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)); protocolDescriptorList.append(QVariant::fromValue(protocol)); protocol.clear(); protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) << QVariant::fromValue(quint8(rfcommServer->serverPort())); protocolDescriptorList.append(QVariant::fromValue(protocol)); serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); serviceInfo.registerService(localAdapter); }
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()); } }
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); }
void QBluetoothServiceDiscoveryAgentPrivate::VisitAttributeValueL(CSdpAttrValue &aValue, TSdpElementType aType) { qDebug() << "VisitAttributeValueL"; QVariant var; TUint datasize = aValue.DataSize(); switch (aType) { case ETypeNil: break; case ETypeUint: if (datasize == 8) { TUint64 value; aValue.Uint64(value); var = QVariant::fromValue(value); } else var = QVariant::fromValue(aValue.Uint()); break; case ETypeInt: var = QVariant::fromValue(aValue.Int()); break; case ETypeUUID: { TPtrC8 shortForm(aValue.UUID().ShortestForm()); if (shortForm.Size() == 2) { QBluetoothUuid uuid(ntohs(*reinterpret_cast<const quint16 *>(shortForm.Ptr()))); var = QVariant::fromValue(uuid); } else if (shortForm.Size() == 4) { QBluetoothUuid uuid(ntohl(*reinterpret_cast<const quint32 *>(shortForm.Ptr()))); var = QVariant::fromValue(uuid); } else if (shortForm.Size() == 16) { QBluetoothUuid uuid(*reinterpret_cast<const quint128 *>(shortForm.Ptr())); var = QVariant::fromValue(uuid); } break; } case ETypeString: { TPtrC8 stringBuffer = aValue.Des(); var = QVariant::fromValue(QString::fromLocal8Bit(reinterpret_cast<const char *>(stringBuffer.Ptr()), stringBuffer.Size())); break; } case ETypeBoolean: var = QVariant::fromValue(static_cast<bool>(aValue.Bool())); break; case ETypeDES: m_stack.push(QVariant::fromValue(QBluetoothServiceInfo::Sequence())); break; case ETypeDEA: m_stack.push(QVariant::fromValue(QBluetoothServiceInfo::Alternative())); break; case ETypeURL: { TPtrC8 stringBuffer = aValue.Des(); var = QVariant::fromValue(QUrl(QString::fromLocal8Bit(reinterpret_cast<const char *>(stringBuffer.Ptr()), stringBuffer.Size()))); break; } case ETypeEncoded: qWarning() << "Don't know how to handle encoded type."; break; default: qWarning() << "Don't know how to handle type" << aType; } if (aType != ETypeDES && aType != ETypeDEA) { if (m_stack.size() == 0) { // single value attribute, just push onto stack m_stack.push(var); } else if (m_stack.size() >= 1) { // sequence or alternate attribute, add non-DES -DEA values to DES or DEA if (m_stack.top().canConvert<QBluetoothServiceInfo::Sequence>()) { QBluetoothServiceInfo::Sequence *sequence = static_cast<QBluetoothServiceInfo::Sequence *>(m_stack.top().data()); sequence->append(var); } else if (m_stack.top().canConvert<QBluetoothServiceInfo::Alternative>()) { QBluetoothServiceInfo::Alternative *alternative = static_cast<QBluetoothServiceInfo::Alternative *>(m_stack.top().data()); alternative->append(var); } else { qWarning("Unknown type in the QVariant, should be either a QBluetoothServiceInfo::Sequence or an QBluetoothServiceInfo::Alternative"); } } } }
void QBluetoothServiceDiscoveryAgentPrivate::populateDiscoveredServices(const QBluetoothDeviceInfo &remoteDevice, const QList<QBluetoothUuid> &uuids) { /* Android doesn't provide decent SDP data. A list of uuids is close to meaning-less * * The following approach is chosen: * - If we see an SPP service class and we see * one or more custom uuids we match them up. Such services will always be SPP services. * - If we see a custom uuid but no SPP uuid then we return * BluetoothServiceInfo instance with just a servuceUuid (no service class set) * - Any other service uuid will stand on its own. * */ Q_Q(QBluetoothServiceDiscoveryAgent); //find SPP and custom uuid QBluetoothUuid uuid; int sppIndex = -1; QVector<int> customUuids; for (int i = 0; i < uuids.count(); i++) { uuid = uuids.at(i); if (uuid.isNull()) continue; //check for SPP protocol bool ok = false; quint16 uuid16 = uuid.toUInt16(&ok); if (ok && uuid16 == QBluetoothUuid::SerialPort) sppIndex = i; //check for custom uuid if (uuid.minimumSize() == 16) customUuids.append(i); } for (int i = 0; i < uuids.count(); i++) { if (i == sppIndex && !customUuids.isEmpty()) continue; QBluetoothServiceInfo serviceInfo; serviceInfo.setDevice(remoteDevice); QBluetoothServiceInfo::Sequence protocolDescriptorList; { QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)); protocolDescriptorList.append(QVariant::fromValue(protocol)); } if (customUuids.contains(i) && sppIndex > -1) { //we have a custom uuid of service class type SPP //set rfcomm protocol QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) << QVariant::fromValue(0); protocolDescriptorList.append(QVariant::fromValue(protocol)); //set SPP service class uuid QBluetoothServiceInfo::Sequence classId; classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, classId); classId.prepend(QVariant::fromValue(uuids.at(i))); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); serviceInfo.setServiceName(QBluetoothServiceDiscoveryAgent::tr("Serial Port Profile")); serviceInfo.setServiceUuid(uuids.at(i)); } else if (sppIndex == i && customUuids.isEmpty()) { //set rfcomm protocol QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) << QVariant::fromValue(0); protocolDescriptorList.append(QVariant::fromValue(protocol)); QBluetoothServiceInfo::Sequence classId; classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, classId); //also we need to set the custom uuid to the SPP uuid //otherwise QBluetoothSocket::connectToService() would fail due to a missing service uuid serviceInfo.setServiceUuid(uuids.at(i)); } else if (customUuids.contains(i)) { //custom uuid but no serial port serviceInfo.setServiceUuid(uuids.at(i)); } //Check if the UUID is in the uuidFilter if (!uuidFilter.isEmpty() && !uuidFilter.contains(serviceInfo.serviceUuid())) continue; serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); if (!customUuids.contains(i)) { //if we don't have custom uuid use it as class id as well QBluetoothServiceInfo::Sequence classId; classId << QVariant::fromValue(uuids.at(i)); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); QBluetoothUuid::ServiceClassUuid clsId = static_cast<QBluetoothUuid::ServiceClassUuid>(uuids.at(i).toUInt16()); serviceInfo.setServiceName(QBluetoothUuid::serviceClassToString(clsId)); } //don't include the service if we already discovered it before if (!isDuplicatedService(serviceInfo)) { discoveredServices << serviceInfo; //qCDebug(QT_BT_ANDROID) << serviceInfo; emit q->serviceDiscovered(serviceInfo); } } }
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()); } } }
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 QDeclarativeBluetoothService::setRegistered(bool registered) { d->m_needsRegistration = registered; if(!d->m_componentComplete){ return; } if(!registered) { if(!d->m_service) return; d->m_service->unregisterService(); emit registeredChanged(); } if(!d->m_service){ d->m_service = new QBluetoothServiceInfo(); } delete d->m_listen; d->m_listen = 0; d->listen(); connect(d->m_listen, SIGNAL(newConnection()), this, SLOT(new_connection())); d->m_service->setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010); // QBluetoothServiceInfo::Sequence classId; //// classId << QVariant::fromVhttp://theunderstatement.com/alue(QBluetoothUuid(serviceUuid)); // classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); // d->m_service->setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); d->m_service->setAttribute(QBluetoothServiceInfo::ServiceName, d->m_name); d->m_service->setAttribute(QBluetoothServiceInfo::ServiceDescription, d->m_description); d->m_service->setServiceUuid(QBluetoothUuid(d->m_uuid)); qDebug() << "name/uuid" << d->m_name << d->m_uuid << d->m_port; d->m_service->setAttribute(QBluetoothServiceInfo::BrowseGroupList, QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; qDebug() << "Port" << d->m_port; if(d->m_protocol == "l2cap"){ protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)) << QVariant::fromValue(quint16(d->m_port)); protocolDescriptorList.append(QVariant::fromValue(protocol)); } else if(d->m_protocol == "rfcomm"){ protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) << QVariant::fromValue(quint8(d->m_port)); protocolDescriptorList.append(QVariant::fromValue(protocol)); } else { qWarning() << "No protocol specified for bluetooth service"; } d->m_service->setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); if(d->m_service->registerService()) { qDebug() << "registered"; emit registeredChanged(); } else { qDebug() << "Failed"; } }
/*! Creates the RFCOMM server and starts to listen for the incoming connections. Returns true if the server was started successfully, false otherwise. */ bool BluetoothServer::startServer() { qDebug() << "Bluetoothserver::startServer(): =>"; if (mServiceUuid == 0) { qDebug() << "BluetoothServer::startServer(): No service information set!"; return false; } if (mRfcommServer) { qDebug() << "BluetoothServer::startServer(): Already started!"; return false; } mSockets.clear(); mLastErrorString = ""; Common::resetBuffer(); qDebug() << "Bluetoothserver::startServer(): Creating a server"; // Create the server mRfcommServer = new QRfcommServer(this); connect(mRfcommServer, SIGNAL(newConnection()), this, SLOT(onNewConnection())); qDebug() << "Bluetoothserver::startServer(): Server created"; #ifdef Q_WS_HARMATTAN if (!mRfcommServer->listen(QBluetoothAddress(), 11)) #else if (!mRfcommServer->listen()) #endif { qDebug() << "BluetoothServer::startServer():" << "Error: mRfcommServer is not listening!"; delete mRfcommServer; mRfcommServer = 0; return false; } qDebug() << "BluetoothServer::startServer(): Server is using port" << mRfcommServer->serverPort(); mServiceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010); #if !defined(Q_WS_SIMULATOR) && !defined(DISABLE_BLUETOOTH) // Class Uuuid must contain at least one entry QBluetoothServiceInfo::Sequence classId; classId << QVariant::fromValue(QBluetoothUuid(mServiceUuid)); mServiceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); #endif mServiceInfo.setServiceAvailability(1); qDebug() << "BluetoothServer::startServer(): Using service info:" << mServiceInfo.attribute(QBluetoothServiceInfo::ServiceName).toString() << mServiceInfo.attribute(QBluetoothServiceInfo::ServiceProvider).toString(); // Set the Service UUID set mServiceInfo.setServiceUuid(QBluetoothUuid(mServiceUuid)); // Set service discoverability mServiceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); #if !defined(Q_WS_SIMULATOR) && !defined(DISABLE_BLUETOOTH) // Protocol descriptor list QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; protocol.clear(); protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) << QVariant::fromValue(quint8(mRfcommServer->serverPort())); protocolDescriptorList.append(QVariant::fromValue(protocol)); mServiceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); #endif // Register the service if (mServiceInfo.registerService()) { qDebug() << "BluetoothServer::startServer():" << "Service registered. Waiting for clients to connect."; } else { qDebug() << "BluetoothServer::startServer():" << "Failed to register the service!"; delete mRfcommServer; mRfcommServer = 0; return false; } qDebug() << "Bluetoothserver::startServer(): <="; return true; }
int Main::buttonClicked() { QList<QBluetoothHostInfo> localAdapters = QBluetoothLocalDevice::allDevices(); printf("%s - localAdapters.count=%d\n", __func__, localAdapters.count()); for(int i = 0; i < localAdapters.size(); i++) { qDebug() << __func__ << " - adapter: " << i << " " << localAdapters[i].name().toUtf8().constData() << " " << localAdapters[i].address().toString(); } if (localAdapters.size() < 1) { qDebug() << __func__ << " no bluetooth found"; return -1; } QBluetoothLocalDevice adapter(localAdapters.at(0).address()); adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable); QBluetoothServer *rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, NULL); connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected())); QBluetoothAddress actualAddress = localAdapters[0].address(); printf("%s - QBluetoothServer - starting to listen at %s\n", __func__, actualAddress.toString().toUtf8().constData()); bool result = rfcommServer->listen(actualAddress); if (!result) { qDebug() << __func__ << "-" << result; return -1; } //! [Get local device name] QString localName = QBluetoothLocalDevice().name(); qDebug() << "local device name" << localName; //result = serviceInfo.registerService(/*actualAddress*/); #pragma region Attributes serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010); //! [Class Uuuid must contain at least 1 entry] QBluetoothServiceInfo::Sequence classId; classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort)); serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList, classId); printf("%s - done %d\n", __func__, 1); classId.prepend(QVariant::fromValue(QBluetoothUuid(serviceUuid))); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId); serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,classId); //! [Class Uuuid must contain at least 1 entry] printf("%s - done %d\n", __func__, 2); //! [Service name, description and provider] serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, tr("Bt Chat Server")); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription, tr("Example bluetooth chat server")); serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider, tr("qt-project.org")); //! [Service name, description and provider] //! [Service UUID set] serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid)); //! [Service UUID set] printf("%s - done %d\n", __func__, 3); //! [Service Discoverability] QBluetoothServiceInfo::Sequence publicBrowse; publicBrowse << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup)); serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList, publicBrowse); //! [Service Discoverability] printf("%s - done %d\n", __func__, 4); //! [Protocol descriptor list] QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)); protocolDescriptorList.append(QVariant::fromValue(protocol)); protocol.clear(); protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm)) << QVariant::fromValue(quint8(rfcommServer->serverPort())); protocolDescriptorList.append(QVariant::fromValue(protocol)); serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); //! [Protocol descriptor list] printf("%s - done %d\n", __func__, 5); //! [Register service] result = serviceInfo.registerService(actualAddress); if (!result) { printf("%s - registering with SDP failed\n", __func__); return -1; } //! [Register service] #pragma endregion printf("%s - done\n", __func__); return 0; }