PDA

View Full Version : Can`t do spp connection between android and PC windows



ikerena
31st May 2018, 11:27
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();


Adapters = QBluetoothLocalDevice::allDevices();
if (Adapters.count() < 2) {

} else {
//we ignore more than two adapters
QBluetoothLocalDevice adapter(Adapters.at(0).address());
adapter.setHostMode(QBluetoothLocalDevice::HostDis coverable);
}


startServer();
}


void mobile_sensors::startServer(const QBluetoothAddress& localAdapter)
{
ui->startServer->setText("entra en startserver");
ui->startServer->adjustSize();
rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProt ocol, this);



bool result = rfcommServer->listen();
if (!result) {

ui->creado->setText("no se ha creado bien");
ui->creado->adjustSize();
return;
}else{

ui->creado->setText("se ha creado bien");
ui->creado->adjustSize();
}

connect(rfcommServer, SIGNAL(newConnection()), this, SLOT(clientConnected()));


const QString &serviceName = "andtroid sensors";

QBluetoothServiceInfo serviceInfo;
serviceInfo.setAttribute(QBluetoothServiceInfo::Se rviceName, serviceName);
QBluetoothServiceInfo::Sequence browseSequence;
browseSequence << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid: :PublicBrowseGroup));
serviceInfo.setAttribute(QBluetoothServiceInfo::Br owseGroupList,
browseSequence);

QBluetoothServiceInfo::Sequence classId;
classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid: :SerialPort));
serviceInfo.setAttribute(QBluetoothServiceInfo::Bl uetoothProfileDescriptorList,
classId);

// const QBluetoothUuid uuid = serviceUuid;
//Android requires custom uuid to be set as service class
classId.prepend(QVariant::fromValue(QBluetoothUuid (serviceUuid)));
serviceInfo.setAttribute(QBluetoothServiceInfo::Se rviceClassIds, classId);
serviceInfo.setServiceUuid(QBluetoothUuid(serviceU uid));
//setServiceUuid(serviceUuid);

QBluetoothServiceInfo::Sequence protocolDescriptorList;
QBluetoothServiceInfo::Sequence protocol;
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid: :L2cap));
protocolDescriptorList.append(QVariant::fromValue( protocol));
protocol.clear();

protocolDescriptorList.append(QVariant::fromValue( protocol));
serviceInfo.setAttribute(QBluetoothServiceInfo::Pr otocolDescriptorList,
protocolDescriptorList);
bool result2 = serviceInfo.registerService();


}


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()
{

ui->startServer->setText("Entra en startserver");
ui->startServer->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();
}