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); }
/** * @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 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 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); } } }
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; }