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. BLE: sending data to device
Forum Updated to NodeBB v4.3 + New Features

BLE: sending data to device

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.1k Views
  • 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.
  • i.n.g.o.I Offline
    i.n.g.o.I Offline
    i.n.g.o.
    wrote on last edited by
    #1

    Hello.

    i'm asking myself if this is the way to send data to a BLE device:

    void BLESerialDevice::writeData(const QByteArray& data) {
    
        // get 20 bytes (?) to send
        dataSending = data.left(20);
        dataLeftToSend = data.right(data.size()-20);
    
        m_service->writeCharacteristic(writeCharacteristic, dataSending, QLowEnergyService::WriteWithResponse);
    }
    
    void BLESerialDevice::mycharacteristicWritten(const QLowEnergyCharacteristic &info, const QByteArray &value) {
        // checks removed (compare value to dataSending, etc)
    
        // write next 20 bytes
        writeData(dataLeftToSend);
    }
    
    ...
    
    // get data and write it out
    void doSendData() {
        QByteArray data = getSomeData();
        MyBLESerialDevice.writeData(data);
    }
    

    sending data with this method transmits all the bytes, only the data-rate if pretty low:

    sent  2429 bytes  in  9243  ms  =  ~263 bytes per seconds
    

    trying to send it in a tight loop would drop data...

    void BLESerialDevice::writeData(const QByteArray& data) {
        int i = 0;
        while (true) {
            dataSending = data.mid(i, 20);
    
            if (dataSending.isEmpty()) break;
    
            m_service->writeCharacteristic(writeCharacteristic, dataSending, QLowEnergyService::WriteWithResponse); 
    
            i += 20;
        };
    }
    

    so i wonder:
    is this (first code-snippet) the way to send data to a BLE device?
    how would the preferred way to write data be?
    how could the data-rate be improved?

    thanks for comments and recommendations
    io

    J.HilkJ 1 Reply Last reply
    0
    • i.n.g.o.I i.n.g.o.

      Hello.

      i'm asking myself if this is the way to send data to a BLE device:

      void BLESerialDevice::writeData(const QByteArray& data) {
      
          // get 20 bytes (?) to send
          dataSending = data.left(20);
          dataLeftToSend = data.right(data.size()-20);
      
          m_service->writeCharacteristic(writeCharacteristic, dataSending, QLowEnergyService::WriteWithResponse);
      }
      
      void BLESerialDevice::mycharacteristicWritten(const QLowEnergyCharacteristic &info, const QByteArray &value) {
          // checks removed (compare value to dataSending, etc)
      
          // write next 20 bytes
          writeData(dataLeftToSend);
      }
      
      ...
      
      // get data and write it out
      void doSendData() {
          QByteArray data = getSomeData();
          MyBLESerialDevice.writeData(data);
      }
      

      sending data with this method transmits all the bytes, only the data-rate if pretty low:

      sent  2429 bytes  in  9243  ms  =  ~263 bytes per seconds
      

      trying to send it in a tight loop would drop data...

      void BLESerialDevice::writeData(const QByteArray& data) {
          int i = 0;
          while (true) {
              dataSending = data.mid(i, 20);
      
              if (dataSending.isEmpty()) break;
      
              m_service->writeCharacteristic(writeCharacteristic, dataSending, QLowEnergyService::WriteWithResponse); 
      
              i += 20;
          };
      }
      

      so i wonder:
      is this (first code-snippet) the way to send data to a BLE device?
      how would the preferred way to write data be?
      how could the data-rate be improved?

      thanks for comments and recommendations
      io

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @i.n.g.o.
      Hi, the First version is, roughly, the way I do it currently too. You‘re actually faster, because I‘m bound to a Modbus type of protocol that take 8 byte away for a head and Tail.
      😔


      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
      1
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by kuzulis
        #3

        BLE is not designed to fast data transferring.

        PS: Also many things depends on type of your remote device: who did FW (maybe it has bugs or sleeps), what is a characteristic's properties, what is a HW vendor (TI, NORDIC and etc).

        1 Reply Last reply
        3
        • i.n.g.o.I Offline
          i.n.g.o.I Offline
          i.n.g.o.
          wrote on last edited by
          #4

          thanks for the comments!

          1 Reply Last reply
          1

          • Login

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