问题描述
这是我们已经发送了有关定制服务的定制特征的定期更新的设备.该设备中的服务和特性是通过XML文件定义的.当然,这是指蓝牙BLE协议.
Here is the thing a device that we have transmits regular updates of a custom characteristic of a custom service. The service and characteristic in this device is defined via a XML file. This, of course, is referring to the Bluetooth BLE protocol.
我要做的是创建一个简单的Qt Android应用程序,该应用程序连接到设备并监视更新.我已经找到了服务并将其信号连接到它.我使用以下代码完成了此操作:
What I'm trying to do is create a simple Qt Android App that connects to the device and monitors the update. I've gotten as far as discovering the service and connecting it signal to it. I've done that using this code:
void BLETest::on_stateChanged(QLowEnergyService::ServiceState state){
Q_UNUSED(state);
// Only printing data if all services are in correct state.
for (qint32 i = 0; i < monitoredServices.size(); i++){
if (monitoredServices.at(i)->state() != QLowEnergyService::ServiceDiscovered){
logger->out("There are still services that have not been discoverd",Logger::LC_ORANGE);
return;
}
}
QString msg = "PRINTING SERVICE DATA<br>";
for (qint32 i = 0; i < monitoredServices.size(); i++){
QLowEnergyService *monitoredService = monitoredServices.at(i);
QList<QLowEnergyCharacteristic> clist = monitoredService->characteristics();
msg = msg + "SERVICE: " + monitoredService->serviceName() + ". UUID: " + monitoredService->serviceUuid().toString() + "<br>";
// Checking if this is the service that need connection.
if (monitoredService->serviceUuid() == QBluetoothUuid(QString("0a464eef-af72-43fd-8a8b-1f26f6319dab"))){
QString res;
if (connect(monitoredService,SIGNAL(characteristicChanged(QLowEnergyCharacteristic,QByteArray)),this,SLOT(on_charastericChanged(QLowEnergyCharacteristic,QByteArray)))) res = "true";
else res = "false";
logger->out("CONNECTED TO TARGET SERVICE: " + res,Logger::LC_ORANGE);
}
for (int i = 0; i < clist.size(); i++){
QString name = clist.at(i).name();
if (name.isEmpty()) name = "UNDEFINED NAME";
QByteArray buffer = clist.at(i).value();
//QString value = QString(clist.at(i).value());
QString value = QByteArray(buffer.toHex()) + " (BS = " + QString::number(buffer.size()) + ")";
QString properties = QString::number(clist.at(i).properties());
msg = msg + "CHARACTERISTIC: " + clist.at(i).uuid().toString() + " - " + name + ": " + value + ". PROPERTIES: " + properties + "<br>";
}
if (clist.isEmpty()){
msg = msg + "No characteristics found<br>";
}
}
logger->out(msg);
}
以上功能等待所有服务被发现,然后为所有服务的所有特征打印UUID,名称和值.处理完我要监视的服务后,便完成了对changedCharacteristic信号的连接.
The above functions waits for all services to be discovered then prints the UUID, name and Value for all characteristics of all services. When the service I want to monitored is processed a connection is done to the changedCharacteristic signal.
执行此操作时,我要监视的服务特征的打印值就是该特征的原始值.但是,随着该值的更新,我不会收到通知(该信号永远不会触发),因此该值在我的应用程序中也不会更改.
When I do this the printed value of the characteristic of the service I want to monitor is the original value for that characteristic. However as that value updates I'm not notified (the signal never triggers) and so the value never changes in my app.
我需要编写一些代码来实际触发信号吗?
Do I need to write some code to actually trigger the signals?
PD:使用Blue Gecko演示应用程序,我可以看到值发生了变化.
PD: Using the Blue Gecko Demo App I can see the values changing.
我决定使用计时器来轮询特征值,并且它永远不会改变.这可能表明为什么也从不产生信号.
I decided to use a timer to Poll the value of the characteristic and it never changes. Which might indicate why the signal is never generated either.
推荐答案
您应将已更改特性的处理程序连接到服务:
You should connect a chracteristic changed handler to the service:
connect(service, SIGNAL(characteristicChanged(QLowEnergyCharacteristic, QByteArray)), this, SLOT(on_characteristicChanged(QLowEnergyCharacteristic, QByteArray)));
在插槽中,您可以浏览数据阵列.
In the slot you can explore the data array.
但是,仅在启用了此特性通知的情况下,服务才会发出信号(characteristicChanged()).仅当特征具有notify属性时,此方法才有效,在您的应用程序中应该如此.
However, the signal (characteristicChanged()) will only emitted by the service if the for this chracteristic notification is enabled. This works only, if the characteritic has a notify property, that should the case in your application.
这篇关于适用于Android的Qt BLE:特征更新不会触发featureChanged信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!