Results 1 to 3 of 3

Thread: How to write to BLE Characteristic?

  1. #1
    Join Date
    Aug 2019
    Posts
    2

    Default How to write to BLE Characteristic?

    Please refer my code

    Qt Code:
    1. #include "bluetoothupdater.h"
    2.  
    3. BluetoothUpdater::BluetoothUpdater()
    4. {
    5. QBluetoothDeviceDiscoveryAgent* discovery = new QBluetoothDeviceDiscoveryAgent();
    6.  
    7. connect(discovery, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, [&] (const QBluetoothDeviceInfo &device)
    8. {
    9. if(device.name() == "Targ")
    10. {
    11. qDebug() << "Found new device:" << device.name();
    12. ptrQBluetoothDeviceInfo = new QBluetoothDeviceInfo(QBluetoothAddress(device.address()), device.name(), 0);
    13. m_control = new QLowEnergyController(*ptrQBluetoothDeviceInfo);
    14. m_control->connectToDevice();
    15. m_control->discoverServices();
    16. //connect(m_control, &QLowEnergyService::stateChanged, this, &BluetoothUpdater::service serviceDiscovered, this, &BluetoothUpdater::serviceDiscovered);
    17. connect(m_control, &QLowEnergyController::serviceDiscovered, this, &BluetoothUpdater::serviceDiscovered);
    18. connect(m_control, &QLowEnergyController::discoveryFinished, this, &BluetoothUpdater::serviceScanDone);
    19. }
    20. });
    21.  
    22. //connect(discovery, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BluetoothUpdater::addDevice);
    23. discovery->setLowEnergyDiscoveryTimeout(5000);
    24. discovery->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
    25. }
    26.  
    27. void BluetoothUpdater::serviceScanDone()
    28. {
    29. qDebug() << "Service scan done.";
    30.  
    31. if (m_foundHeartRateService)
    32. {
    33. qDebug() << m_foundHeartRateService;
    34. m_service = m_control->createServiceObject(QBluetoothUuid(DFUService));
    35. qDebug() << m_service;
    36. }
    37.  
    38. if (m_service)
    39. {
    40. qDebug() << "DFU Service found";
    41. m_service->discoverDetails();
    42. connect(m_service, &QLowEnergyService::stateChanged, this, &BluetoothUpdater::serviceStateChanged);
    43. connect(m_service, &QLowEnergyService::characteristicChanged, this, &BluetoothUpdater::updateValue);
    44. connect(m_service, &QLowEnergyService::descriptorWritten, this, &BluetoothUpdater::confirmedDescriptorWrite);
    45. }
    46. else
    47. {
    48. qDebug() << "DFU Service not found.";
    49. }
    50. }
    51.  
    52. void BluetoothUpdater::serviceDiscovered(const QBluetoothUuid &gatt)
    53. {
    54. if (gatt == QBluetoothUuid(DFUService))
    55. {
    56. qDebug() << "DFU service discovered.";
    57. m_foundHeartRateService = true;
    58. }
    59. }
    60.  
    61. void BluetoothUpdater::serviceStateChanged(QLowEnergyService::ServiceState state)
    62. {
    63. if(state == QLowEnergyService::ServiceDiscovered)
    64. {
    65. ControlPointChar = m_service->characteristic(QBluetoothUuid(ControlPointUUID));
    66. if(ControlPointChar.isValid())
    67. {
    68. qDebug() << "Control Point Characteristic Found";
    69. m_service->writeCharacteristic(ControlPointChar, QByteArray::fromHex("0601"), QLowEnergyService::WriteWithResponse);
    70. }
    71. else
    72. {
    73. qDebug() << "Control Point Characteristic Not Found";
    74. }
    75. ControlPointDesc = ControlPointChar.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
    76. if (ControlPointDesc.isValid())
    77. {
    78. qDebug() << "Descriptor";
    79. m_service->writeDescriptor(ControlPointDesc, QByteArray::fromHex("0100"));
    80. }
    81. }
    82. }
    83.  
    84. void BluetoothUpdater::updateValue(const QLowEnergyCharacteristic &c, const QByteArray &value)
    85. {
    86. qDebug() << "updateValue, c.uuid:" << c.uuid();
    87. qDebug() << "updateValue, value:" << value;
    88. }
    89.  
    90. void BluetoothUpdater::confirmedDescriptorWrite(const QLowEnergyDescriptor &d, const QByteArray &value)
    91. {
    92. qDebug() << "confirmedDescriptorWrite:" << value;
    93. if (d.isValid() && d == ControlPointDesc && value == QByteArray::fromHex("0000"))
    94. {
    95. qDebug() << "Inside confirmedDescriptorWrite";
    96. m_control->disconnectFromDevice();
    97. delete m_service;
    98. m_service = nullptr;
    99. }
    100. }
    To copy to clipboard, switch view to plain text mode 

    This line
    Qt Code:
    1. m_service->writeCharacteristic(ControlPointChar, QByteArray::fromHex("0601"), QLowEnergyService::WriteWithResponse);
    To copy to clipboard, switch view to plain text mode 
    does not work

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,165
    Thanks
    235
    Thanked 662 Times in 652 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to write to BLE Characteristic?

    does not work
    Doesn't compile?
    Doesn't link?
    Doesn't execute?
    Doesn't produce the desired result?
    Or is just tired and didn't want to work?

    Sorry, I can't be of more help, but my crystal ball is out of service today.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Aug 2019
    Posts
    2

    Default Re: How to write to BLE Characteristic?

    My bad!!

    Qt Code:
    1. #include "bluetoothupdater.h"
    2.  
    3. BluetoothUpdater::BluetoothUpdater()
    4. {
    5. QBluetoothDeviceDiscoveryAgent* discovery = new QBluetoothDeviceDiscoveryAgent();
    6.  
    7. connect(discovery, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, [&] (const QBluetoothDeviceInfo &device)
    8. {
    9. if(device.name() == "Targ")
    10. {
    11. qDebug() << "Found new device:" << device.name();
    12. ptrQBluetoothDeviceInfo = new QBluetoothDeviceInfo(QBluetoothAddress(device.address()), device.name(), 0);
    13. m_control = new QLowEnergyController(*ptrQBluetoothDeviceInfo);
    14. m_control->connectToDevice();
    15. m_control->discoverServices();
    16. connect(m_control, &QLowEnergyController::serviceDiscovered, this, &BluetoothUpdater::serviceDiscovered);
    17. connect(m_control, &QLowEnergyController::discoveryFinished, this, &BluetoothUpdater::serviceScanDone);
    18. }
    19. });
    20.  
    21. discovery->setLowEnergyDiscoveryTimeout(5000);
    22. discovery->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
    23. }
    24.  
    25. void BluetoothUpdater::serviceScanDone()
    26. {
    27. qDebug() << "Service scan done.";
    28.  
    29. if (m_foundHeartRateService)
    30. {
    31. qDebug() << m_foundHeartRateService;
    32. m_service = m_control->createServiceObject(QBluetoothUuid(DFUService));
    33. qDebug() << m_service;
    34. }
    35.  
    36. if (m_service)
    37. {
    38. qDebug() << "DFU Service found";
    39. m_service->discoverDetails();
    40. connect(m_service, &QLowEnergyService::stateChanged, this, &BluetoothUpdater::serviceStateChanged);
    41. connect(m_service, &QLowEnergyService::characteristicChanged, this, &BluetoothUpdater::updateValue);
    42. connect(m_service, &QLowEnergyService::descriptorWritten, this, &BluetoothUpdater::confirmedDescriptorWrite);
    43. }
    44. else
    45. {
    46. qDebug() << "DFU Service not found.";
    47. }
    48. }
    49.  
    50. void BluetoothUpdater::serviceDiscovered(const QBluetoothUuid &gatt)
    51. {
    52. if (gatt == QBluetoothUuid(DFUService))
    53. {
    54. qDebug() << "DFU service discovered.";
    55. m_foundHeartRateService = true;
    56. }
    57. }
    58.  
    59. void BluetoothUpdater::serviceStateChanged(QLowEnergyService::ServiceState state)
    60. {
    61. if(state == QLowEnergyService::ServiceDiscovered)
    62. {
    63. ControlPointChar = m_service->characteristic(QBluetoothUuid(ControlPointUUID));
    64. if(ControlPointChar.isValid())
    65. {
    66. qDebug() << "Control Point Characteristic Found";
    67. ControlPointDesc = ControlPointChar.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
    68. if (ControlPointDesc.isValid())
    69. {
    70. qDebug() << "Control Point Descriptor";
    71. m_service->writeDescriptor(ControlPointDesc, QByteArray::fromHex("0100"));
    72. }
    73. WriteControl = 1;
    74. }
    75. else
    76. {
    77. qDebug() << "Control Point Characteristic Not Found";
    78. WriteControl = 0;
    79. }
    80.  
    81. PacketChar = m_service->characteristic(QBluetoothUuid(PacketUUID));
    82. if(PacketChar.isValid())
    83. {
    84. qDebug() << "Packet Characteristic Found";
    85. WritePacket = 1;
    86. }
    87. else
    88. {
    89. qDebug() << "Packet Characteristic Not Found";
    90. WritePacket = 0;
    91. }
    92.  
    93. if((WriteControl == 1) && (WritePacket == 1))
    94. {
    95. QFile InitFS("C:/Users/Sonal/Documents/data_glove_editor/res/firmwareUpdate/app.dat"); // Init dat file
    96. if (!InitFS.open(QIODevice::ReadOnly))
    97. {
    98. qDebug() << "Init file cannot be opened";
    99. return;
    100. }
    101. else
    102. {
    103. qDebug() << "File opened";
    104.  
    105. m_service->writeCharacteristic(ControlPointChar, QByteArray::fromHex("0601"), QLowEnergyService::WriteWithResponse);
    106. QThread::msleep(100);
    107.  
    108. m_service->writeCharacteristic(ControlPointChar, QByteArray::fromHex("01018E000000"), QLowEnergyService::WriteWithResponse);
    109. QThread::msleep(100);
    110.  
    111. qDebug() << "Init File Length = " << InitFS.size();
    112.  
    113. int NoOfInitFilePackets = InitFS.size() / BLEPacketLength;
    114. qDebug() << "No Of Init File Packets = " << NoOfInitFilePackets;
    115.  
    116. int InitFileBytesRemaining = InitFS.size() % BLEPacketLength;
    117. qDebug() << "Init File Bytes Remaining = " << InitFileBytesRemaining;
    118.  
    119. for (InitFilePacketCounter = 0, InitFilePointer = 0; InitFilePacketCounter < NoOfInitFilePackets; InitFilePacketCounter++, InitFilePointer += 20)
    120. {
    121. InitFS.seek(InitFilePointer);
    122. InitBuffer = InitFS.read(BLEPacketLength);
    123. //qDebug() << "InitFilePointer = " << InitFilePointer;
    124. qDebug() << "InitBuffer = " << InitBuffer.toHex();
    125. m_service->writeCharacteristic(PacketChar, QByteArray::fromHex(InitBuffer), QLowEnergyService::WriteWithoutResponse);
    126. QThread::msleep(100);
    127. }
    128.  
    129. InitFS.seek(InitFilePointer);
    130. InitFileBytesLeft = InitFS.read(InitFileBytesRemaining);
    131. qDebug() << "InitFileBytesLeft = " << InitFileBytesLeft.toHex();
    132.  
    133. m_service->writeCharacteristic(PacketChar, QByteArray::fromHex(InitFileBytesLeft), QLowEnergyService::WriteWithoutResponse);
    134. QThread::msleep(100);
    135.  
    136. m_service->writeCharacteristic(ControlPointChar, QByteArray::fromHex("03"), QLowEnergyService::WriteWithResponse);
    137. QThread::msleep(100);
    138.  
    139. m_service->writeCharacteristic(ControlPointChar, QByteArray::fromHex("04"), QLowEnergyService::WriteWithResponse);
    140. QThread::msleep(100);
    141. }
    142. }
    143. }
    144. }
    145.  
    146. void BluetoothUpdater::updateValue(const QLowEnergyCharacteristic &c, const QByteArray &value)
    147. {
    148. //qDebug() << "updateValue, c.uuid:" << c.uuid();
    149. qDebug() << " updateValue, value:" << value.toHex();
    150. }
    151.  
    152. void BluetoothUpdater::confirmedDescriptorWrite(const QLowEnergyDescriptor &d, const QByteArray &value)
    153. {
    154. qDebug() << "confirmedDescriptorWrite:" << value.toHex();
    155. if (d.isValid() && d == ControlPointDesc && value == QByteArray::fromHex("0000"))
    156. {
    157. qDebug() << "Inside confirmedDescriptorWrite";
    158. m_control->disconnectFromDevice();
    159. delete m_service;
    160. m_service = nullptr;
    161. }
    162. }
    To copy to clipboard, switch view to plain text mode 

    I moved a step further. New code is pasted above. I'm not getting responses for the last 2 "m_service->writeCharacteristic(ControlPointChar,....". If I comment "m_service->writeCharacteristic(PacketChar, ... ", then I get responses for "m_service->writeCharacteristic(ControlPointChar, ...". I always get responses for first 2 "m_service->writeCharacteristic(ControlPointChar, ...". Is there some problem with creating the handles to 2 different characteristics using "m_service->writeCharacteristic"?? When I say I'm getting responses, it means that "updateValue" is being called each time I write to ControlPointChar. What am I missing here? Why multiple writes to multiple characteristics is failing??
    Last edited by BB; 21st August 2019 at 01:45.

Similar Threads

  1. BLE Writing to Custom Characteristic
    By GeorgeIoak in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2017, 17:39
  2. Best way to write to a file
    By The 11th plague of Egypt in forum Newbie
    Replies: 4
    Last Post: 25th August 2011, 15:43
  3. How to write in a QFile
    By gorka_sm in forum Newbie
    Replies: 2
    Last Post: 5th May 2011, 13:08
  4. Replies: 2
    Last Post: 2nd November 2010, 06:15
  5. TCP Write Raw data
    By ^NyAw^ in forum General Programming
    Replies: 19
    Last Post: 23rd November 2007, 17:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.