PDA

View Full Version : BLE Writing to Custom Characteristic



GeorgeIoak
1st March 2017, 20:51
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:


//GI new Code
// Write to motor
const QLowEnergyCharacteristic link = service->characteristic(QBluetoothUuid(Device_UUID_Motor_Ch aracteristic));
if (service->state() == QLowEnergyService::ServiceDiscovered){
service->QLowEnergyService::writeCharacteristic(link,QByteA rray::fromHex("01501000"),QLowEnergyService::WriteWithoutResponse);
}
//End GI Code

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


const QString Device_UUID_Motor_Characteristic = "{00010002-0000-1000-8000-00805F9B0421}";

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

GeorgeIoak
2nd March 2017, 17:39
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


void Device::connectToService(const QString &uuid)
{
QLowEnergyService *service = 0;
for (int i = 0; i < m_services.size(); i++) {
ServiceInfo *serviceInfo = (ServiceInfo*)m_services.at(i);
if (serviceInfo->getUuid() == uuid) {
service = serviceInfo->service();
break;
}
}

if (!service)
return;

qDeleteAll(m_characteristics);
m_characteristics.clear();
emit characteristicsUpdated();

if (service->state() == QLowEnergyService::DiscoveryRequired) {
//! [les-service-3]
connect(service, SIGNAL(stateChanged(QLowEnergyService::ServiceStat e)),
this, SLOT(serviceDetailsDiscovered(QLowEnergyService::S erviceState)));
service->discoverDetails();
setUpdate("Back\n(Discovering details...)");
//! [les-service-3]
return;
}

//discovery already done
const QList<QLowEnergyCharacteristic> chars = service->characteristics();
foreach (const QLowEnergyCharacteristic &ch, chars) {
CharacteristicInfo *cInfo = new CharacteristicInfo(ch);
m_characteristics.append(cInfo);
//GI new Code
// Write to motor
//const QLowEnergyCharacteristic link = service->characteristic(QBluetoothUuid(Device_UUID_Motor_Ch aracteristic));
const QLowEnergyCharacteristic link = service->characteristic(QBluetoothUuid(Device_UUID_Session_ Characteristic));
if (service->state() == QLowEnergyService::ServiceDiscovered){
//service->QLowEnergyService::writeCharacteristic(link,QByteA rray::fromHex("01991000"),QLowEnergyService::WriteWithoutResponse);
//service->writeCharacteristic(link,QByteArray::fromHex("01330100"),QLowEnergyService::WriteWithoutResponse); //Motor
service->writeCharacteristic(link,QByteArray::fromHex("000000003C03E80005015032"),QLowEnergyService::WriteWithoutResponse); //Session
}
//End GI Code
}



QTimer::singleShot(0, this, SIGNAL(characteristicsUpdated()));
}


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.