1. 程式人生 > >QT之Android下獲取手機感測器資料學習筆記

QT之Android下獲取手機感測器資料學習筆記

QT += core gui sensors positioning

其中sensors是獲取手機上感測器資料的元件,positioning是獲取位置資訊的元件

1、獲取陀螺儀感測器資料

#include <QGyroscope>

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>

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>

QLightSensor *lightSensor;
QLightReading *lightReading;

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

4、獲取電磁感測器資料

#include <QMagnetometer>

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>

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>

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 <QGeoPositionInfoSource>
#include <QGeoCoordinate>
#include <QGeoPositionInfo>

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));
}