Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use QProcess for some program like "bluetoothctl"
Forum Updated to NodeBB v4.3 + New Features

How to use QProcess for some program like "bluetoothctl"

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 701 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mihan
    wrote on 2 Mar 2020, 09:00 last edited by
    #1

    Hi
    I want to use Qt Bluetooth but it's not useful on Qt 5.6.2( event on ubuntu or imx6 device ). It just can scan but can't pair and connect. But Qt 5.12.0 is ok I know.

    So I use QProcess to run bluetoothctl for control bluetooth, But I can't write and read anything. What should I do ?

    Regards
    Mihan

    J J 2 Replies Last reply 2 Mar 2020, 09:05
    0
    • M Mihan
      2 Mar 2020, 09:00

      Hi
      I want to use Qt Bluetooth but it's not useful on Qt 5.6.2( event on ubuntu or imx6 device ). It just can scan but can't pair and connect. But Qt 5.12.0 is ok I know.

      So I use QProcess to run bluetoothctl for control bluetooth, But I can't write and read anything. What should I do ?

      Regards
      Mihan

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 2 Mar 2020, 09:05 last edited by jsulm 3 Feb 2020, 09:06
      #2

      @Mihan said in How to use QProcess for some program like "bluetoothctl":

      What should I do ?

      Read documentation: https://doc.qt.io/qt-5/qprocess.html
      Connect needed signals to slots:

      • https://doc.qt.io/qt-5/qprocess.html#errorOccurred
      • https://doc.qt.io/qt-5/qprocess.html#readyReadStandardError
      • https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput

      "But I can't write and read anything" - what about showing the code, so others can actually see what could be the problem?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 2 Mar 2020, 09:22
      2
      • M Mihan
        2 Mar 2020, 09:00

        Hi
        I want to use Qt Bluetooth but it's not useful on Qt 5.6.2( event on ubuntu or imx6 device ). It just can scan but can't pair and connect. But Qt 5.12.0 is ok I know.

        So I use QProcess to run bluetoothctl for control bluetooth, But I can't write and read anything. What should I do ?

        Regards
        Mihan

        J Offline
        J Offline
        JonB
        wrote on 2 Mar 2020, 09:15 last edited by JonB 3 Feb 2020, 09:16
        #3

        @Mihan
        @jsulm's ...Read... are for reading back from the sub-process. You write to it (e.g. if you need to send it a "command") via a write() on your parent's stdout/file handle 1.

        1 Reply Last reply
        0
        • J jsulm
          2 Mar 2020, 09:05

          @Mihan said in How to use QProcess for some program like "bluetoothctl":

          What should I do ?

          Read documentation: https://doc.qt.io/qt-5/qprocess.html
          Connect needed signals to slots:

          • https://doc.qt.io/qt-5/qprocess.html#errorOccurred
          • https://doc.qt.io/qt-5/qprocess.html#readyReadStandardError
          • https://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput

          "But I can't write and read anything" - what about showing the code, so others can actually see what could be the problem?

          M Offline
          M Offline
          Mihan
          wrote on 2 Mar 2020, 09:22 last edited by
          #4

          @jsulm Sure, I have connected the signals to the slots, here is the test code

          untitled.pro

          #-------------------------------------------------
          #
          # Project created by QtCreator 2020-02-28T10:02:44
          #
          #-------------------------------------------------
          
          QT       += core gui \
                      multimedia \
                      bluetooth \
          
          
          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
          
          TARGET = untitled4
          TEMPLATE = app
          
          # The following define makes your compiler emit warnings if you use
          # any feature of Qt which has been marked as deprecated (the exact warnings
          # depend on your compiler). Please consult the documentation of the
          # deprecated API in order to know how to port your code away from it.
          DEFINES += QT_DEPRECATED_WARNINGS
          
          # You can also make your code fail to compile if you use deprecated APIs.
          # In order to do so, uncomment the following line.
          # You can also select to disable deprecated APIs only up to a certain version of Qt.
          #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
          
          CONFIG += c++11
          
          SOURCES += \
                  main.cpp \
                  mainwindow.cpp
          
          HEADERS += \
                  mainwindow.h
          
          FORMS += \
                  mainwindow.ui
          
          # Default rules for deployment.
          qnx: target.path = /tmp/$${TARGET}/bin
          else: unix:!android: target.path = /opt/$${TARGET}/bin
          !isEmpty(target.path): INSTALLS += target
          

          main.cpp

          #include "mainwindow.h"
          #include <QApplication>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              MainWindow w;
              w.show();
          
              return a.exec();
          }
          

          mainwindow.h

          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          #include <QProcess>
          
          namespace Ui {
          class MainWindow;
          }
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit MainWindow(QWidget *parent = nullptr);
              ~MainWindow();
          
          public slots:
              void BluetoothctlRecevie();
              void BluetoothctlError();
          
          private:
              Ui::MainWindow *ui;
          
              QProcess *m_Bluetoothctl;
          };
          
          #endif // MAINWINDOW_H
          

          mainwindow.cpp

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          
              m_Bluetoothctl = new QProcess(this);
              m_Bluetoothctl->setProgram("bluetoothctl");
          
              connect(m_Bluetoothctl,SIGNAL(readyRead()),this,SLOT(BluetoothctlRecevie()));
              connect(m_Bluetoothctl,SIGNAL(readyReadStandardError()),this,SLOT(BluetoothctlError()));
          
              m_Bluetoothctl->start();
          
              m_Bluetoothctl->write("scan on");
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          void MainWindow::BluetoothctlRecevie()
          {
              qDebug()<<m_Bluetoothctl->read(1024);
          }
          
          void MainWindow::BluetoothctlError()
          {
              qDebug()<<m_Bluetoothctl->readAllStandardError();
          }
          

          mainwindow.ui

          <?xml version="1.0" encoding="UTF-8"?>
          <ui version="4.0">
           <class>MainWindow</class>
           <widget class="QMainWindow" name="MainWindow">
            <property name="geometry">
             <rect>
              <x>0</x>
              <y>0</y>
              <width>400</width>
              <height>300</height>
             </rect>
            </property>
            <property name="windowTitle">
             <string>MainWindow</string>
            </property>
            <widget class="QWidget" name="centralWidget"/>
            <widget class="QMenuBar" name="menuBar">
             <property name="geometry">
              <rect>
               <x>0</x>
               <y>0</y>
               <width>400</width>
               <height>28</height>
              </rect>
             </property>
            </widget>
            <widget class="QToolBar" name="mainToolBar">
             <attribute name="toolBarArea">
              <enum>TopToolBarArea</enum>
             </attribute>
             <attribute name="toolBarBreak">
              <bool>false</bool>
             </attribute>
            </widget>
            <widget class="QStatusBar" name="statusBar"/>
           </widget>
           <layoutdefault spacing="6" margin="11"/>
           <resources/>
           <connections/>
          </ui>
          
          
          J 1 Reply Last reply 2 Mar 2020, 09:29
          0
          • M Mihan
            2 Mar 2020, 09:22

            @jsulm Sure, I have connected the signals to the slots, here is the test code

            untitled.pro

            #-------------------------------------------------
            #
            # Project created by QtCreator 2020-02-28T10:02:44
            #
            #-------------------------------------------------
            
            QT       += core gui \
                        multimedia \
                        bluetooth \
            
            
            greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
            
            TARGET = untitled4
            TEMPLATE = app
            
            # The following define makes your compiler emit warnings if you use
            # any feature of Qt which has been marked as deprecated (the exact warnings
            # depend on your compiler). Please consult the documentation of the
            # deprecated API in order to know how to port your code away from it.
            DEFINES += QT_DEPRECATED_WARNINGS
            
            # You can also make your code fail to compile if you use deprecated APIs.
            # In order to do so, uncomment the following line.
            # You can also select to disable deprecated APIs only up to a certain version of Qt.
            #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
            
            CONFIG += c++11
            
            SOURCES += \
                    main.cpp \
                    mainwindow.cpp
            
            HEADERS += \
                    mainwindow.h
            
            FORMS += \
                    mainwindow.ui
            
            # Default rules for deployment.
            qnx: target.path = /tmp/$${TARGET}/bin
            else: unix:!android: target.path = /opt/$${TARGET}/bin
            !isEmpty(target.path): INSTALLS += target
            

            main.cpp

            #include "mainwindow.h"
            #include <QApplication>
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                MainWindow w;
                w.show();
            
                return a.exec();
            }
            

            mainwindow.h

            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            #include <QProcess>
            
            namespace Ui {
            class MainWindow;
            }
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = nullptr);
                ~MainWindow();
            
            public slots:
                void BluetoothctlRecevie();
                void BluetoothctlError();
            
            private:
                Ui::MainWindow *ui;
            
                QProcess *m_Bluetoothctl;
            };
            
            #endif // MAINWINDOW_H
            

            mainwindow.cpp

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
                m_Bluetoothctl = new QProcess(this);
                m_Bluetoothctl->setProgram("bluetoothctl");
            
                connect(m_Bluetoothctl,SIGNAL(readyRead()),this,SLOT(BluetoothctlRecevie()));
                connect(m_Bluetoothctl,SIGNAL(readyReadStandardError()),this,SLOT(BluetoothctlError()));
            
                m_Bluetoothctl->start();
            
                m_Bluetoothctl->write("scan on");
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::BluetoothctlRecevie()
            {
                qDebug()<<m_Bluetoothctl->read(1024);
            }
            
            void MainWindow::BluetoothctlError()
            {
                qDebug()<<m_Bluetoothctl->readAllStandardError();
            }
            

            mainwindow.ui

            <?xml version="1.0" encoding="UTF-8"?>
            <ui version="4.0">
             <class>MainWindow</class>
             <widget class="QMainWindow" name="MainWindow">
              <property name="geometry">
               <rect>
                <x>0</x>
                <y>0</y>
                <width>400</width>
                <height>300</height>
               </rect>
              </property>
              <property name="windowTitle">
               <string>MainWindow</string>
              </property>
              <widget class="QWidget" name="centralWidget"/>
              <widget class="QMenuBar" name="menuBar">
               <property name="geometry">
                <rect>
                 <x>0</x>
                 <y>0</y>
                 <width>400</width>
                 <height>28</height>
                </rect>
               </property>
              </widget>
              <widget class="QToolBar" name="mainToolBar">
               <attribute name="toolBarArea">
                <enum>TopToolBarArea</enum>
               </attribute>
               <attribute name="toolBarBreak">
                <bool>false</bool>
               </attribute>
              </widget>
              <widget class="QStatusBar" name="statusBar"/>
             </widget>
             <layoutdefault spacing="6" margin="11"/>
             <resources/>
             <connections/>
            </ui>
            
            
            J Online
            J Online
            jsulm
            Lifetime Qt Champion
            wrote on 2 Mar 2020, 09:29 last edited by
            #5

            @Mihan said in How to use QProcess for some program like "bluetoothctl":

            QProcess *m_Bluetoothctl;

            Why not simply allocate on the stack?

            You should wait until the process started before writing (https://doc.qt.io/qt-5/qprocess.html#started):

            m_Bluetoothctl->start();
            m_Bluetoothctl->write("scan on");
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mihan
              wrote on 2 Mar 2020, 09:30 last edited by
              #6

              @JonB @jsulm
              I think I know the problem. It should be receive "\n" to confirm the command!

              1 Reply Last reply
              1

              1/6

              2 Mar 2020, 09:00

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved