QT之Android下获取手机传感器数据学习笔记

QT += core gui sensors positioning

其中sensors是获取手机上传感器数据的组件,positioning是获取位置信息的组件

1、获取陀螺仪传感器数据

#include 

QGyroscope *gyroscope;
QGyroscopeReading *reader;

gyroscope = new QGyroscope(this);
gyroscope->start();
reader = gyroscope->reading();
qreal x = reader->x();
qreal y = reader->y();
qreal z = reader->z();

2、获取加速度传感器数据

#include 

QAccelerometer *acceler;
QAccelerometerReading *accelereader;

acceler = new QAccelerometer(this);
acceler->setAccelerationMode(QAccelerometer::Combined);
acceler->start();
accelereader = acceler->reading();

qreal x = accelereader->x();
qreal y = accelereader->y();
qreal z = accelereader->z();

3、获取光线强度传感器数据

#include 

QLightSensor *lightSensor;
QLightReading *lightReading;

lightSensor = new QLightSensor(this);
lightSensor->start();
lightReading = lightSensor->reading();
qreal lux = lightReading->lux();

4、获取电磁传感器数据

#include 

QMagnetometer *magnetoMeter;
QMagnetometerReading *magnetometerReading;

magnetoMeter = new QMagnetometer(this);
magnetoMeter->start();
magnetometerReading = magnetoMeter->reading();
x = magnetometerReading->x();
y = magnetometerReading->y();
z = magnetometerReading->z();

5、获取接近传感器数据

#include 

QProximitySensor *proximitySensor;
QProximityReading *proximityReading;

proximitySensor = new QProximitySensor(this);
proximitySensor->start();
proximityReading = proximitySensor->reading();
if(proximityReading->close())
{
   ui->label_30->setText("接近传感器:接近");
}
else
{
   ui->label_30->setText("接近传感器:远离");
}

6、旋转传感器数据读取

#include 

QRotationSensor *rotationSensor;
QRotationReading *rotationReading;

rotationSensor = new QRotationSensor(this);
rotationSensor->start();
rotationReading = rotationSensor->reading();
ui->lcdNumber_11->display(rotationReading->x());
ui->lcdNumber_12->display(rotationReading->y());
ui->lcdNumber_13->display(rotationReading->z());

7、获取手机位置相关信息

#include 
#include 
#include 

double nowGroundSpeed=0.0;
double nowVerticalSpeed=0.0;
double nowMagneticVariation=0.0;
double nowHorizontalAccuracy=0.0;
double nowVerticalAccuracy=0.0;
double nowLongitude=0.0;
double nowLatitude=0.0;
double nowDirection=0.0;
QString nowTimes="";

QGeoPositionInfoSource *source;

source = QGeoPositionInfoSource::createDefaultSource(this);
if (source) {
    connect(source, SIGNAL(positionUpdated(const QGeoPositionInfo&)),
        this, SLOT(positionUpdated(const QGeoPositionInfo&)));
    source->setUpdateInterval(200);//设置刷新时间 单位毫秒
    source->startUpdates();//启动设备
}

void MainWindow::positionUpdated(const QGeoPositionInfo &info)
{
    nowLongitude = info.coordinate().longitude();//经度
    nowLatitude = info.coordinate().latitude();//纬度
    nowDirection = info.attribute(QGeoPositionInfo::Direction);
    nowGroundSpeed = info.attribute(QGeoPositionInfo::GroundSpeed);
    nowVerticalSpeed = info.attribute(QGeoPositionInfo::VerticalSpeed);
    nowMagneticVariation = info.attribute(QGeoPositionInfo::MagneticVariation);
    nowHorizontalAccuracy = info.attribute(QGeoPositionInfo::HorizontalAccuracy);
    nowVerticalAccuracy = info.attribute(QGeoPositionInfo::VerticalAccuracy);
    nowTimes = info.timestamp().toString();

    ui->label_10->setText(nowTimes);
    ui->label_12->setText(QString::number(nowLongitude,10,7));
    ui->label_14->setText(QString::number(nowLatitude,10,7));

    ui->label_16->setText(QString::number(nowGroundSpeed,10,7));
    ui->label_18->setText(QString::number(nowVerticalSpeed,10,7));

    ui->label_20->setText(QString::number(nowMagneticVariation,10,7));
    ui->label_22->setText(QString::number(nowDirection,10,7));

    ui->label_24->setText(QString::number(nowHorizontalAccuracy,10,7)+"         
      "+QString::number(nowVerticalAccuracy,10,7));
}

 

你可能感兴趣的:(QT)