QML android 采集手机传感器数据 并通过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
        }
    }

}
相关推荐
小尘要自信3 小时前
2026手机远控电脑横测:CMD、游戏按键、144帧...ToDesk/向日葵/RustDesk跨设备处理任务
游戏·智能手机·电脑
时代分流6 小时前
云手机推荐2026:安卓全能、批量挂机、原生iOS全覆盖
android·ios·智能手机
大鹅办公7 小时前
苹果手机忘记锁屏密码怎么办?2026年5种方法实测(含保数据方案)
经验分享·智能手机·苹果手机
智造工厂1 天前
领盾LEDEN-L1数模对讲+热成像+气体检测工业智能防爆巡检终端,全方位赋能工业巡检现场!
智能手机
wujian83111 天前
【 文心手机版生成的表格怎么复制下来 】?这个问题,[ AI 导出鸭 ] 给出了完全不同的答案
人工智能·ai·chatgpt·智能手机·ai导出鸭
极客猴子2 天前
能提取抖音视频文案的APP推荐:短视频文案工具合集
人工智能·智能手机·音视频·语音识别
wujian83112 天前
【腾讯元宝手机版生成的表格怎么复制下来】?试试 [ AI 导出鸭 ] 的“格式网关”
人工智能·ai·chatgpt·智能手机·ai导出鸭
wujian83112 天前
豆包导出word手机,就用AI导出鸭:一站式批量导出方案深度解析
人工智能·ai·chatgpt·智能手机·word·ai导出鸭
拿本唠嗑AI研究2 天前
从电脑到手机:DeepSeek Harness Windows安装与手机互动教程(避坑完全版)
人工智能·windows·智能手机·deepseek·harness
Buke..3 天前
【APP 逆向】哔哩哔哩 sign 参数逆向(上):Frida 反调试绕过与 unidbg 调用
java·开发语言·爬虫·python·安卓