Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to connect BLE device on Win10 os?



  • 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()?


  • Lifetime Qt Champion


  • Moderators

    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).



  • Win10 1903 18362.53
    Qt5.11.1\5.11.1\msvc2017_64

    Is it OK?


  • Moderators

    Yep, should be fine.


  • Moderators

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



  • 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.


  • Moderators

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

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



  • 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?


Log in to reply