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

QT bug on Write DATA to bluetooth low energy device



  • void MainWindow::on_pushButton_send_clicked()
    {
    QByteArray array;

    for(int i=0;i<64;i++) array[i]=i;
    
    /*写入newValue作为特性的值。
    
     如果操作成功,将发射characteristicWritten()信号;
    
    低功耗设备: 每次最多写20个字节
    
    */
    
    
      m_service->writeCharacteristic(hrChar_w,array, QLowEnergyService::WriteWithoutResponse);
     // m_service->writeCharacteristic(hrChar_w,array, QLowEnergyService::WriteWithResponse);
    

    }

    When write data to bluetooth low energy device, qt output this message:
    Using QByteRef with an index pointing outside the valid range of a QByteArray. The corresponding behavior is deprecated, and will be changed in a future version of Qt.
    Using QByteRef with an index pointing outside the valid range of a QByteArray. The corresponding behavior is deprecated, and will be changed in a future version of Qt.
    Using QByteRef with an index pointing outside the valid range of a QByteArray. The corresponding behavior is deprecated, and will be changed in a future version of Qt.
    Using QByteRef with an index pointing outside the valid range of a QByteArray. The corresponding behavior is deprecated, and will be changed in a future version of Qt.

    Is it means so far qt is not suitable on this field? I used qt latest version of V5.14.2


  • Moderators

    @Zhang_Yubiao said in QT bug on Write DATA to bluetooth low energy device:

    QByteArray array;
    for(int i=0;i<64;i++) array[i]=i;

    Reserve the array's size first and warning should go away. Or use array.append(i); instead.



  • void MainWindow::on_pushButton_send_clicked()
    {
    QByteArray array;

    for(int i=0;i<64;i++) array.append(i); 
    
    /*写入newValue作为特性的值。
    
     如果操作成功,将发射characteristicWritten()信号;
    
    低功耗设备: 每次最多写20个字节
    
    */
    
    
      m_service->writeCharacteristic(hrChar_w,array, QLowEnergyService::WriteWithoutResponse);
     // m_service->writeCharacteristic(hrChar_w,array, QLowEnergyService::WriteWithResponse);
    

    }

    connect(m_service,&QLowEnergyService::stateChanged, this, this
    {
    if(m_service->state()==0)
    {
    qDebug() << "Write DATA cause Service invalid";
    qDebug() << "Controler state"<< m_controler->state() ;
    }
    qDebug() << "service state change" << m_service->state() << ",||||||----";

    QT output:
    13:45:48: Starting E:\BLE_DEMO\build-Bluetooth_link-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug\debug\Bluetooth_link.exe ...
    enter bludedevice constructor....
    find a new bluebooth device
    find a new bluebooth device
    device name::: "S9"
    device address::: "12:58:56:7A:4E:E5"
    device name::: "Nordic_UART"
    device address::: "DE:89:F5:DF:AB:5A"
    low Energy device ....
    low Energy device name::: "Nordic_UART"
    device name::: "Nordic_UART"
    device address::: "DE:89:F5:DF:AB:5A"
    m_controler connected ......
    list.count()= 0
    service state change QLowEnergyService::DiscoveringServices ,||||||----
    finish service discovery-----
    service state change QLowEnergyService::ServiceDiscovered ,||||||----

    Write DATA cause Service invalid
    Controler state QLowEnergyController::DiscoveredState
    service state change QLowEnergyService::InvalidService ,||||||----italicised text


  • Moderators

    Please wrap your code between code tags, it's very hard to read.

    Your service is invalid, you need to solve that before you can start writing characteristics.



  • Before writing service is valid, qt can read DADA from BLE device, after writing characteristics the service became invalid,and both read & write are all invalid.

    connect(m_controler,&QLowEnergyController::serviceDiscovered, this, [this](QBluetoothUuid serviceUuid)
    {
    if(serviceUuid == QBluetoothUuid(QUuid("6e400001-b5a3-f393-e0a9-e50e24dcca9e")))
    {
    //我们用的服务类型是0xffd0对应的uuid
    //发现匹配的服务后,使用控制器对象创建服务对象
    m_service = m_controler->createServiceObject(serviceUuid,this);

       if(m_service)
       {
         QList<QLowEnergyCharacteristic> list=m_service->characteristics();
    
         qDebug()<<"list.count()="<<list.count();
    
         //characteristics 获取详细特性
    
         SendMaxMode=list.count();  //设置模式选择上限
    
    
    
         // 服务对象创建成功后,坚挺服务状态变化,如果状态变成已发现,则进行后续服务下特征对象获取
         connect(m_service,&QLowEnergyService::stateChanged, this, [this]()
         {
             if(m_service->state()==0)
             {
                qDebug() << "Write DATA cause Service invalid";
                qDebug() << "Controler state"<< m_controler->state() ;
             }
             qDebug() << "service state change" << m_service->state() << ",||||||----";
    
    
             //发现服务, 建立characteristic对象实例
             if(m_service->state() == QLowEnergyService::ServiceDiscovered)
             {
                 QLowEnergyCharacteristic hrChar = m_service->characteristic(QBluetoothUuid(QUuid("6e400003-b5a3-f393-e0a9-e50e24dcca9e")));
    
                 if(!hrChar.isValid())
                 {
                     qDebug() << "characteristic 6e400003-b5a3-f393-e0a9-e50e24dcca9e error:::";
                 }
                 // 设置特征对象可用
                 //enable the chracteristic notification by write 0x01 to client characteristic configuration
                 QLowEnergyDescriptor m_notificationDesc = hrChar.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
                 if (m_notificationDesc.isValid())
                 {
                     if(hrChar.properties() & QLowEnergyCharacteristic::Notify) {
                          qDebug() << "";
                     }
                     m_service->writeDescriptor(m_notificationDesc, QByteArray::fromHex("0100"));
                 }
    
                 hrChar_w = m_service->characteristic(QBluetoothUuid(QUuid("6e400002-b5a3-f393-e0a9-e50e24dcca9e")));
    
                 if(!hrChar_w.isValid())
                 {
                     qDebug() << "characteristic 6e400002-b5a3-f393-e0a9-e50e24dcca9e error:::";
                 }
    
    
             }
    
    
         });
    
         connect(m_service, SIGNAL(characteristicChanged(QLowEnergyCharacteristic,QByteArray)),
    
                        this, SLOT(BleServiceCharacteristicChanged(QLowEnergyCharacteristic,QByteArray)));
    
         /*当特性值成功更改为newValue时,会发出此信号。*/
    
         connect(m_service, SIGNAL(characteristicWritten(QLowEnergyCharacteristic,QByteArray)),
    
                        this, SLOT(BleServiceCharacteristicWrite(QLowEnergyCharacteristic,QByteArray)));
    
    
         /*服务状态改变时发出此信号。newState也可以通过state()。*/
    
            connect(m_service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)),
    
                        this, SLOT(BleServiceServiceStateChanged(QLowEnergyService::ServiceState)));

  • Moderators

    Well, I don't know. Perhaps your device does not recognise that value?

    QByteArray::fromHex("0100") does not equal to 0x01.



  • Mobile APP can read from and write to the device. Qt only can read DATA from device.
    I don't think it's a device problem. Do you agree?


  • Moderators

    Yes that suggests the device is working fine. But it still does not confirm that it will understand a byte array derived from hex representation of 0100.


Log in to reply