我正在QT中为外围设备编写固件。 该设备已连接到手机或笔记本电脑,并且通讯方式为 在蓝牙LowEnergy上建立。 我已经有将数据从外围设备(比如手表)发送到PC或电话的代码。现在,我想将数据从手机发送到手表。我想发送一些基本数据,例如日期,时间等,以便我可以配置手表。
body {
overflow-y: scroll;
scroll-behavior: smooth;
overflow-anchor: none;
}
我更改了此行,以便可以读取数据。是的,特征必须为WRITE。表示其他设备正在写入数据。
Here is the code for advertising/sending data from the watch to the phone
QCoreApplication app(argc, argv);
//! [Advertising Data]
QLowEnergyAdvertisingData advertisingData;
advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
advertisingData.setIncludePowerLevel(true);
advertisingData.setLocalName("HeartRateServer");
advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::HeartRate);
//! [Advertising Data]
//! [Service Data]
QLowEnergyCharacteristicData charData;
charData.setUuid(QBluetoothUuid::HeartRateMeasurement);
charData.setValue(QByteArray(2, 0));
charData.setProperties(QLowEnergyCharacteristic::Read);
const QLowEnergyDescriptorData clientConfig(QBluetoothUuid::ClientCharacteristicConfiguration,
QByteArray(2, 0));
charData.addDescriptor(clientConfig);
QLowEnergyServiceData serviceData;
serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
serviceData.setUuid(QBluetoothUuid::HeartRate);
serviceData.addCharacteristic(charData);
//! [Service Data]
//! [Start Advertising]
const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral());
const QScopedPointer<QLowEnergyService> service(leController->addService(serviceData));
leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,
advertisingData);
//! [Start Advertising]
qDebug()<<"Sending data...";
//! [Provide Heartbeat]
QTimer heartbeatTimer;
quint8 currentHeartRate = 60;
enum ValueChange { ValueUp, ValueDown } valueChange = ValueUp;
const auto heartbeatProvider = [&service, ¤tHeartRate, &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::HeartRateMeasurement);
value="hello world! it works !!!!!!!";
service->writeCharacteristic(characteristic, value); // Potentially causes notification.
qDebug()<<"Data sent: "<<value;
};
QObject::connect(&heartbeatTimer, &QTimer::timeout, heartbeatProvider);
heartbeatTimer.start(100);
//! [Provide Heartbeat]
auto reconnect = [&leController, advertisingData]() {
leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData, advertisingData);
};
QObject::connect(leController.data(), &QLowEnergyController::disconnected, reconnect);
我也将以下几行:
charData.setProperties(QLowEnergyCharacteristic::Write);
因此有一个适用于Android的应用程序,可让我将数据发送到设备。 但是我得到的唯一数据是:数据读取:“ \ x00 \ x00” 这是一个空字符串。数据已发送,但我不知道如何读取。有人可以帮忙吗?
谢谢!