Cannot change Bluetooth Low Energy advertising service



  • Hi, I have the same problem like in this unsolved question: https://forum.qt.io/topic/95346/cannot-change-qtbluetooth-advertising-service.
    I modified the QT Heart Rate Server example to advertise a custom uuid instead of Heart Rate service but my iPhone app is still discovering Heart Rate service. I have also tried advertising Battery Service but the problem remains, iPhone still discovers Heart Rate service.

    I changed the name which is being advertised to make sure my iPhone is connecting to the correct peripheral. I also print the uuid of the service which is being advertised and it prints

    {0000180f-0000-1000-8000-00805f9b34fb}
    

    which is the uuid for the Battery Service. However that is not what the iPhone discovers.

    Here is the modified code:

    /***************************************************************************
    **
    ** Copyright (C) 2017 The Qt Company Ltd.
    ** Contact: https://www.qt.io/licensing/
    **
    ** This file is part of the examples of the QtBluetooth module of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:BSD$
    ** Commercial License Usage
    ** Licensees holding valid commercial Qt licenses may use this file in
    ** accordance with the commercial license agreement provided with the
    ** Software or, alternatively, in accordance with the terms contained in
    ** a written agreement between you and The Qt Company. For licensing terms
    ** and conditions see https://www.qt.io/terms-conditions. For further
    ** information use the contact form at https://www.qt.io/contact-us.
    **
    ** BSD License Usage
    ** Alternatively, you may use this file under the terms of the BSD license
    ** as follows:
    **
    ** "Redistribution and use in source and binary forms, with or without
    ** modification, are permitted provided that the following conditions are
    ** met:
    **   * Redistributions of source code must retain the above copyright
    **     notice, this list of conditions and the following disclaimer.
    **   * Redistributions in binary form must reproduce the above copyright
    **     notice, this list of conditions and the following disclaimer in
    **     the documentation and/or other materials provided with the
    **     distribution.
    **   * Neither the name of The Qt Company Ltd nor the names of its
    **     contributors may be used to endorse or promote products derived
    **     from this software without specific prior written permission.
    **
    **
    ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
    **
    ** $QT_END_LICENSE$
    **
    ****************************************************************************/
    
    #include <QtBluetooth/qlowenergyadvertisingdata.h>
    #include <QtBluetooth/qlowenergyadvertisingparameters.h>
    #include <QtBluetooth/qlowenergycharacteristic.h>
    #include <QtBluetooth/qlowenergycharacteristicdata.h>
    #include <QtBluetooth/qlowenergydescriptordata.h>
    #include <QtBluetooth/qlowenergycontroller.h>
    #include <QtBluetooth/qlowenergyservice.h>
    #include <QtBluetooth/qlowenergyservicedata.h>
    #include <QtCore/qbytearray.h>
    #include <QtCore/qcoreapplication.h>
    #include <QtCore/qlist.h>
    #include <QtCore/qloggingcategory.h>
    #include <QtCore/qscopedpointer.h>
    #include <QtCore/qtimer.h>
    
    class BLEListener: public QObject
    {
        Q_OBJECT
    
    public slots:
        void characteristicRead(){
            QTextStream out(stdout);
            out << "CHARACTERISTIC READ!" << endl;
        }
    };
    
    int main(int argc, char *argv[])
    {
        //QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true"));
        QCoreApplication app(argc, argv);
    
        //! [Advertising Data]
        QLowEnergyAdvertisingData advertisingData;
        advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
        advertisingData.setIncludePowerLevel(true);
        advertisingData.setLocalName("eae128-eae128-ea");
        advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::BatteryService);
    
        QTextStream out(stdout);
        QList<QBluetoothUuid> discoverableServices = advertisingData.services();
        out << "Services:" << endl;
        foreach (QBluetoothUuid uuid, discoverableServices) {
            out << uuid.toString() << endl;
        }
        out << endl;
    
        //! [Advertising Data]
    
        //! [Service Data]
        QLowEnergyCharacteristicData charData;
        charData.setUuid(QBluetoothUuid::BatteryLevel);
        charData.setValue(QByteArray(2, 0));
        charData.setProperties(QLowEnergyCharacteristic::Notify);
        const QLowEnergyDescriptorData clientConfig(QBluetoothUuid::ClientCharacteristicConfiguration,
                                                    QByteArray(2, 0));
        charData.addDescriptor(clientConfig);
    
        QLowEnergyServiceData serviceData;
        serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
        serviceData.setUuid(QBluetoothUuid::BatteryService);
        serviceData.addCharacteristic(charData);
        //! [Service Data]
    
        //! [Start Advertising]
        const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral());
        QScopedPointer<QLowEnergyService> service(leController->addService(serviceData));
        leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,
                                       advertisingData);
        //! [Start Advertising]
    
        //! [Provide Heartbeat]
        QTimer heartbeatTimer;
        quint8 currentHeartRate = 60;
        enum ValueChange { ValueUp, ValueDown } valueChange = ValueUp;
        const auto heartbeatProvider = [&service, &currentHeartRate, &valueChange]() {
            QByteArray value;
            value.append(char(0)); // Flags that specify the format of the value.
            value.append(char(currentHeartRate)); // Actual value.
            QLowEnergyCharacteristic characteristic
                    = service->characteristic(QBluetoothUuid::BatteryLevel);
            Q_ASSERT(characteristic.isValid());
            service->writeCharacteristic(characteristic, value); // Potentially causes notification.
            if (currentHeartRate == 60)
                valueChange = ValueUp;
            else if (currentHeartRate == 100)
                valueChange = ValueDown;
            if (valueChange == ValueUp)
                ++currentHeartRate;
            else
                --currentHeartRate;
        };
        QObject::connect(&heartbeatTimer, &QTimer::timeout, heartbeatProvider);
        heartbeatTimer.start(1000);
        //! [Provide Heartbeat]
    
        auto reconnect = [&leController, advertisingData, &service, serviceData]()
        {
            service.reset(leController->addService(serviceData));
            if (!service.isNull())
                leController->startAdvertising(QLowEnergyAdvertisingParameters(),
                                               advertisingData, advertisingData);
        };
        //QObject::connect(leController.data(), &QLowEnergyController::disconnected, reconnect);
    
        return app.exec();
    }
    
    

    Any help is appreciated.



  • I solved the problem, iPhone was caching bluetooth data for the peripheral which is why it was always discovering the same service and characteristic. Turning bluetooth off and on again on iPhone fixed the problem. Hopefully this might help someone who runs into the same issue.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.