[STM32WBA] 【STM32WBA52CG测评】Qt BLE初探

[复制链接]
1365|1
 楼主| BinWin 发表于 2023-8-7 23:17 | 显示全部楼层 |阅读模式
      因为ST的BLE工具无论是移动端还是WEB端都不可修改,即使源码也看不懂,毕竟不擅长的领域。但是Windows作为最常用的开发平台,大部分的测试调试工具都能够实现,所以就初步尝试用Qt的BLE模块来做个小工具。      基于开源版的Qt,默认安装了Mingw编译套件。毕竟也是不少人研究过的东西,借鉴一下也是很快能运行起来的。首先在工程文件中引入BLE
       1.png
     这个时候可以先编译一下默认的空工程,不出错的话说明安装没有问题,版本也可以使用。BLE模块是有的。
     然后计划一下布局,设计师界面拖拖控件。
      2.png
     之后加入功能代码。分为代理部分,控制部分,服务处理部分。以下摘抄部分。

  1. void Agent::startScanDevice(uint32_t timeOut)
  2. {
  3.     if(m_agent)
  4.     {
  5.         m_agent->setLowEnergyDiscoveryTimeout(timeOut);

  6.         m_agent->start();

  7.         if(m_agent->isActive())
  8.         {
  9.             SendMessage("scanning...");
  10.         }
  11.     }
  12. }
  1. QLowEnergyService * Controller::CreateService(QBluetoothUuid serviceUUID)
  2. {
  3.     if(m_controller)
  4.     {
  5.         return m_controller->createServiceObject(serviceUUID);
  6.     }
  7.     else
  8.     {
  9.         return NULL;
  10.     }
  11. }
  1. void Service::OpenNotify(QLowEnergyCharacteristic ch, bool flag)
  2. {
  3.     if(m_service)
  4.     {
  5.         if(ch.isValid())
  6.         {
  7.             if(ch.properties() & QLowEnergyCharacteristic::Notify)
  8.             {
  9.                 QLowEnergyDescriptor d = ch.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
  10.                 if(d.isValid())
  11.                 {
  12.                     if(true == flag)
  13.                     {
  14.                         m_service->writeDescriptor(d, QByteArray::fromHex("0100"));
  15.                     }
  16.                     else
  17.                     {
  18.                         m_service->writeDescriptor(d, QByteArray::fromHex("0000"));
  19.                     }
  20.                 }
  21.             }
  22.         }
  23.     }
  24. }

  25. void Service::ReadCharacteristic(QLowEnergyCharacteristic ch)
  26. {
  27.     if(m_service)
  28.     {
  29.         if(ch.isValid())
  30.         {
  31.             if(ch.properties() & QLowEnergyCharacteristic::Read)
  32.             {
  33.                 m_service->readCharacteristic(ch);
  34.             }
  35.         }
  36.     }
  37. }

  38. void Service::WriteCharacteristic(QLowEnergyCharacteristic ch, QByteArray arr)
  39. {
  40.     if(m_service)
  41.     {
  42.         if(ch.isValid())
  43.         {
  44.             if(ch.properties() & QLowEnergyCharacteristic::Write)
  45.             {
  46.                 m_service->writeCharacteristic(ch, arr, QLowEnergyService::WriteWithResponse);
  47.             }
  48.             else if(ch.properties() & QLowEnergyCharacteristic::WriteNoResponse)
  49.             {
  50.                 m_service->writeCharacteristic(ch, arr, QLowEnergyService::WriteWithoutResponse);
  51.             }
  52.             else if(ch.properties() & QLowEnergyCharacteristic::WriteSigned)
  53.             {
  54.                 m_service->writeCharacteristic(ch, arr, QLowEnergyService::WriteSigned);
  55.             }
  56.         }

  57.     }
  58. }
    主要部分完成后逐步改错,然后运行搜索看看有没有结果      3.png
     居然把蓝牙耳机也搜到了,看来Qt实现还是比较简单的,Nucleo-STM32WBA52板子运行程序,再试试搜索,,
     居然没搜到板子的蓝牙id,换了个工程还是没有,看来浮于表面是没法解决问题的。后续搞清楚这一点。还有个值得注意的地方,就是编译器的选择,有网友提到过对于BLE这个功能,mingw可能会比VC编译器遇到的问题多。后续要研究清楚。
     



yangxiaor520 发表于 2023-8-8 08:34 来自手机 | 显示全部楼层
QT跨平台,兼容性更好。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

20

主题

65

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部