Results 1 to 2 of 2

Thread: BLE Writing to Custom Characteristic

  1. #1
    Join Date
    Mar 2017
    Posts
    3
    Qt products
    Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default BLE Writing to Custom Characteristic

    I am more of a hardware person than software so I'm looking for some help. I am modifying the lowenergyscanner example project so I can write to a custom characteristic. The example project is able to find our device (I am currently running this on Mac OS), find the services, and then if I select our custom service it is able to find all of the characteristics.

    I've searched and found some posts on the internet but I have not been successful in writing to our device. In device.cpp I have added:

    Qt Code:
    1. //GI new Code
    2. // Write to motor
    3. const QLowEnergyCharacteristic link = service->characteristic(QBluetoothUuid(Device_UUID_Motor_Characteristic));
    4. if (service->state() == QLowEnergyService::ServiceDiscovered){
    5. service->QLowEnergyService::writeCharacteristic(link,QByteArray::fromHex("01501000"),QLowEnergyService::WriteWithoutResponse);
    6. }
    7. //End GI Code
    To copy to clipboard, switch view to plain text mode 

    In device.h I added the following constant. I tried both with and without the {}

    Qt Code:
    1. const QString Device_UUID_Motor_Characteristic = "{00010002-0000-1000-8000-00805F9B0421}";
    To copy to clipboard, switch view to plain text mode 

    I am able to debug the app and depending on where this code is placed it will execute but it does not activate the motor (the code sequence will activate the motor and then stop it after a short period)

    For now I just want to write 0x01 0x50 0x10 0x00 to the Motor characteristic which has a UUID of 00010002-0000-1000-8000-00805F9B0421 and is part of our custom service which has a UUID of "00010000-0000-1000-8000-00805F9B0421"

    I have been able to use BLE apps on a mobile phone and successfully written this to our board but I have not been successful with this Qt.

    Ideally once I have performed this I would like to modify the app to directly connect to our board using our board name and then issue this command so I can verify that the board is working.

    Thanks in advance for your help.

    George

  2. #2
    Join Date
    Mar 2017
    Posts
    3
    Qt products
    Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: BLE Writing to Custom Characteristic

    Last night I was able to make a little progress. What I found is that after the program finds and lists all of the characteristics for a selected service (the custom one in my case) if you hit the Back button and select the service again the code that you have for writing to the characteristic will execute. Here is the section of code I am working with in device.cpp

    Qt Code:
    1. void Device::connectToService(const QString &uuid)
    2. {
    3. QLowEnergyService *service = 0;
    4. for (int i = 0; i < m_services.size(); i++) {
    5. ServiceInfo *serviceInfo = (ServiceInfo*)m_services.at(i);
    6. if (serviceInfo->getUuid() == uuid) {
    7. service = serviceInfo->service();
    8. break;
    9. }
    10. }
    11.  
    12. if (!service)
    13. return;
    14.  
    15. qDeleteAll(m_characteristics);
    16. m_characteristics.clear();
    17. emit characteristicsUpdated();
    18.  
    19. if (service->state() == QLowEnergyService::DiscoveryRequired) {
    20. //! [les-service-3]
    21. connect(service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)),
    22. this, SLOT(serviceDetailsDiscovered(QLowEnergyService::ServiceState)));
    23. service->discoverDetails();
    24. setUpdate("Back\n(Discovering details...)");
    25. //! [les-service-3]
    26. return;
    27. }
    28.  
    29. //discovery already done
    30. const QList<QLowEnergyCharacteristic> chars = service->characteristics();
    31. foreach (const QLowEnergyCharacteristic &ch, chars) {
    32. CharacteristicInfo *cInfo = new CharacteristicInfo(ch);
    33. m_characteristics.append(cInfo);
    34. //GI new Code
    35. // Write to motor
    36. //const QLowEnergyCharacteristic link = service->characteristic(QBluetoothUuid(Device_UUID_Motor_Characteristic));
    37. const QLowEnergyCharacteristic link = service->characteristic(QBluetoothUuid(Device_UUID_Session_Characteristic));
    38. if (service->state() == QLowEnergyService::ServiceDiscovered){
    39. //service->QLowEnergyService::writeCharacteristic(link,QByteArray::fromHex("01991000"),QLowEnergyService::WriteWithoutResponse);
    40. //service->writeCharacteristic(link,QByteArray::fromHex("01330100"),QLowEnergyService::WriteWithoutResponse); //Motor
    41. service->writeCharacteristic(link,QByteArray::fromHex("000000003C03E80005015032"),QLowEnergyService::WriteWithoutResponse); //Session
    42. }
    43. //End GI Code
    44. }
    45.  
    46.  
    47.  
    48. QTimer::singleShot(0, this, SIGNAL(characteristicsUpdated()));
    49. }
    To copy to clipboard, switch view to plain text mode 

    I was experimenting with writing to different characteristics and was able to but this is not the ideal method for what I am trying to achieve. My next step would be to take the current code and instead of scanning for services and characteristics I can go directly to connecting to the device (based on the product name) and write and read to a few different characteristics.

    Any helpful advice with moving along with this would be greatly appreciated.

Similar Threads

  1. writing txt to template
    By terhje in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2011, 15:08
  2. Need help writing my first Qt app
    By netnut in forum Newbie
    Replies: 4
    Last Post: 16th December 2010, 11:52
  3. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 21:23
  4. Replies: 1
    Last Post: 26th January 2007, 08:10
  5. Writing Plugins for Qt
    By sarode in forum Qt Programming
    Replies: 4
    Last Post: 29th May 2006, 10:04

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.