I am creating an app which creates a server and waits for a connection. When the connection is established, the android mobile will send the PC data from accelerometer, gyroscope and magnetometer. I'm doing this with C++ and Qt. The problem is that the app create the server but when I establish the connection with PC creating a connection with a COM port. A message is shown to me in the PC saying: "the selected device is not running a serial port service".

This is the code, there could be more mistakes:


Qt Code:
  1. static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
  2. mobile_sensors::mobile_sensors(QWidget *parent) :
  3. QMainWindow(parent),
  4. ui(new Ui::mobile_sensors)
  5. {
  6.  
  7.  
  8. ui->setupUi(this);
  9.  
  10. QString empieza = "empieza la aplicación";
  11. ui->empieza->setText(empieza);
  12. ui->empieza->adjustSize();
  13.  
  14.  
  15. Adapters = QBluetoothLocalDevice::allDevices();
  16. if (Adapters.count() < 2) {
  17.  
  18. } else {
  19. //we ignore more than two adapters
  20. QBluetoothLocalDevice adapter(Adapters.at(0).address());
  21. adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
  22. }
  23.  
  24.  
  25. startServer();
  26. }
  27.  
  28.  
  29. void mobile_sensors::startServer(const QBluetoothAddress& localAdapter)
  30. {
  31. ui->startServer->setText("entra en startserver");
  32. ui->startServer->adjustSize();
  33. rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
  34.  
  35.  
  36.  
  37. bool result = rfcommServer->listen();
  38. if (!result) {
  39.  
  40. ui->creado->setText("no se ha creado bien");
  41. ui->creado->adjustSize();
  42. return;
  43. }else{
  44.  
  45. ui->creado->setText("se ha creado bien");
  46. ui->creado->adjustSize();
  47. }
  48.  
  49. connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));
  50.  
  51.  
  52. const QString &serviceName = "andtroid sensors";
  53.  
  54. QBluetoothServiceInfo serviceInfo;
  55. serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceName, serviceName);
  56. QBluetoothServiceInfo::Sequence browseSequence;
  57. browseSequence << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
  58. serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
  59. browseSequence);
  60.  
  61. QBluetoothServiceInfo::Sequence classId;
  62. classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
  63. serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,
  64. classId);
  65.  
  66. // const QBluetoothUuid uuid = serviceUuid;
  67. //Android requires custom uuid to be set as service class
  68. classId.prepend(QVariant::fromValue(QBluetoothUuid(serviceUuid)));
  69. serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
  70. serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid));
  71. //setServiceUuid(serviceUuid);
  72.  
  73. QBluetoothServiceInfo::Sequence protocolDescriptorList;
  74. QBluetoothServiceInfo::Sequence protocol;
  75. protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap));
  76. protocolDescriptorList.append(QVariant::fromValue(protocol));
  77. protocol.clear();
  78.  
  79. protocolDescriptorList.append(QVariant::fromValue(protocol));
  80. serviceInfo.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
  81. protocolDescriptorList);
  82. bool result2 = serviceInfo.registerService();
  83.  
  84.  
  85. }
  86.  
  87.  
  88. mobile_sensors::~mobile_sensors()
  89. {
  90. accelerometer->stop();
  91.  
  92. delete accelerometer;
  93.  
  94. stopServer();
  95. delete ui;
  96. }
  97.  
  98. void mobile_sensors::accel_data()
  99. {
  100.  
  101. qreal x = accelerometer->reading()->x();
  102. qreal y = accelerometer->reading()->y();
  103. qreal z = accelerometer->reading()->z();
  104.  
  105.  
  106. send_data(x,y,z,"acceleremoter");
  107.  
  108.  
  109. /*foreach (QBluetoothSocket *socket, clientSockets)
  110.   socket->write(text);*/
  111. }
  112.  
  113. void mobile_sensors::send_data(qreal x, qreal y, qreal z, QString type)
  114. {
  115. QString data;
  116. if(type == "accelerometer"){
  117.  
  118. data = "accelerometer x:"+QString::number(x)+", y:"+QString::number(y)+", z:"+QString::number(z);
  119.  
  120. }else if(type == "gyroscope"){
  121.  
  122. data = "gyroscope x:"+QString::number(x)+", y:"+QString::number(y)+", z:"+QString::number(z);
  123.  
  124. }else if(type == "magnetometer"){
  125.  
  126. data = "magnetometer x:"+QString::number(x)+", y:"+QString::number(y)+", z:"+QString::number(z);
  127.  
  128. }
  129.  
  130. QByteArray text = data.toUtf8() + '\n';
  131.  
  132. foreach (QBluetoothSocket *socket, clientSockets)
  133. socket->write(text);
  134. }
  135.  
  136.  
  137. void mobile_sensors::gyro_data()
  138. {
  139. qreal x = gyroscope->reading()->x();
  140. qreal y = gyroscope->reading()->y();
  141. qreal z = gyroscope->reading()->z();
  142.  
  143.  
  144. send_data(x,y,z, "gyroscope");
  145.  
  146.  
  147. }
  148.  
  149. void mobile_sensors::magn_data()
  150. {
  151. qreal x = magnetometer->reading()->x();
  152. qreal y = magnetometer->reading()->y();
  153. qreal z = magnetometer->reading()->z();
  154.  
  155.  
  156. send_data(x,y,z, "magnetometer");
  157.  
  158.  
  159. }
  160.  
  161.  
  162. void mobile_sensors::stopServer()
  163. {/*
  164.   // Unregister service
  165.   serviceInfo.unregisterService();*/
  166.  
  167. // Close sockets
  168. qDeleteAll(clientSockets);
  169.  
  170. // Close server
  171. delete rfcommServer;
  172. rfcommServer = 0;
  173. }
  174.  
  175. void mobile_sensors::clientConnected()
  176. {
  177.  
  178. ui->startServer->setText("Entra en startserver");
  179. ui->startServer->adjustSize();
  180. accelerometer = new QAccelerometer(this);
  181. gyroscope = new QGyroscope(this);
  182. magnetometer = new QMagnetometer(this);
  183. connect(accelerometer, SIGNAL(readingChanged()), this, SLOT(send_accel_data()));
  184. connect(gyroscope, SIGNAL(readingChanged()), this, SLOT(send_gyro_data()));
  185.  
  186.  
  187. QBluetoothSocket *socket = rfcommServer->nextPendingConnection();
  188. if (!socket)
  189. return;
  190.  
  191. connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
  192. clientSockets.append(socket);
  193. accelerometer->start();
  194. gyroscope->start();
  195. magnetometer->start();
  196. }
  197.  
  198. void mobile_sensors::clientDisconnected()
  199. {
  200. QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
  201. if (!socket)
  202. return;
  203.  
  204. clientSockets.removeOne(socket);
  205.  
  206. socket->deleteLater();
  207. }
To copy to clipboard, switch view to plain text mode