0

I am trying to retrieve Service Data information from Bluetooth devices using QBluetoothDeviceInfo class (Qt 6.3), but when it contains bytes with 0 values it gets truncated.

For example I have the Service Data in my bluetooth device written as 0xFF2233000065: the problem is that serviceData() method of QBluetoothDeviceInfo returns a QByteArray that has only the first three bytes FF, 22, 33 and stops when it founds 00. Any of you has encountered such a similar issue?

Thank you in advance

edit: Here I paste excerpts from my code, trying to clarify my issue:

    //...

discoveryAgent = new QBluetoothDeviceDiscoveryAgent();

connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,
        this, &BluetoothController::addDevice);
        

//...

void BluetoothController::addDevice(const QBluetoothDeviceInfo &device)
{
    //...
    QBluetoothUuid sId = device.serviceIds().front();
    
    //Here is the problem: instead of having sData composed by 6 bytes
    //as expected in my test (i.e 0xFF 0x22 0x33 0x00 0x00 0x65),
    //sData has size = 3 and has only 0xFF 0x22 0x33
    QByteArray sData = device.serviceData(sId);
    
    //...
}
srappose
  • 1
  • 1
  • Please [edit] your question and add the relevant source code or, even better, a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Michael Kotzjan May 23 '22 at 10:43
  • As a guess I'd say you're processing the returned `QByteArray` in a way that interprets it as a null terminated string. Hence everything after the first null is ignored. – G.M. May 23 '22 at 10:56
  • @G.M. as you can see in the part of code that I have added few minutes ago, I am not processing anything because is the serviceData method itself that returns the "truncated" array – srappose May 23 '22 at 12:08
  • Re. `"sData has size = 3 and has only 0xFF 0x22 0x33"`: how do you know that? How are you viewing the data -- in a debugger? If so, a debugger will probably have no knowledge of the `QByteArray` internals and may well treat any `char *` it finds as representing a null terminated string. – G.M. May 23 '22 at 12:19
  • @G.M. both from the debugger and by printing sData.size() I get 3. – srappose May 23 '22 at 12:34
  • @srappose could you please try the other [serviceData()](https://doc.qt.io/qt-6/qbluetoothdeviceinfo.html#serviceData-1) method? It returns a QMultiHash and, according to the docs, returns the complete set of all service data from advertisement packets – Michael Kotzjan May 23 '22 at 12:38
  • From the [documentation](https://doc.qt.io/qt-6/qbluetoothdeviceinfo.html#serviceData): `Note: The remote device may provide multiple data entries per serviceId. This function only returns the first entry. If all entries are needed use serviceData() which returns a multi hash."`. Have you looked at all of the data using the [other overload](https://doc.qt.io/qt-6/qbluetoothdeviceinfo.html#serviceData-1)? – G.M. May 23 '22 at 12:40
  • @MichaelKotzjan and G.M. I have also tried the other serviceData() with QMultiHash but I get the same wrong result: //... QMultiHash sDataMultiHash = device.serviceData(); QBluetoothUuid bUuid = sDataMultiHash.keys().front(); //ba returns the same truncated data of sData, i.e.: 0xFF 0x22 0x33 QByteArray ba = sDataMultiHash[bUuid]; //.... (NOTE: sorry but I do not know how to post the code in the comments) – srappose May 23 '22 at 12:55
  • Please use the [edit](https://stackoverflow.com/posts/72347179/edit) facility to update your post rather than putting code in comments. – G.M. May 23 '22 at 18:19
  • Please read the [`QMultiHash` documentation](https://doc.qt.io/qt-6/qmultihash.html) as well as the comments above: `The remote device may provide` **`multiple`** `data entries per serviceId`. You need to check them *all* using something like [`QMultiHash::equalRange`](https://doc.qt.io/qt-6/qmultihash.html#equal_range). – G.M. May 23 '22 at 18:23
  • Unfortunately also trying with equalRange I have only one iteration, giving me the same result of my previous comment (truncated data). – srappose May 24 '22 at 06:46

0 Answers0