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 connect BLE device on Win10 os?
Qt 6.11 is out! See what's new in the release blog

How to connect BLE device on Win10 os?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.4k 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.
  • Z Offline
    Z Offline
    Zhang_Yubiao
    wrote on last edited by
    #1

    void MainWindow::discoveryFinished()
    {
    qDebug()<<"discoveryFinished "+BTaddress;
    //static QString serviceUuid("00001101-0000-1000-8000-00805F9B34FB");
    static QString serviceUuid("6e400003-b5a3-f393-e0a9-e50e24dcca9e");
    socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
    socket->connectToService(QBluetoothAddress(BTaddress), QBluetoothUuid(serviceUuid),QIODevice::ReadWrite);
    qDebug()<<"connectFinished ";
    connect(socket,SIGNAL(readyRead()), this, SLOT(readBluetoothDataEvent()));
    qDebug()<<"connectreadBluetoothDataFinished ";
    connect(socket,SIGNAL(connected()), this, SLOT(bluetoothConnectedEvent()));
    qDebug()<<"connectbluetoothConnectedEvent ";
    }

    void MainWindow::bluetoothConnectedEvent()
    {
    qDebug()<<"bluetoothConnectedEvent";

    bluetoothDataSend("hellow bluetooth");
    

    }

    void MainWindow::readBluetoothDataEvent()
    {
    char data[100];
    qDebug()<<BTaddress + "ReadEvent";
    qint64 len = socket->read((char *)data, 100);

    QByteArray qa2((char*)data,len);
    QString qstr(qa2.toHex());//
    qDebug()<<"----"<<qstr.toUpper();
    }

    output:
    Starting E:\BLE_DEMO\build-Bluetooth_link-Desktop_Qt_5_11_1_MSVC2017_64bit-Debug\debug\Bluetooth_link.exe...
    "12:58:56:7A:4E:E5 S9"
    ""
    "00:02:5B:01:89:C7 CY5"
    ""
    "DE:89:F5:DF:AB:5A Nordic_UART"
    "DE:89:F5:DF:AB:5A"
    "discoveryFinished DE:89:F5:DF:AB:5A"
    connectFinished
    connectreadBluetoothDataFinished
    connectbluetoothConnectedEvent

    Why not excute void MainWindow::bluetoothConnectedEvent() & void MainWindow::readBluetoothDataEvent()?

    jsulmJ 1 Reply Last reply
    0
    • Z Zhang_Yubiao

      void MainWindow::discoveryFinished()
      {
      qDebug()<<"discoveryFinished "+BTaddress;
      //static QString serviceUuid("00001101-0000-1000-8000-00805F9B34FB");
      static QString serviceUuid("6e400003-b5a3-f393-e0a9-e50e24dcca9e");
      socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
      socket->connectToService(QBluetoothAddress(BTaddress), QBluetoothUuid(serviceUuid),QIODevice::ReadWrite);
      qDebug()<<"connectFinished ";
      connect(socket,SIGNAL(readyRead()), this, SLOT(readBluetoothDataEvent()));
      qDebug()<<"connectreadBluetoothDataFinished ";
      connect(socket,SIGNAL(connected()), this, SLOT(bluetoothConnectedEvent()));
      qDebug()<<"connectbluetoothConnectedEvent ";
      }

      void MainWindow::bluetoothConnectedEvent()
      {
      qDebug()<<"bluetoothConnectedEvent";

      bluetoothDataSend("hellow bluetooth");
      

      }

      void MainWindow::readBluetoothDataEvent()
      {
      char data[100];
      qDebug()<<BTaddress + "ReadEvent";
      qint64 len = socket->read((char *)data, 100);

      QByteArray qa2((char*)data,len);
      QString qstr(qa2.toHex());//
      qDebug()<<"----"<<qstr.toUpper();
      }

      output:
      Starting E:\BLE_DEMO\build-Bluetooth_link-Desktop_Qt_5_11_1_MSVC2017_64bit-Debug\debug\Bluetooth_link.exe...
      "12:58:56:7A:4E:E5 S9"
      ""
      "00:02:5B:01:89:C7 CY5"
      ""
      "DE:89:F5:DF:AB:5A Nordic_UART"
      "DE:89:F5:DF:AB:5A"
      "discoveryFinished DE:89:F5:DF:AB:5A"
      connectFinished
      connectreadBluetoothDataFinished
      connectbluetoothConnectedEvent

      Why not excute void MainWindow::bluetoothConnectedEvent() & void MainWindow::readBluetoothDataEvent()?

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Zhang_Yubiao Connect a slot to https://doc.qt.io/qt-5/qbluetoothsocket.html#error-1 and print the error when it is called: https://doc.qt.io/qt-5/qbluetoothsocket.html#errorString

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

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        General recommendation for Windows: use newest version of OS (Microsoft keeps fixing BLE support. On older versions it is either not working at all or crashing a lot) and moderately new version of Qt (anything newer than Qt 5.10 should work OK).

        (Z(:^

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          Zhang_Yubiao
          wrote on last edited by
          #4

          Win10 1903 18362.53
          Qt5.11.1\5.11.1\msvc2017_64

          Is it OK?

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Yep, should be fine.

            (Z(:^

            J.HilkJ 1 Reply Last reply
            0
            • sierdzioS sierdzio

              Yep, should be fine.

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @sierdzio are you sure ? Isn't the BTLE device discovery only "fixed" in 5.13 and up ?


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                Zhang_Yubiao
                wrote on last edited by
                #7

                13:53:16: Starting E:\BLE_DEMO\build-Bluetooth_link-Desktop_Qt_5_14_0_MSVC2017_64bit-Debug\debug\Bluetooth_link.exe ...
                "12:58:56:7A:4E:E5 S9"
                ""
                "00:02:5B:01:89:C7 CY5"
                ""
                "DE:89:F5:DF:AB:5A Nordic_UART"
                "DE:89:F5:DF:AB:5A"
                "discoveryFinished DE:89:F5:DF:AB:5A"
                connectFinished
                connectreadBluetoothDataFinished
                connectbluetoothConnectedEvent

                Use QT_5_14 same problem.

                J.HilkJ 1 Reply Last reply
                0
                • Z Zhang_Yubiao

                  13:53:16: Starting E:\BLE_DEMO\build-Bluetooth_link-Desktop_Qt_5_14_0_MSVC2017_64bit-Debug\debug\Bluetooth_link.exe ...
                  "12:58:56:7A:4E:E5 S9"
                  ""
                  "00:02:5B:01:89:C7 CY5"
                  ""
                  "DE:89:F5:DF:AB:5A Nordic_UART"
                  "DE:89:F5:DF:AB:5A"
                  "discoveryFinished DE:89:F5:DF:AB:5A"
                  connectFinished
                  connectreadBluetoothDataFinished
                  connectbluetoothConnectedEvent

                  Use QT_5_14 same problem.

                  J.HilkJ Online
                  J.HilkJ Online
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @Zhang_Yubiao
                  why do you use QBluetoothSocket and not QBluetoothDeviceDiscoveryAgent & QLowEnergyService

                  Or do you not target BlueTooth LowEnergy , but normal Bluetooth?


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    Zhang_Yubiao
                    wrote on last edited by
                    #9

                    MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                    {
                    ui->setupUi(this);

                    QBluetoothDeviceDiscoveryAgent *discoveryAgent;
                    discoveryAgent = new QBluetoothDeviceDiscoveryAgent;
                    connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(discoverBlueTooth(QBluetoothDeviceInfo)));
                    connect(discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished()));
                    discoveryAgent->start();
                    

                    }

                    Module is LowEnergy, is there problem in the code?

                    1 Reply Last reply
                    0

                    • Login

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