0

我目前正在使用 qt 进行项目。我想通过蓝牙广告来宣传异步计算的结果。

我正在像这样的 BluetoothAdvertisingClass 中设置广告商

void BLEServer::startAdvertising(QString string){
    advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
    advertisingData.setIncludePowerLevel(true);
    advertisingData.setLocalName("Server");
    advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::HeartRate);
    advertisingData.setManufacturerData(manufacturereID,buildDataPackage(string));

    QLowEnergyCharacteristicData charData;
    charData.setUuid(QBluetoothUuid::HeartRateMeasurement);
    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::HeartRate);
    serviceData.addCharacteristic(charData);

    leController = QSharedPointer<QLowEnergyController>(QLowEnergyController::createPeripheral());
    QScopedPointer<QLowEnergyService> service(leController->addService(serviceData));
    advertisingParameters  = QLowEnergyAdvertisingParameters();
    advertisingParameters.setMode(QLowEnergyAdvertisingParameters::AdvNonConnInd);

    leController->startAdvertising(advertisingParameters, advertisingData, advertisingData);

}

我在 BLESErver.h 文件中定义了变量 leControler、advertisngData 和 manufatcurer id

QSharedPointer<QLowEnergyController> leController;
QLowEnergyAdvertisingData advertisingData;
int manufacturereID = 1775;

并且将 dataPackage 构建为 ByteArray 的函数定义为 this

QByteArray BLEHServer::buildDataPackage(QString string){
    QByteArray stringArray = string.toLocal8Bit();
    return stringArray;
}

问题是我想相当频繁地更改广告价值,我不确定如何正确地做到这一点,或者这是否是广告的意图。

目前我刚刚开始一个新的广告商并停止旧的广告商,但我想这不是它的意图。它看起来像这样:

void BLEServer::changeAdvertisingData(QString string){
    try {

        //Stopping Advertising and creating a new Controller
        leController->stopAdvertising();
        leController = QSharedPointer<QLowEnergyController>(QLowEnergyController::createPeripheral());
        //Create new Advertising Data and swapping it with the old ones
        QLowEnergyAdvertisingData newAdvertisingData;
        newAdvertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
        newAdvertisingData.setIncludePowerLevel(true);
        newAdvertisingData.setLocalName("Anki");
        newAdvertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::HeartRate);
        newAdvertisingData.setManufacturerData(manufacturereID,buildDataPackage(string));
        advertisingData.swap(newAdvertisingData);

        //Start to advertise new Data
        leController->startAdvertising(advertisingParameters, advertisingData, advertisingData);
    } catch (QException e){
        throw(e);
    }
}

当我经常这样做时,这种停止和重新启动会导致问题,有时可能会发生这种情况。

有更好的方法吗?

4

1 回答 1

1

而不是启动/停止广告,您应该保存对 QBluetoothService 对象的引用(在您的代码中它是心率服务),​​并更新服务类的数据。当下一个广告包到期时,广告将自动获取新值。

或者,如果您正在编程发送方和接收方,您可以使用 setRawData() 函数为数据包设置任意 31 字节数据。

于 2018-12-21T14:46:53.643 回答