您当前的位置: 首页 >  嵌入式

正点原子

暂无认证

  • 0浏览

    0关注

    382博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【正点原子Linux连载】第二十章 USB Bluetooth 摘自【正点原子】I.MX6U嵌入式Qt开发指南V1.0.2

正点原子 发布时间:2022-07-15 11:56:56 ,浏览量:0

1)实验平台:正点原子阿尔法Linux开发板 2)平台购买地址:https://item.taobao.com/item.htm?id=603672744434 3)全套实验源码+手册+视频下载地址:http://www.openedv.com/thread-300792-1-1.html 4)对正点原子Linux感兴趣的同学可以加群讨论:935446741

第二十章 USB Bluetooth

Qt官方提供了蓝牙的相关类和API函数,也提供了相关的例程给我们参考。编者根据Qt官方的例程编写出适合我们Ubuntu和正点原子I.MX6U开发板的例程。注意Windows上不能使用Qt的蓝牙例程,因为底层需要有BlueZ协议栈,而Windows没有。Windows可能需要去移植。编者就不去探究了。确保我们正点原子I.MX6U开发板与Ubuntu可用即可,所以大家还是老实的用Ubuntu来开发吧!

20.1 资源简介

在正点原子IMX6U开发板上虽然没有带板载蓝牙,但是可以外接免驱USB蓝牙,直接在USB接口插上一个USB蓝牙模块就可以进行本章节的实验了。详细请看【正点原子】I.MX6U用户快速体验V1.x.pdf的第3.29小节蓝牙测试,先了解蓝牙是如何在Linux上如何使用的,切记先看正点原子快速体验文档,了解用哪种蓝牙芯片,和怎么测试蓝牙的。本Qt教程就不再介绍了。

20.2 应用实例

项目简介:Qt蓝牙聊天。将蓝牙设置成一个服务器,或者用做客户端,连接手机即可通信。 例06_bluetooth_chat,Qt蓝牙聊天(难度:难)。项目路径为Qt/3/06_bluetooth_chat。 Qt使用蓝牙,需要在项目文件加上相应的蓝牙模块。添加的代码如下红色加粗部分。06_bluetooth_chat.pro文件代码如下。

1   QT       += core gui bluetooth
2 
3   greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4 
5   CONFIG += c++11
6 
7   # The following define makes your compiler emit warnings if you use
8   # any Qt feature that has been marked deprecated (the exact warnings
9   # depend on your compiler). Please consult the documentation of the
10  # deprecated API in order to know how to port your code away from it.
11  DEFINES += QT_DEPRECATED_WARNINGS
12
13  # You can also make your code fail to compile if it uses deprecated APIs.
14  # In order to do so, uncomment the following line.
15  # You can also select to disable deprecated APIs only up to a certain version of Qt.
16  #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17
18  SOURCES += \
19      chatclient.cpp \
20      chatserver.cpp \
21      main.cpp \
22      mainwindow.cpp \
23      remoteselector.cpp
24
25  HEADERS += \
26      chatclient.h \
27      chatserver.h \
28      mainwindow.h \
29      remoteselector.h
30
31  # Default rules for deployment.
32  qnx: target.path = /tmp/$${TARGET}/bin
33  else: unix:!android: target.path = /opt/$${TARGET}/bin
34  !isEmpty(target.path): INSTALLS += target
第18~29行,可以看到我们的项目组成文件。一个客户端,一个服务端,一个主界面和一个远程选择蓝牙的文件。总的看起来有四大部分,下面就介绍这四大部分的文件。
chatclient.h的代码如下。
   /******************************************************************
    Copyright (C) 2015 The Qt Company Ltd.
    Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
    * @projectName   06_bluetooth_chat
    * @brief         chatclient.h
    * @author        Deng Zhimao
    * @email         1252699831@qq.com
    * @net            www.openedv.com
    * @date           2021-03-20
    *******************************************************************/
1   #ifndef CHATCLIENT_H
2   #define CHATCLIENT_H
3 
4   #include 
5   #include 
6   #include 
7 
8   QT_FORWARD_DECLARE_CLASS(QBluetoothSocket)
9 
10  class ChatClient : public QObject
11  {
12      Q_OBJECT
13
14  public:
15      explicit ChatClient(QObject *parent = nullptr);
16      ~ChatClient();
17
18      /* 开启客户端 */
19      void startClient(const QBluetoothServiceInfo &remoteService);
20
21      /* 停止客户端 */
22      void stopClient();
23
24  public slots:
25      /* 发送消息 */
26      void sendMessage(const QString &message);
27
28      /* 主动断开连接 */
29      void disconnect();
30
31  signals:
32      /* 接收到消息信号 */
33      void messageReceived(const QString &sender, const QString &message);
34
35      /* 连接信号 */
36      void connected(const QString &name);
37
38      /* 断开连接信号 */
39      void disconnected();
40
41  private slots:
42      /* 从socket里读取消息 */
43      void readSocket();
44
45      /* 连接 */
46      void connected();
47
48  private:
49      /* socket通信 */
50      QBluetoothSocket *socket;
51  };
52
53  #endif // CHATCLIENT_H
chatclient.h文件主要是客户端的头文件,其中写一些接口,比如开启客户端,关闭客户端,接收信号与关闭信号等等。

chatclient.cpp的代码如下。

    /******************************************************************
    Copyright (C) 2015 The Qt Company Ltd.
    Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
    * @projectName   06_bluetooth_chat
    * @brief         chatclient.cpp
    * @author        Deng Zhimao
    * @email         1252699831@qq.com
    * @net            www.openedv.com
    * @date           2021-03-20
    *******************************************************************/

1   #include "chatclient.h"
2   #include 
3 
4   ChatClient::ChatClient(QObject *parent)
5       :   QObject(parent), socket(0)
6   {
7   }
8 
9   ChatClient::~ChatClient()
10  {
11      stopClient();
12  }
13
14  /* 开启客户端 */
15  void ChatClient::startClient(const QBluetoothServiceInfo &remoteService)
16  {
17      if (socket)
18          return;
19      
20      // Connect to service
21      socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
22      qDebug() peerName(),
50                               QString::fromUtf8(line.constData(), 
51                                                 line.length()));
52      }
53  }
54
55  /* 发送的消息 */
56  void ChatClient::sendMessage(const QString &message)
57  {
58      qDebug()peerName());
68  }
69
70  /* 主动断开连接*/
71  void ChatClient::disconnect() {
72      qDebug()            
关注
打赏
1665308814
查看更多评论
0.0499s