static const QLatin1String serviceUuid
("e8e10f95-1a70-4b27-9ccf-02010264e9c8");
mobile_sensors
::mobile_sensors(QWidget *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::HostDiscoverable);
}
startServer();
}
void mobile_sensors::startServer(const QBluetoothAddress& localAdapter)
{
ui->startServer->setText("entra en startserver");
ui->startServer->adjustSize();
rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, 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::ServiceName, serviceName);
QBluetoothServiceInfo::Sequence browseSequence;
browseSequence <<
QVariant::fromValue(QBluetoothUuid
(QBluetoothUuid
::PublicBrowseGroup));
serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
browseSequence);
QBluetoothServiceInfo::Sequence classId;
classId <<
QVariant::fromValue(QBluetoothUuid
(QBluetoothUuid
::SerialPort));
serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,
classId);
// const QBluetoothUuid uuid = serviceUuid;
//Android requires custom uuid to be set as service class
classId.
prepend(QVariant::fromValue(QBluetoothUuid
(serviceUuid
)));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid));
//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::ProtocolDescriptorList,
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
) {
if(type == "accelerometer"){
}else if(type == "gyroscope"){
}else if(type == "magnetometer"){
}
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();
}
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::HostDiscoverable);
}
startServer();
}
void mobile_sensors::startServer(const QBluetoothAddress& localAdapter)
{
ui->startServer->setText("entra en startserver");
ui->startServer->adjustSize();
rfcommServer = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, 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::ServiceName, serviceName);
QBluetoothServiceInfo::Sequence browseSequence;
browseSequence << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));
serviceInfo.setAttribute(QBluetoothServiceInfo::BrowseGroupList,
browseSequence);
QBluetoothServiceInfo::Sequence classId;
classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
serviceInfo.setAttribute(QBluetoothServiceInfo::BluetoothProfileDescriptorList,
classId);
// const QBluetoothUuid uuid = serviceUuid;
//Android requires custom uuid to be set as service class
classId.prepend(QVariant::fromValue(QBluetoothUuid(serviceUuid)));
serviceInfo.setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);
serviceInfo.setServiceUuid(QBluetoothUuid(serviceUuid));
//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::ProtocolDescriptorList,
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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks