我目前正在使用 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);
}
}
当我经常这样做时,这种停止和重新启动会导致问题,有时可能会发生这种情况。
有更好的方法吗?