当前位置: 代码网 > it编程>编程语言>C/C++ > QML android 采集手机传感器数据 并通过udp 发送

QML android 采集手机传感器数据 并通过udp 发送

2024年08月01日 C/C++ 我要评论
利用 qt 开发 安卓 app ,采集手机传感器数据 并通过udp 发送。

利用 qt 开发 安卓 app ,采集手机传感器数据 并通过udp 发送

#ifndef udplink_h
#define udplink_h

#include <qobject>
#include <qudpsocket>
#include <qhostaddress>

class udplink : public qobject
{
    q_object
public:
    explicit udplink(qobject *parent = nullptr);
    void setaddress(qstring _ip,quint16 _port);
    void senddata(qbytearray ba);

signals:
private:
    qstring ip;
    quint16 port;

    qudpsocket socket;
};

#endif // udplink_h
#include "udplink.h"

udplink::udplink(qobject *parent)
    : qobject{parent}
{

}

void udplink::setaddress(qstring _ip, quint16 _port)
{
    ip=_ip;
    port = _port;
}

void udplink::senddata(qbytearray ba)
{
    socket.writedatagram(ba, qhostaddress(ip), port);
}
#ifndef app_h
#define app_h

#include <qobject>
#include <udplink.h>
#include <atomic>

#include <qaccelerometer>
#include <qgyroscope>
#include <qrotationsensor>
#include <qlightsensor>

class app : public qobject
{
    q_object
    q_property(bool isruning read getisruning write setisruning notify isruningchanged)
public:
    explicit app(qobject *parent = nullptr);

    q_invokable void start(qstring ip);
    q_invokable void stop();


    bool getisruning() const;
    void setisruning(bool newisruning);

signals:
    void gyrovalue(qreal x,qreal y,qreal z);
    void accelervalue(qreal x,qreal y,qreal z);
    void rotationvalue(qreal x,qreal y,qreal z);
    void lightvalue(qreal lux);
    void loginfo(qstring str);

    void isruningchanged();

private:
    udplink udplink;
    bool isruning{false};

    //陀螺
    qgyroscope *gyroscope;
    qgyroscopereading *gyroreader;

    //加速度计
    qaccelerometer *acceler;
    qaccelerometerreading *accelereader;

    //旋转
    qrotationsensor *rotationsensor;
    qrotationreading *rotationreading;

    //光线
    qlightsensor *lightsensor;
    qlightreading *lightreading;
};

#endif // app_h
#include "app.h"
#include <qtconcurrent>
#include <chrono>
#include <thread>
#include <qjsondocument>
#include <qjsonarray>
#include <qjsonobject>
#include <qjsonvalue>

app::app(qobject *parent)
    : qobject{parent}
{

}

void app::start(qstring ip)
{
    udplink.setaddress(ip,8023);

    qdebug()<<"start "<<ip;

    gyroscope = new qgyroscope(this);
    connect(gyroscope, &qgyroscope::readingchanged, this, [&](){
        gyroreader = gyroscope->reading();

        qjsonobject obj_root;
        qjsonarray arr;
        qreal gyroscopex = gyroreader->x();
        qreal gyroscopey = gyroreader->y();
        qreal gyroscopez = gyroreader->z();

        arr.append(qstring::number(gyroscopex,'f',2));
        arr.append(qstring::number(gyroscopey,'f',2));
        arr.append(qstring::number(gyroscopez,'f',2));
        obj_root.insert("qgyroscope",arr);

        qjsondocument jsondocu(obj_root);
        qbytearray jsondata = jsondocu.tojson();
        udplink.senddata(jsondata);

        emit gyrovalue(gyroscopex,gyroscopey,gyroscopez);
    });

    acceler = new qaccelerometer(this);
    acceler->setaccelerationmode(qaccelerometer::combined);
    connect(acceler, &qaccelerometer::readingchanged, this, [&](){
        accelereader = acceler->reading();

        qjsonobject obj_root;
        qjsonarray arr;
        qreal accelerx = accelereader->x();
        qreal accelery = accelereader->y();
        qreal accelerz = accelereader->z();

        arr.append(qstring::number(accelerx,'f',2));
        arr.append(qstring::number(accelery,'f',2));
        arr.append(qstring::number(accelerz,'f',2));
        obj_root.insert("qaccelerometer",arr);

        qjsondocument jsondocu(obj_root);
        qbytearray jsondata = jsondocu.tojson();
        udplink.senddata(jsondata);

        emit accelervalue(accelerx,accelery,accelerz);
    });

    rotationsensor = new qrotationsensor(this);
    connect(rotationsensor, &qrotationsensor::readingchanged, this, [&](){
        rotationreading = rotationsensor->reading();

        qjsonobject obj_root;
        qjsonarray arr;
        qreal rotationx = rotationreading->x();
        qreal rotationy = rotationreading->y();
        qreal rotationz = rotationreading->z();

        arr.append(qstring::number(rotationx,'f',2));
        arr.append(qstring::number(rotationy,'f',2));
        arr.append(qstring::number(rotationz,'f',2));
        obj_root.insert("qrotationsensor",arr);

        qjsondocument jsondocu(obj_root);
        qbytearray jsondata = jsondocu.tojson();
        udplink.senddata(jsondata);

        emit rotationvalue(rotationx,rotationy,rotationz);
    });

    lightsensor = new qlightsensor(this);
    connect(lightsensor, &qlightsensor::readingchanged, this, [&](){
        lightreading = lightsensor->reading();

        qjsonobject obj_root;
        qjsonarray arr;
        qreal lux = lightreading->lux();

        arr.append(qstring::number(lux,'f',2));
        obj_root.insert("qlightsensor",arr);

        qjsondocument jsondocu(obj_root);
        qbytearray jsondata = jsondocu.tojson();
        udplink.senddata(jsondata);

        emit lightvalue(lux);
    });

    if(gyroscope->start()&&acceler->start()&&rotationsensor->start()&&lightsensor->start()){
        setisruning(true);
        emit loginfo(qstring::fromutf8("启动成功"));
    }else{
        setisruning(false);
        emit loginfo(qstring::fromutf8("启动失败"));
    }
}

void app::stop()
{
    gyroscope->stop();
    acceler->stop();
    rotationsensor->stop();
    lightsensor->stop();
    setisruning(false);
}

bool app::getisruning() const
{
    return isruning;
}

void app::setisruning(bool newisruning)
{
    if (isruning == newisruning)
        return;
    isruning = newisruning;
    emit isruningchanged();
}


import qtquick 2.15
import qtquick.window 2.15
import qtquick.controls 2.15
import qtquick.layouts 1.15
import app 1.0

window {
    id:root
    width: 640
    height: 480
    visible: true
    title: qstr("数据采集")

    app{
        id:app
        ongyrovalue: {
            var str = '陀螺仪:'+x.tofixed(2)+' '+y.tofixed(2)+' '+z.tofixed(2)
            gyrolabel.text = str
        }
        onaccelervalue: {
            var str = '加速度计:'+x.tofixed(2)+' '+y.tofixed(2)+' '+z.tofixed(2)
            accelerlabel.text = str
        }

        onrotationvalue: {
            var str = '旋转:'+x.tofixed(2)+' '+y.tofixed(2)+' '+z.tofixed(2)
            rotationlabel.text = str
        }

        onlightvalue: {
            var str = '光线:'+lux.tofixed(2)
            lightlabel.text=str
        }
        onloginfo: {
            debuginof.text=str
        }
    }
    rowlayout{
        id:topbar
        anchors.margins: 5
        anchors.top: parent.top
        anchors.left: parent.left
        spacing: 5

        rectangle{
            id:address
            layout.alignment: qt.alignhcenter
            height: linkbtn.height
            width: 200
            border.color: "black"
            border.width: 1
            textinput{
                id:ip
                anchors.fill: parent
                verticalalignment:text.alignvcenter
                horizontalalignment:text.alignhcenter
                text: "192.168.1"
            }
        }

        button{
            id:linkbtn
            layout.alignment: qt.alignhcenter
            text: !app.isruning?"启动":"停止"
            onclicked: {
                if(!app.isruning){
                    app.start(ip.text)
                }else{
                    app.stop()
                }
            }
        }

    }

    columnlayout{
        anchors.left:parent.left
        anchors.right:parent.right
        anchors.top:topbar.bottom
        anchors.bottom:parent.bottom
        anchors.margins: 5
        label{
            id:gyrolabel
            width: 200
            height: 50
            text: "陀螺仪"
        }
        label{
            id:accelerlabel
             width: 200
             height: 50
             text: "加速度计"
        }
        label{
            id:rotationlabel
             width: 200
             height: 50
             text: "旋转"
        }
        label{
            id:lightlabel
             width: 200
             height: 50
             text:"光线"
        }
        textedit{
            id:debuginof
            height: 50
        }
    }

}

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com