aarelovich
aarelovich

Reputation: 5556

Qt BLE For Android: Cannot read value of characteristic for Custom Service

Here is the setup:

A coworker created a small firmware that changes the value of a custom characteristic within a custom service (non standard unique 128 bit UUID) about once every second. The device used to transmit uses a BLE (Bluetooth low power) implementation.

I needed to implement a small App (as a working example ONLY) to monitor said value. However I've run into a small problem. I've follwed the instructions here: http://doc.qt.io/qt-5/qtbluetooth-le-overview.html and I've manged to discover the service and "read it" (I get us UUID) by using this code:

void BLETest::on_stateChanged(QLowEnergyService::ServiceState state){
#ifdef DBUG
    logger->out("Service Monitor State: " + lowEnergyServiceStateToString(state),Logger::LC_ORANGE);
#endif

    if (state == QLowEnergyService::ServiceDiscovered){
        QString chars = "";
        QList<QLowEnergyCharacteristic> clist = monitoredService->characteristics();
        for (int i = 0; i < clist.size(); i++){
            chars = clist.at(i).uuid().toString() + " - " + clist.at(i).name() + ": " + QString(clist.at(i).value());
            chars = chars + ". Value size: " + QString::number(clist.at(i).value().size()) + "<br>";
        }
        if (chars.isEmpty()){
            chars = "No characteristics found";
        }
        logger->out(chars);
    }

}

Now this prints the UUID of the service but the size of the value byte array is zero. Using another (private App) we can actually see the value field for the characteristic in that service changing. Furthermore, even though there was a connection done to the service's object characteristicChanged singal, that signal is never triggered, which I imagine is because the characteristic value can't be read.

My question is: Is there anything wrong with the code that you can't think of? Or is it simply that is not possible to monitor custom services and characteristics with the current BLE implementation of Qt Bluetooth?

PD: I'm using Qt 5.7.1

Upvotes: 2

Views: 2520

Answers (1)

AD1170
AD1170

Reputation: 418

you must enable the chracteristic notification by write 0x01 to client characteristic configuration descriptor (CCCD).

foreach(QLowEnergyCharacteristic c, srv->characteristics()){
    QLowEnergyDescriptor d = c.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
    if(!c.isValid()){
        continue;
    }
    if(c.properties() & QLowEnergyCharacteristic::Notify){ // enable notification
        srv->writeDescriptor(d, QByteArray::fromHex("0100"));
    }
    if(c.properties() & QLowEnergyCharacteristic::Indicate){ // enable indication
        srv->writeDescriptor(d, QByteArray::fromHex("0200"));
    }
}

Upvotes: 3

Related Questions