Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can`t do spp connection between android and PC windows



  • 0
    down vote
    favorite
    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:

    static const QLatin1String serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
    mobile_sensors::mobile_sensors(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mobile_sensors)
    {
    ui->setupUi(this);

    QString empieza = "empieza la aplicaciĆ³n";
    ui->empieza->setText(empieza);
    ui->empieza->adjustSize();
    startServer();
    }

    void mobile_sensors::startServer(const QBluetoothAddress& localAdapter)
    {

    ui->startServer->setText("entra en startserver");
    ui->startServer->adjustSize();
    rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
    connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));

    bool result = rfcommServer->listen(localAdapter);
    if (!result) {
    QLabel* server_result = new QLabel();
    server_result->setText("no se ha podido crear el servidor");
    server_result->adjustSize();
    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 sensor
    data Server"));
    serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceDescription,
    tr("Sensors data app"));
    serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceProvider,
    tr("sensors-data.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);

    }

    mobile_sensors::~mobile_sensors()
    {
    accelerometer->stop();

    delete accelerometer;

    stopServer();
    delete ui;
    }

    void mobile_sensors::accel_data()
    {

    qreal x = accelerometer->reading()->x();
    qreal y = accelerometer->reading()->y();
    qreal z = accelerometer->reading()->z();

    send_data(x,y,z,"acceleremoter");

    /*foreach (QBluetoothSocket socket, clientSockets)
    socket->write(text);
    /
    }

    void mobile_sensors::send_data(qreal x, qreal y, qreal z, QString type)
    {
    QString data;
    if(type == "accelerometer"){

            data = "accelerometer x:"+QString::number(x)+", 
            y:"+QString::number(y)+", z:"+QString::number(z);
    
    }else if(type == "gyroscope"){
    
            data = "gyroscope x:"+QString::number(x)+", 
            y:"+QString::number(y)+", z:"+QString::number(z);
    
    }else if(type == "magnetometer"){
    
            data = "magnetometer x:"+QString::number(x)+", 
            y:"+QString::number(y)+", z:"+QString::number(z);
    
    }
    
    QByteArray text = data.toUtf8() + '\n';
    
    foreach (QBluetoothSocket *socket, clientSockets)
        socket->write(text);
    

    }

    void mobile_sensors::gyro_data()
    {
    qreal x = gyroscope->reading()->x();
    qreal y = gyroscope->reading()->y();
    qreal z = gyroscope->reading()->z();

    send_data(x,y,z, "gyroscope");

    }

    void mobile_sensors::magn_data()
    {
    qreal x = magnetometer->reading()->x();
    qreal y = magnetometer->reading()->y();
    qreal z = magnetometer->reading()->z();

    send_data(x,y,z, "magnetometer");

    }

    void mobile_sensors::stopServer()
    {/*
    // Unregister service
    serviceInfo.unregisterService();*/

    // Close sockets
    qDeleteAll(clientSockets);

    // Close server
    delete rfcommServer;
    rfcommServer = 0;
    }

    void mobile_sensors::clientConnected()
    {
    QLabel* connected = new QLabel();
    connected->setText("cliente conectado");
    connected->adjustSize();
    accelerometer = new QAccelerometer(this);
    gyroscope = new QGyroscope(this);
    magnetometer = new QMagnetometer(this);
    connect(accelerometer, SIGNAL(readingChanged()), this,
    SLOT(send_accel_data()));
    connect(gyroscope, SIGNAL(readingChanged()), this, SLOT(send_gyro_data()));

    QBluetoothSocket *socket = rfcommServer->nextPendingConnection();
    if (!socket)
    return;

    connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    clientSockets.append(socket);
    accelerometer->start();
    gyroscope->start();
    magnetometer->start();
    }

    void mobile_sensors::clientDisconnected()
    {
    QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
    if (!socket)
    return;

    clientSockets.removeOne(socket);

    socket->deleteLater();
    }


Log in to reply