因为ST的BLE工具无论是移动端还是WEB端都不可修改,即使源码也看不懂,毕竟不擅长的领域。但是Windows作为最常用的开发平台,大部分的测试调试工具都能够实现,所以就初步尝试用Qt的BLE模块来做个小工具。 基于开源版的Qt,默认安装了Mingw编译套件。毕竟也是不少人研究过的东西,借鉴一下也是很快能运行起来的。首先在工程文件中引入BLE
这个时候可以先编译一下默认的空工程,不出错的话说明安装没有问题,版本也可以使用。BLE模块是有的。
然后计划一下布局,设计师界面拖拖控件。
之后加入功能代码。分为代理部分,控制部分,服务处理部分。以下摘抄部分。
- void Agent::startScanDevice(uint32_t timeOut)
- {
- if(m_agent)
- {
- m_agent->setLowEnergyDiscoveryTimeout(timeOut);
- m_agent->start();
- if(m_agent->isActive())
- {
- SendMessage("scanning...");
- }
- }
- }
- QLowEnergyService * Controller::CreateService(QBluetoothUuid serviceUUID)
- {
- if(m_controller)
- {
- return m_controller->createServiceObject(serviceUUID);
- }
- else
- {
- return NULL;
- }
- }
- void Service::OpenNotify(QLowEnergyCharacteristic ch, bool flag)
- {
- if(m_service)
- {
- if(ch.isValid())
- {
- if(ch.properties() & QLowEnergyCharacteristic::Notify)
- {
- QLowEnergyDescriptor d = ch.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
- if(d.isValid())
- {
- if(true == flag)
- {
- m_service->writeDescriptor(d, QByteArray::fromHex("0100"));
- }
- else
- {
- m_service->writeDescriptor(d, QByteArray::fromHex("0000"));
- }
- }
- }
- }
- }
- }
- void Service::ReadCharacteristic(QLowEnergyCharacteristic ch)
- {
- if(m_service)
- {
- if(ch.isValid())
- {
- if(ch.properties() & QLowEnergyCharacteristic::Read)
- {
- m_service->readCharacteristic(ch);
- }
- }
- }
- }
- void Service::WriteCharacteristic(QLowEnergyCharacteristic ch, QByteArray arr)
- {
- if(m_service)
- {
- if(ch.isValid())
- {
- if(ch.properties() & QLowEnergyCharacteristic::Write)
- {
- m_service->writeCharacteristic(ch, arr, QLowEnergyService::WriteWithResponse);
- }
- else if(ch.properties() & QLowEnergyCharacteristic::WriteNoResponse)
- {
- m_service->writeCharacteristic(ch, arr, QLowEnergyService::WriteWithoutResponse);
- }
- else if(ch.properties() & QLowEnergyCharacteristic::WriteSigned)
- {
- m_service->writeCharacteristic(ch, arr, QLowEnergyService::WriteSigned);
- }
- }
- }
- }
主要部分完成后逐步改错,然后运行搜索看看有没有结果
居然把蓝牙耳机也搜到了,看来Qt实现还是比较简单的,Nucleo-STM32WBA52板子运行程序,再试试搜索,,
居然没搜到板子的蓝牙id,换了个工程还是没有,看来浮于表面是没法解决问题的。后续搞清楚这一点。还有个值得注意的地方,就是编译器的选择,有网友提到过对于BLE这个功能,mingw可能会比VC编译器遇到的问题多。后续要研究清楚。
|