c++ - void BTPairedServer::clientConnected() not called -


this code communication between 2 bluetooth devices. establish connection based on common uuid. both devices act both server , client. took code http://doc.qt.io/qt-5/qtbluetooth-btchat-example.html. able discover both devices not able start socket connection between them.when socket connection established, void btpairedserver::clientconnected() should called

btsettingstab::btsettingstab(const qfileinfo & fileinfo, qwidget*parent)     :qdialog(parent),currentadapterindex(0) {     choosesettings = new qlabel(tr("choose settings"));     settingslistbox = new qlistwidget;      settings.append(tr("register locket"));     settingslistbox->insertitems(0,settings);     layout1 = new qvboxlayout;     qdialogbuttonbox * buttonbox = new qdialogbuttonbox(qdialogbuttonbox::ok|qdialogbuttonbox::cancel);     connect(buttonbox, &qdialogbuttonbox::accepted,this,&btsettingstab::accept);     connect(buttonbox,&qdialogbuttonbox::rejected,this,&btsettingstab::reject);     layout1->addwidget(choosesettings);     layout1->addwidget(settingslistbox);     layout1->addwidget(buttonbox);     setlayout(layout1); }      void btsettingstab::accept() {     printf("hello world\n");     if(flagstate1 == echosesettings)     {         qstring curr = settingslistbox->currentitem()->text();         if(!strcmp(curr.tolatin1().data(),"register locket"))         {             //flag = true;             //scanneddevices = new qlabel(tr("scanned devices :"));             //newconfiguration(&devices,&bluetoothnamesandaddresses, &numbtdevdiscovered);             localadapters = qbluetoothlocaldevice::alldevices();             //char a[] = "back";             qlist<qbluetoothhostinfo>::iterator i;             /*or (i = localadapters.begin(); != localadapters.end(); ++i)                 //cout << *i << endl;                 devices.append(i->address().tostring());*/             //qbluetoothlocaldevice adapter(localadapters.at(0).address());             //(check)adapter.sethostmode(qbluetoothlocaldevice::hostdiscoverable);             server = new btpairedserver(this);                 connect(server, signal(clientconnected(qstring)), this, slot(clientconnected(qstring)));                 connect(server, signal(clientdisconnected(qstring)), this, slot(clientdisconnected(qstring)));                 connect(server, signal(messagereceived(qstring,qstring)),                         this, slot(showmessage(qstring,qstring)));                 connect(this, signal(sendmessage(qstring)), server, slot(sendmessage(qstring)));                 server->startserver();                  localname = qbluetoothlocaldevice().name();                 const qbluetoothaddress adapter2 = localadapters.isempty() ?                                                        qbluetoothaddress() :                                                        localadapters.at(currentadapterindex).address();                  //qbluetoothaddress adapter2 = localadapters.at(0).address();                 m_discoveryagent = new qbluetoothservicediscoveryagent(adapter2);                  connect(m_discoveryagent, signal(servicediscovered(qbluetoothserviceinfo)),                         this, slot(servicediscovered(qbluetoothserviceinfo)));                 connect(m_discoveryagent, signal(finished()), this, slot(discoveryfinished()));                 connect(m_discoveryagent, signal(canceled()), this, slot(discoveryfinished()));                 if (m_discoveryagent->isactive())                     m_discoveryagent->stop();                  //ui->remotedevices->clear();   m_discoveryagent->setuuidfilter(qbluetoothuuid(serviceuuid));                     m_discoveryagent->start(qbluetoothservicediscoveryagent::fulldiscovery);                 //devices.append(a);              }             else             {                 devicelistbox->insertitems(0,devices);             }         } //there crash in section if more 1 device added fixed later else if(flagstate1 ==  eadddevices) { scanneddevices = new qlabel(tr("scanned devices :")); char a[] = "back"; devices.append(a); devicelistbox = new qlistwidget; devicelistbox->insertitems(0,devices); layout2 = new qvboxlayout; layout2->addwidget(scanneddevices); layout2->addwidget(devicelistbox);  qdialogbuttonbox *buttonbox = new qdialogbuttonbox(qdialogbuttonbox::ok                                      | qdialogbuttonbox::cancel);  connect(buttonbox, &qdialogbuttonbox::accepted, this, &btsettingstab::accept); connect(buttonbox, &qdialogbuttonbox::rejected, this, &btsettingstab::reject); layout2->addwidget(buttonbox);  delete(choosesettings); delete(layout1); setlayout(layout2); flagstate1 = echosedevices; }  }  btpairedserver::btpairedserver(qobject *parent) :   qobject(parent), rfcommserver(0) { }  btpairedserver::~btpairedserver() { stopserver(); } void btpairedserver::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::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()*/8888)); protocoldescriptorlist.append(qvariant::fromvalue(protocol)); serviceinfo.setattribute(qbluetoothserviceinfo::protocoldescriptorlist,                          protocoldescriptorlist);  serviceinfo.registerservice(localadapter); } void btsettingstab::servicediscovered(const qbluetoothserviceinfo &serviceinfo) { #if 0 qdebug() << "discovered service on"      << serviceinfo.device().name() << serviceinfo.device().address().tostring(); qdebug() << "\tservice name:" << serviceinfo.servicename(); qdebug() << "\tdescription:"      << serviceinfo.attribute(qbluetoothserviceinfo::servicedescription).tostring(); qdebug() << "\tprovider:"      << serviceinfo.attribute(qbluetoothserviceinfo::serviceprovider).tostring(); qdebug() << "\tl2cap protocol service multiplexer:"      << serviceinfo.protocolservicemultiplexer(); qdebug() << "\trfcomm server channel:" << serviceinfo.serverchannel(); #endif  m_service = serviceinfo; qmapiterator<qlistwidgetitem *, qbluetoothserviceinfo> i(m_discoveredservices); while (i.hasnext()){ i.next(); if (serviceinfo.device().address() == i.value().device().address()){     return; } } int rssi = serviceinfo.device().rssi(); int txpower = -59; double distance; double ratio; if(rssi == 0) distance = -1.0; else { ratio = rssi * 1.0 /txpower; if(ratio < 1.0)     distance = pow(ratio,10); else     distance = (0.89976)*pow(ratio,7.7095) + 0.111; } qbluetoothuuid list = serviceinfo.serviceuuid(); devices.append(qstring::number(list.txpower)); devices.append(qstring::number(distance)); /*qlist<qbluetoothuuid>::iterator = list.begin(); for(;it!=list.end();it++) { devices.append(qstring::number(it->cyclingpowermeasurement)); }*/ qstring remotename; if (serviceinfo.device().name().isempty()) remotename = serviceinfo.device().address().tostring(); else remotename = serviceinfo.device().name();  qlistwidgetitem *item = new qlistwidgetitem(qstring::fromlatin1("%1 %2").arg(remotename,                                                      serviceinfo.servicename()));  qstring item2= qstring::fromlatin1("%1 %2 %3").arg(remotename,serviceinfo.servicename(),qstring::number(rssi)); //sprintf(item2,"%s,%s",remotename,serviceinfo.servicename()); //cout<<item2<<endl; m_discoveredservices.insert(item, serviceinfo); //ui->remotedevices->additem(item); devices.append(item2); flagstate1 =  eadddevices; qbluetoothserviceinfo service = m_service;               qdebug() << "connecting service 2" << service.servicename()                       << "on" << service.device().name();               // create client              qdebug() << "going create client";              btpairedclient *client = new btpairedclient(m_service,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);  emit accept(); }  void btsettingstab::showmessage(const qstring & sender, const qstring& message) { qmessagebox::information(0,qstring::fromlatin1("%1:\n").arg(sender),qstring::fromlatin1("%1:\n").arg(message),qmessagebox::ok); }  void btsettingstab::sendclicked() { showmessage(localname, "ui->sendtext->text()"); emit sendmessage("ui->sendtext->text()"); }  void btsettingstab::clientconnected(const qstring& name) { qmessagebox::information(0,qstring("accepted connection from"),qstring(name),qmessagebox::ok); }  void btpairedserver::clientconnected() { qbluetoothsocket *socket = rfcommserver->nextpendingconnection(); if (!socket) return;  connect(socket, signal(readyread()), this, slot(readsocket())); connect(socket, signal(disconnected()), this, slot(clientdisconnected())); clientsockets.append(socket); emit clientconnected(socket->peername()); }  btpairedclient::btpairedclient(qbluetoothserviceinfo serviceinfo,qobject *parent) :   qobject(parent), socket(0),m_service(serviceinfo) { }  btpairedclient::~btpairedclient() { stopclient(); }  void btpairedclient::startclient(const qbluetoothserviceinfo &remoteservice) { if (socket) return;  // connect service socket = new qbluetoothsocket(qbluetoothserviceinfo::rfcommprotocol); qdebug() << "create socket"; socket->connecttoservice(remoteservice); qdebug() << "connecttoservice done";  connect(socket, signal(readyread()), this, slot(readsocket())); connect(socket, signal(connected()), this, slot(connected())); connect(socket, signal(disconnected()), this, signal(disconnected())); }  void btpairedclient::connected() { emit connected(socket->peername()); } 


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -