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


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

    1. #ifndef UDPLINK_H
    2. #define UDPLINK_H
    3. #include
    4. #include
    5. #include
    6. class UdpLink : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit UdpLink(QObject *parent = nullptr);
    11. void setAddress(QString _ip,quint16 _port);
    12. void sendData(QByteArray ba);
    13. signals:
    14. private:
    15. QString ip;
    16. quint16 port;
    17. QUdpSocket socket;
    18. };
    19. #endif // UDPLINK_H
    1. #include "udplink.h"
    2. UdpLink::UdpLink(QObject *parent)
    3. : QObject{parent}
    4. {
    5. }
    6. void UdpLink::setAddress(QString _ip, quint16 _port)
    7. {
    8. ip=_ip;
    9. port = _port;
    10. }
    11. void UdpLink::sendData(QByteArray ba)
    12. {
    13. socket.writeDatagram(ba, QHostAddress(ip), port);
    14. }
    1. #ifndef APP_H
    2. #define APP_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. class App : public QObject
    11. {
    12. Q_OBJECT
    13. Q_PROPERTY(bool isRuning READ getIsRuning WRITE setIsRuning NOTIFY isRuningChanged)
    14. public:
    15. explicit App(QObject *parent = nullptr);
    16. Q_INVOKABLE void start(QString ip);
    17. Q_INVOKABLE void stop();
    18. bool getIsRuning() const;
    19. void setIsRuning(bool newIsRuning);
    20. signals:
    21. void gyroValue(qreal x,qreal y,qreal z);
    22. void accelerValue(qreal x,qreal y,qreal z);
    23. void rotationValue(qreal x,qreal y,qreal z);
    24. void lightValue(qreal lux);
    25. void logInfo(QString str);
    26. void isRuningChanged();
    27. private:
    28. UdpLink udplink;
    29. bool isRuning{false};
    30. //陀螺
    31. QGyroscope *gyroscope;
    32. QGyroscopeReading *gyroreader;
    33. //加速度计
    34. QAccelerometer *acceler;
    35. QAccelerometerReading *accelereader;
    36. //旋转
    37. QRotationSensor *rotationSensor;
    38. QRotationReading *rotationReading;
    39. //光线
    40. QLightSensor *lightSensor;
    41. QLightReading *lightReading;
    42. };
    43. #endif // APP_H
    1. #include "app.h"
    2. #include <QtConcurrent>
    3. #include <chrono>
    4. #include <thread>
    5. #include <QJsonDocument>
    6. #include <QJsonArray>
    7. #include <QJsonObject>
    8. #include <QJsonValue>
    9. App::App(QObject *parent)
    10. : QObject{parent}
    11. {
    12. }
    13. void App::start(QString ip)
    14. {
    15. udplink.setAddress(ip,8023);
    16. qDebug()<<"start "<<ip;
    17. gyroscope = new QGyroscope(this);
    18. connect(gyroscope, &QGyroscope::readingChanged, this, [&](){
    19. gyroreader = gyroscope->reading();
    20. QJsonObject obj_root;
    21. QJsonArray arr;
    22. qreal gyroscopex = gyroreader->x();
    23. qreal gyroscopey = gyroreader->y();
    24. qreal gyroscopez = gyroreader->z();
    25. arr.append(QString::number(gyroscopex,'f',2));
    26. arr.append(QString::number(gyroscopey,'f',2));
    27. arr.append(QString::number(gyroscopez,'f',2));
    28. obj_root.insert("QGyroscope",arr);
    29. QJsonDocument jsonDocu(obj_root);
    30. QByteArray jsonData = jsonDocu.toJson();
    31. udplink.sendData(jsonData);
    32. emit gyroValue(gyroscopex,gyroscopey,gyroscopez);
    33. });
    34. acceler = new QAccelerometer(this);
    35. acceler->setAccelerationMode(QAccelerometer::Combined);
    36. connect(acceler, &QAccelerometer::readingChanged, this, [&](){
    37. accelereader = acceler->reading();
    38. QJsonObject obj_root;
    39. QJsonArray arr;
    40. qreal accelerx = accelereader->x();
    41. qreal accelery = accelereader->y();
    42. qreal accelerz = accelereader->z();
    43. arr.append(QString::number(accelerx,'f',2));
    44. arr.append(QString::number(accelery,'f',2));
    45. arr.append(QString::number(accelerz,'f',2));
    46. obj_root.insert("QAccelerometer",arr);
    47. QJsonDocument jsonDocu(obj_root);
    48. QByteArray jsonData = jsonDocu.toJson();
    49. udplink.sendData(jsonData);
    50. emit accelerValue(accelerx,accelery,accelerz);
    51. });
    52. rotationSensor = new QRotationSensor(this);
    53. connect(rotationSensor, &QRotationSensor::readingChanged, this, [&](){
    54. rotationReading = rotationSensor->reading();
    55. QJsonObject obj_root;
    56. QJsonArray arr;
    57. qreal rotationx = rotationReading->x();
    58. qreal rotationy = rotationReading->y();
    59. qreal rotationz = rotationReading->z();
    60. arr.append(QString::number(rotationx,'f',2));
    61. arr.append(QString::number(rotationy,'f',2));
    62. arr.append(QString::number(rotationz,'f',2));
    63. obj_root.insert("QRotationSensor",arr);
    64. QJsonDocument jsonDocu(obj_root);
    65. QByteArray jsonData = jsonDocu.toJson();
    66. udplink.sendData(jsonData);
    67. emit rotationValue(rotationx,rotationy,rotationz);
    68. });
    69. lightSensor = new QLightSensor(this);
    70. connect(lightSensor, &QLightSensor::readingChanged, this, [&](){
    71. lightReading = lightSensor->reading();
    72. QJsonObject obj_root;
    73. QJsonArray arr;
    74. qreal lux = lightReading->lux();
    75. arr.append(QString::number(lux,'f',2));
    76. obj_root.insert("QLightSensor",arr);
    77. QJsonDocument jsonDocu(obj_root);
    78. QByteArray jsonData = jsonDocu.toJson();
    79. udplink.sendData(jsonData);
    80. emit lightValue(lux);
    81. });
    82. if(gyroscope->start()&&acceler->start()&&rotationSensor->start()&&lightSensor->start()){
    83. setIsRuning(true);
    84. emit logInfo(QString::fromUtf8("启动成功"));
    85. }else{
    86. setIsRuning(false);
    87. emit logInfo(QString::fromUtf8("启动失败"));
    88. }
    89. }
    90. void App::stop()
    91. {
    92. gyroscope->stop();
    93. acceler->stop();
    94. rotationSensor->stop();
    95. lightSensor->stop();
    96. setIsRuning(false);
    97. }
    98. bool App::getIsRuning() const
    99. {
    100. return isRuning;
    101. }
    102. void App::setIsRuning(bool newIsRuning)
    103. {
    104. if (isRuning == newIsRuning)
    105. return;
    106. isRuning = newIsRuning;
    107. emit isRuningChanged();
    108. }
    1. import QtQuick 2.15
    2. import QtQuick.Window 2.15
    3. import QtQuick.Controls 2.15
    4. import QtQuick.Layouts 1.15
    5. import App 1.0
    6. Window {
    7. id:root
    8. width: 640
    9. height: 480
    10. visible: true
    11. title: qsTr("数据采集")
    12. App{
    13. id:app
    14. onGyroValue: {
    15. var str = '陀螺仪:'+x.toFixed(2)+' '+y.toFixed(2)+' '+z.toFixed(2)
    16. gyroLabel.text = str
    17. }
    18. onAccelerValue: {
    19. var str = '加速度计:'+x.toFixed(2)+' '+y.toFixed(2)+' '+z.toFixed(2)
    20. accelerLabel.text = str
    21. }
    22. onRotationValue: {
    23. var str = '旋转:'+x.toFixed(2)+' '+y.toFixed(2)+' '+z.toFixed(2)
    24. rotationLabel.text = str
    25. }
    26. onLightValue: {
    27. var str = '光线:'+lux.toFixed(2)
    28. lightLabel.text=str
    29. }
    30. onLogInfo: {
    31. debugInof.text=str
    32. }
    33. }
    34. RowLayout{
    35. id:topBar
    36. anchors.margins: 5
    37. anchors.top: parent.top
    38. anchors.left: parent.left
    39. spacing: 5
    40. Rectangle{
    41. id:address
    42. Layout.alignment: Qt.AlignHCenter
    43. height: linkBtn.height
    44. width: 200
    45. border.color: "black"
    46. border.width: 1
    47. TextInput{
    48. id:ip
    49. anchors.fill: parent
    50. verticalAlignment:Text.AlignVCenter
    51. horizontalAlignment:Text.AlignHCenter
    52. text: "192.168.1"
    53. }
    54. }
    55. Button{
    56. id:linkBtn
    57. Layout.alignment: Qt.AlignHCenter
    58. text: !app.isRuning?"启动":"停止"
    59. onClicked: {
    60. if(!app.isRuning){
    61. app.start(ip.text)
    62. }else{
    63. app.stop()
    64. }
    65. }
    66. }
    67. }
    68. ColumnLayout{
    69. anchors.left:parent.left
    70. anchors.right:parent.right
    71. anchors.top:topBar.bottom
    72. anchors.bottom:parent.bottom
    73. anchors.margins: 5
    74. Label{
    75. id:gyroLabel
    76. width: 200
    77. height: 50
    78. text: "陀螺仪"
    79. }
    80. Label{
    81. id:accelerLabel
    82. width: 200
    83. height: 50
    84. text: "加速度计"
    85. }
    86. Label{
    87. id:rotationLabel
    88. width: 200
    89. height: 50
    90. text: "旋转"
    91. }
    92. Label{
    93. id:lightLabel
    94. width: 200
    95. height: 50
    96. text:"光线"
    97. }
    98. TextEdit{
    99. id:debugInof
    100. height: 50
    101. }
    102. }
    103. }

  • 相关阅读:
    华为CSS堆叠技术介绍与实现
    车载以太网-传输层-UDP
    iVX低代码平台系列制作APP简单的个人界面
    如何使用 perf 分析 splice 中 pipe 的容量变化
    阿里云和AWS对比研究三——存储产品对比
    数据结构 专项练习
    借助Log360实现综合可见性的增强网络安全
    逻辑回归模型和Python代码实现
    本宁顿学院青年作家奖报名备赛
    java-php-python-ssm计算机类专业考研交流学习平台计算机毕业设计
  • 原文地址:https://blog.csdn.net/weixin_38416696/article/details/132823959