void BtLocalDevice::serviceDiscovered(const QBluetoothServiceInfo &info) { QStringList classIds; foreach (const QBluetoothUuid &uuid, info.serviceClassUuids()) classIds.append(uuid.toString()); qDebug() << "$$ Found new service" << info.device().address().toString() << info.serviceUuid() << info.serviceName() << info.serviceDescription() << classIds; if (info.serviceUuid() == QBluetoothUuid(QString(TEST_SERVICE_UUID)) || info.serviceClassUuids().contains(QBluetoothUuid(QString(TEST_SERVICE_UUID)))) { //This is here to detect the test server for SPP testing later on bool alreadyKnown = false; foreach (const QBluetoothServiceInfo& found, foundTestServers) { if (found.device().address() == info.device().address()) { alreadyKnown = true; break; } } if (!alreadyKnown) { foundTestServers.append(info); qDebug() << "@@@@@@@@ Adding:" << info.device().address().toString(); } }
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 } }
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] }
//! [Connect to remote service] void Chat::connectClicked() { ui->connectButton->setEnabled(false); // scan for services const QBluetoothAddress adapter = localAdapters.isEmpty() ? QBluetoothAddress() : localAdapters.at(currentAdapterIndex).address(); RemoteSelector remoteSelector(adapter); remoteSelector.startDiscovery(QBluetoothUuid(serviceUuid)); if (remoteSelector.exec() == QDialog::Accepted) { QBluetoothServiceInfo service = remoteSelector.service(); qDebug() << "Connecting to service 2" << service.serviceName() << "on" << service.device().name(); // Create client qDebug() << "Going to create client"; ChatClient *client = new ChatClient(this); qDebug() << "Connecting..."; connect(client, SIGNAL(messageReceived(QString,QString)), this, SLOT(showMessage(QString,QString))); connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); connect(client, SIGNAL(connected(QString)), this, SLOT(connected(QString))); connect(this, SIGNAL(sendMessage(QString)), client, SLOT(sendMessage(QString))); qDebug() << "Start client"; client->startClient(service); clients.append(client); } ui->connectButton->setEnabled(true); }
void QDeclarativeBluetoothDiscoveryModel::setDiscovery(bool discovery_) { d->m_discovery = discovery_; if (!d->m_componentCompleted) return; d->m_working = false; d->m_agent->stop(); if(!discovery_) { emit discoveryChanged(); return; } if(!d->m_uuid.isEmpty()) d->m_agent->setUuidFilter(QBluetoothUuid(d->m_uuid)); d->m_working = true; if (d->m_minimal) { qDebug() << "Doing minimal"; d->m_agent->start(QBluetoothServiceDiscoveryAgent::MinimalDiscovery); } else d->m_agent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery); emit discoveryChanged(); }
void QBluetoothServerPrivate::controlEvent(ppsResult result) { Q_Q(QBluetoothServer); if (result.msg == QStringLiteral("service_connected")) { qCDebug(QT_BT_QNX) << "SPP: Server: Sending request for mount point path"; qCDebug(QT_BT_QNX) << result.dat; for (int i=0; i<result.dat.size(); i++) { qCDebug(QT_BT_QNX) << result.dat.at(i); } if (result.dat.contains(QStringLiteral("addr")) && result.dat.contains(QStringLiteral("uuid")) && result.dat.contains(QStringLiteral("subtype"))) { nextClientAddress = result.dat.at(result.dat.indexOf(QStringLiteral("addr")) + 1); m_uuid = QBluetoothUuid(result.dat.at(result.dat.indexOf(QStringLiteral("uuid")) + 1)); int subtype = result.dat.at(result.dat.indexOf(QStringLiteral("subtype")) + 1).toInt(); qCDebug(QT_BT_QNX) << "Getting mount point path" << m_uuid << nextClientAddress<< subtype; ppsSendControlMessage("get_mount_point_path", 0x1101, m_uuid, nextClientAddress, m_serviceName, this, BT_SPP_SERVER_SUBTYPE); } else { m_lastError = QBluetoothServer::InputOutputError; emit q->error(m_lastError); qCWarning(QT_BT_QNX) << Q_FUNC_INFO << "address not specified in service connect reply"; } } }
BluetoothMultiplexer::BluetoothMultiplexer(const QString &address, const QString &service_uuid, QObject *parent) : QObject(parent) , m_socket(NULL) , m_connections() , m_connections_nextid(1) , m_buffer_in_required(0) , m_buffer_in_data() , m_buffer_in_version(0) , m_buffer_in_type(0) , m_buffer_out_data() { qDebug() << this << "Creating"; m_buffer_out_data.clear(); m_socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this); connect(m_socket, &QBluetoothSocket::connected, this, &BluetoothMultiplexer::_connected); connect(m_socket, &QBluetoothSocket::disconnected, this, &BluetoothMultiplexer::_disconnected); connect(m_socket, &QBluetoothSocket::readyRead, this, &BluetoothMultiplexer::_read); connect(m_socket, &QBluetoothSocket::stateChanged, this, &BluetoothMultiplexer::_stateChanged); connect(m_socket, static_cast<void(QBluetoothSocket::*)(QBluetoothSocket::SocketError)>(&QBluetoothSocket::error), this, &BluetoothMultiplexer::_error); processNext(2, &BluetoothMultiplexer::_processInHeader); m_socket->connectToService(QBluetoothAddress(address), QBluetoothUuid(service_uuid)); }
void QLowEnergyControllerPrivate::servicesDiscovered( QLowEnergyController::Error errorCode, const QString &foundServices) { Q_Q(QLowEnergyController); if (errorCode == QLowEnergyController::NoError) { //Android delivers all services in one go const QStringList list = foundServices.split(QStringLiteral(" "), QString::SkipEmptyParts); foreach (const QString &entry, list) { const QBluetoothUuid service(entry); if (service.isNull()) return; QLowEnergyServicePrivate *priv = new QLowEnergyServicePrivate(); priv->uuid = service; priv->setController(this); QSharedPointer<QLowEnergyServicePrivate> pointer(priv); serviceList.insert(service, pointer); emit q->serviceDiscovered(QBluetoothUuid(entry)); } setState(QLowEnergyController::DiscoveredState); emit q->discoveryFinished(); } else {
/*! Returns the UUID of the characteristic if \l isValid() returns \c true; otherwise a \l {QUuid::isNull()}{null} UUID. */ QBluetoothUuid QLowEnergyCharacteristic::uuid() const { if (d_ptr.isNull() || !data || !d_ptr->characteristicList.contains(data->handle)) return QBluetoothUuid(); return d_ptr->characteristicList[data->handle].uuid; }
void QDeclarativeBluetoothService::setServiceUuid(QString uuid) { d->m_uuid = uuid; if(!d->m_service) d->m_service = new QBluetoothServiceInfo(); d->m_service->setAttribute(QBluetoothServiceInfo::ServiceId, QBluetoothUuid(uuid)); emit detailsChanged(); }
void iControlPadClient::discoverAndConnect(iCPReadableKeyEvent readKeys) { if (m_discoveryAgent->isActive()) m_discoveryAgent->stop(); m_readProperties = readKeys; m_discoveryAgent->setUuidFilter( QBluetoothUuid(QBluetoothUuid::SerialPort) ); m_discoveryAgent->start(); }
void tst_QBluetoothUuid::tst_comparison() { QFETCH(bool, constructUuid16); QFETCH(quint16, uuid16); QFETCH(bool, constructUuid32); QFETCH(quint32, uuid32); QFETCH(bool, constructUuid128); QFETCH(quint128, uuid128); QVERIFY(QBluetoothUuid() == QBluetoothUuid()); if (constructUuid16) { QBluetoothUuid quuid16(uuid16); QBluetoothUuid quuid32(uuid32); QBluetoothUuid quuid128(uuid128); QVERIFY(quuid16.toUInt16() == uuid16); QVERIFY(quuid16.toUInt32() == uuid32); QVERIFY(quuid16 == quuid32); QVERIFY(quuid16 == quuid128); QVERIFY(quuid32 == quuid16); QVERIFY(quuid128 == quuid16); } if (constructUuid32) { QBluetoothUuid quuid32(uuid32); QBluetoothUuid quuid128(uuid128); QVERIFY(quuid32.toUInt32() == uuid32); QVERIFY(quuid32 == quuid128); QVERIFY(quuid128 == quuid32); } if (constructUuid128) { QBluetoothUuid quuid128(uuid128); for (int var = 0; var < 16; ++var) { QVERIFY(quuid128.toUInt128().data[var] == uuid128.data[var]); } } }
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)); }
/*! Returns the UUID of this descriptor if \l isValid() returns \c true; otherwise a \l {QUuid::isNull()}{null} UUID. */ QBluetoothUuid QLowEnergyDescriptor::uuid() const { if (d_ptr.isNull() || !data || !d_ptr->characteristicList.contains(data->charHandle) || !d_ptr->characteristicList[data->charHandle]. descriptorList.contains(data->descHandle)) { return QBluetoothUuid(); } return d_ptr->characteristicList[data->charHandle].descriptorList[data->descHandle].uuid; }
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); }
void Bluetooth::requestConnection(void) { if (!m_socket) { return; } if (m_device->isValid()) { m_device->powerOn(); qDebug() << "Connecting..."; m_socket->connectToService(QBluetoothAddress(m_address), QBluetoothUuid(m_serviceUuid)); } }
void MainWindow::conectarServidor() { QString address(ui->comboBox->currentText()); QStringList lista = address.split(" "); if (lista.size() > 1){ QBluetoothAddress addr(lista.at(1)); qDebug() << lista.at(1); socket->connectToService(addr, QBluetoothUuid(QString("00001101-0000-1000-8000-00805F9B34FB")), QIODevice::ReadWrite); } }
void QBluetoothDeviceDiscoveryAgentPrivate::_q_deviceFound(const QString &address, const QVariantMap &dict) { const QBluetoothAddress btAddress(address); const QString btName = dict.value(QLatin1String("Name")).toString(); quint32 btClass = dict.value(QLatin1String("Class")).toUInt(); #ifdef QTM_DEVICEDISCOVERY_DEBUG qDebug() << "Discovered: " << address << btName << "Num UUIDs" << dict.value(QLatin1String("UUIDs")).toStringList().count() << "total device" << discoveredDevices.count() << "cached" << dict.value(QLatin1String("Cached")).toBool() << "RSSI" << dict.value(QLatin1String("RSSI")).toInt(); #endif QBluetoothDeviceInfo device(btAddress, btName, btClass); if(dict.value(QLatin1String("RSSI")).isValid()) device.setRssi(dict.value(QLatin1String("RSSI")).toInt()); QList<QBluetoothUuid> uuids; foreach (const QString &u, dict.value(QLatin1String("UUIDs")).toStringList()) { uuids.append(QBluetoothUuid(u)); } device.setServiceUuids(uuids, QBluetoothDeviceInfo::DataIncomplete); device.setCached(dict.value(QLatin1String("Cached")).toBool()); for(int i = 0; i < discoveredDevices.size(); i++){ if(discoveredDevices[i].address() == device.address()) { if(discoveredDevices[i] == device) { #ifdef QTM_DEVICEDISCOVERY_DEBUG qDebug() << "Duplicate: " << address; #endif return; } discoveredDevices.replace(i, device); Q_Q(QBluetoothDeviceDiscoveryAgent); #ifdef QTM_DEVICEDISCOVERY_DEBUG qDebug() << "Updated: " << address; #endif emit q->deviceDiscovered(device); return; // this works if the list doesn't contain duplicates. Don't let it. } } #ifdef QTM_DEVICEDISCOVERY_DEBUG qDebug() << "Emit: " << address; #endif discoveredDevices.append(device); Q_Q(QBluetoothDeviceDiscoveryAgent); emit q->deviceDiscovered(device); }
void Nushabe::startClient() { //rfcommServer->serverType(); QString uuid("00001101-0000-1000-8000-00805F9B34FB"); QBluetoothSocket* socket=new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); socket->connectToService(QBluetoothAddress("98:D3:31:60:30:DF"),QBluetoothUuid(uuid)); // Connection is ready to be used qDebug("Debug 1"); connect(socket, SIGNAL(connected()), this, SLOT(bt_connected())); connect(socket, SIGNAL(disconnected()), this,SLOT(bt_disconnected())); connect(socket,SIGNAL(error(QBluetoothSocket::SocketError)),this,SLOT(error(QBluetoothSocket::SocketError))); qDebug() << "Create socket"; qDebug() << "ConnectToService done"; //connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket())); }
void PingPong::startClient() { //! [Searching for the service] discoveryAgent = new QBluetoothServiceDiscoveryAgent(QBluetoothAddress()); connect(discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(addService(QBluetoothServiceInfo))); connect(discoveryAgent, SIGNAL(finished()), this, SLOT(done())); connect(discoveryAgent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(serviceScanError(QBluetoothServiceDiscoveryAgent::Error))); discoveryAgent->setUuidFilter(QBluetoothUuid(serviceUuid)); discoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery); //! [Searching for the service] setMessage(QStringLiteral("Starting server discovery. You are the right player")); // m_role is set to 2 if it is a client m_role = 2; Q_EMIT roleChanged(); }
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 ); }
bool QBluetoothTransferReplySymbian::start() { if (m_address.isNull()) return false; m_running = true; m_discoveryAgent = new QBluetoothServiceDiscoveryAgent(m_address); connect(m_discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(serviceDiscovered(QBluetoothServiceInfo))); connect(m_discoveryAgent, SIGNAL(finished()), this, SLOT(serviceDiscoveryFinished())); // Automatically delete agent when device discovery finishes. connect(m_discoveryAgent, SIGNAL(finished()), this, SLOT(deleteLater())); m_discoveryAgent->setUuidFilter(QBluetoothUuid(QBluetoothUuid::ObexObjectPush)); m_discoveryAgent->start(); return true; }
/*! 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; }
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"; } }
void QDeclarativeBluetoothDiscoveryModel::setRunning(bool running) { if (!d->m_componentCompleted) { d->m_runningRequested = running; return; } if (d->m_running == running) return; d->m_running = running; if (!running) { if (d->m_deviceAgent) d->m_deviceAgent->stop(); if (d->m_serviceAgent) d->m_serviceAgent->stop(); } else { clearModel(); d->m_error = NoError; if (d->m_discoveryMode == DeviceDiscovery) { if (!d->m_deviceAgent) { d->m_deviceAgent = new QBluetoothDeviceDiscoveryAgent(this); connect(d->m_deviceAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(deviceDiscovered(QBluetoothDeviceInfo))); connect(d->m_deviceAgent, SIGNAL(finished()), this, SLOT(finishedDiscovery())); connect(d->m_deviceAgent, SIGNAL(canceled()), this, SLOT(finishedDiscovery())); connect(d->m_deviceAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), this, SLOT(errorDeviceDiscovery(QBluetoothDeviceDiscoveryAgent::Error))); } d->m_deviceAgent->start(); } else { if (!d->m_serviceAgent) { d->m_serviceAgent = new QBluetoothServiceDiscoveryAgent(this); connect(d->m_serviceAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(serviceDiscovered(QBluetoothServiceInfo))); connect(d->m_serviceAgent, SIGNAL(finished()), this, SLOT(finishedDiscovery())); connect(d->m_serviceAgent, SIGNAL(canceled()), this, SLOT(finishedDiscovery())); connect(d->m_serviceAgent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(errorDiscovery(QBluetoothServiceDiscoveryAgent::Error))); } d->m_serviceAgent->setRemoteAddress(QBluetoothAddress(d->m_remoteAddress)); d->m_serviceAgent->clear(); if (!d->m_uuid.isEmpty()) d->m_serviceAgent->setUuidFilter(QBluetoothUuid(d->m_uuid)); if (discoveryMode() == FullServiceDiscovery) { //qDebug() << "Full Discovery"; d->m_serviceAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery); } else { //qDebug() << "Minimal Discovery"; d->m_serviceAgent->start(QBluetoothServiceDiscoveryAgent::MinimalDiscovery); } // we could not start service discovery if (!d->m_serviceAgent->isActive()) { d->m_running = false; errorDiscovery(d->m_serviceAgent->error()); return; } } } emit runningChanged(); }
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()); } }
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); } } }
static int qt_serial_open(serial_t **out, dc_context_t *context, const char* devaddr) { if (out == NULL) return DC_STATUS_INVALIDARGS; // Allocate memory. serial_t *serial_port = (serial_t *) malloc (sizeof (serial_t)); if (serial_port == NULL) { return DC_STATUS_NOMEMORY; } // Library context. serial_port->context = context; // Default to blocking reads. serial_port->timeout = -1; #if defined(Q_OS_WIN) // Create a RFCOMM socket serial_port->socket = ::socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); if (serial_port->socket == INVALID_SOCKET) { free(serial_port); return DC_STATUS_IO; } SOCKADDR_BTH socketBthAddress; int socketBthAddressBth = sizeof (socketBthAddress); char *address = strdup(devaddr); ZeroMemory(&socketBthAddress, socketBthAddressBth); qDebug() << "Trying to connect to address " << devaddr; if (WSAStringToAddressA(address, AF_BTH, NULL, (LPSOCKADDR) &socketBthAddress, &socketBthAddressBth ) != 0) { qDebug() << "FAiled to convert the address " << address; free(address); return DC_STATUS_IO; } free(address); socketBthAddress.addressFamily = AF_BTH; socketBthAddress.port = BT_PORT_ANY; memset(&socketBthAddress.serviceClassId, 0, sizeof(socketBthAddress.serviceClassId)); socketBthAddress.serviceClassId = SerialPortServiceClass_UUID; // Try to connect to the device if (::connect(serial_port->socket, (struct sockaddr *) &socketBthAddress, socketBthAddressBth ) != 0) { qDebug() << "Failed to connect to device"; return DC_STATUS_NODEVICE; } qDebug() << "Succesfully connected to device"; #else // Create a RFCOMM socket serial_port->socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); // Wait until the connection succeeds or until an error occurs QEventLoop loop; loop.connect(serial_port->socket, SIGNAL(connected()), SLOT(quit())); loop.connect(serial_port->socket, SIGNAL(error(QBluetoothSocket::SocketError)), SLOT(quit())); // Create a timer. If the connection doesn't succeed after five seconds or no error occurs then stop the opening step QTimer timer; int msec = 5000; timer.setSingleShot(true); loop.connect(&timer, SIGNAL(timeout()), SLOT(quit())); #if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) // First try to connect on RFCOMM channel 1. This is the default channel for most devices QBluetoothAddress remoteDeviceAddress(devaddr); serial_port->socket->connectToService(remoteDeviceAddress, 1); timer.start(msec); loop.exec(); if (serial_port->socket->state() == QBluetoothSocket::ConnectingState) { // It seems that the connection on channel 1 took more than expected. Wait another 15 seconds qDebug() << "The connection on RFCOMM channel number 1 took more than expected. Wait another 15 seconds."; timer.start(3 * msec); loop.exec(); } else if (serial_port->socket->state() == QBluetoothSocket::UnconnectedState) { // Try to connect on channel number 5. Maybe this is a Shearwater Petrel2 device. qDebug() << "Connection on channel 1 failed. Trying on channel number 5."; serial_port->socket->connectToService(remoteDeviceAddress, 5); timer.start(msec); loop.exec(); if (serial_port->socket->state() == QBluetoothSocket::ConnectingState) { // It seems that the connection on channel 5 took more than expected. Wait another 15 seconds qDebug() << "The connection on RFCOMM channel number 5 took more than expected. Wait another 15 seconds."; timer.start(3 * msec); loop.exec(); } } #elif defined(Q_OS_ANDROID) || (QT_VERSION >= 0x050500 && defined(Q_OS_MAC)) // Try to connect to the device using the uuid of the Serial Port Profile service QBluetoothAddress remoteDeviceAddress(devaddr); serial_port->socket->connectToService(remoteDeviceAddress, QBluetoothUuid(QBluetoothUuid::SerialPort)); timer.start(msec); loop.exec(); if (serial_port->socket->state() == QBluetoothSocket::ConnectingState || serial_port->socket->state() == QBluetoothSocket::ServiceLookupState) { // It seems that the connection step took more than expected. Wait another 20 seconds. qDebug() << "The connection step took more than expected. Wait another 20 seconds"; timer.start(4 * msec); loop.exec(); } #endif if (serial_port->socket->state() != QBluetoothSocket::ConnectedState) { // Get the latest error and try to match it with one from libdivecomputer QBluetoothSocket::SocketError err = serial_port->socket->error(); qDebug() << "Failed to connect to device " << devaddr << ". Device state " << serial_port->socket->state() << ". Error: " << err; free (serial_port); switch(err) { case QBluetoothSocket::HostNotFoundError: case QBluetoothSocket::ServiceNotFoundError: return DC_STATUS_NODEVICE; case QBluetoothSocket::UnsupportedProtocolError: return DC_STATUS_PROTOCOL; #if QT_VERSION >= 0x050400 case QBluetoothSocket::OperationError: return DC_STATUS_UNSUPPORTED; #endif case QBluetoothSocket::NetworkError: return DC_STATUS_IO; default: return QBluetoothSocket::UnknownSocketError; } } #endif *out = serial_port; return DC_STATUS_SUCCESS; }
void RemoteSelector::startDiscovery() { startDiscovery(QBluetoothUuid(QBluetoothUuid::ObexObjectPush)); }