UUID和Name不在同一条BLE广播报文里

概要

通过在命令行窗口打印部分报文信息,发现:设备向外发送BLE蓝牙低功耗广播,设备的UUID和Name不在同一条广播报文里

UUID是通用唯一标识符

一、设备

1、发送报文的设备

能够发送BLE蓝牙低功耗广播的设备。本篇使用的是周围环境中的未知设备。

2、接收报文的设备

本篇使用一台Windows11电脑

二、代码

cpp 复制代码
#include <iostream>
#include <windows.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Devices.Bluetooth.Advertisement.h>
#include <winrt/Windows.Storage.Streams.h>
#include <atomic>
#include <string>
#include <sstream>
#include <vector>
#include <mutex>
#include <iomanip>
#include <chrono>

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Devices::Bluetooth::Advertisement;
using namespace Windows::Storage::Streams;

BluetoothLEAdvertisementWatcher g_watcher{nullptr};

std::string BluetoothAddressToString(uint64_t address){
    std::stringstream ss;
    ss << std::setw(2) << std::setfill('0') << std::hex
        << ((address >> 40) & 0xFF) << ":"
        << ((address >> 32) & 0xFF) << ":"
        << ((address >> 24) & 0xFF) << ":"
        << ((address >> 16) & 0xFF) << ":"
        << ((address >> 8)  & 0xFF) << ":"
        << (address         & 0xFF);
    return ss.str();
}

std::string WStringToUtf8(const std::wstring& wstr)
{
    if(wstr.empty()){
        return "";
    }
    int size=WideCharToMultiByte(CP_UTF8,0,wstr.c_str(),-1,nullptr,0,nullptr,nullptr);
    std::string str(size-1,0);
    WideCharToMultiByte(CP_UTF8,0,wstr.c_str(),-1,&str[0],size,nullptr,nullptr);
    return str;
}

void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
    std::string sout="";
    auto now = std::chrono::system_clock::now();
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count()%10000;
    std::string mac = BluetoothAddressToString(args.BluetoothAddress());
    sout+="[";
    sout+=std::to_string(ms);
    sout+= "ms]";
    sout+= mac;
    sout+= " ";
    std::wstring name = args.Advertisement().LocalName().c_str();
    if(!name.empty()){
        sout+= WStringToUtf8(name)+" ";
    }else{
        sout+="[Empty name]";
    }
    sout+="UUIDS: ";
    for(const auto& uuid : args.Advertisement().ServiceUuids()){
        sout+= WStringToUtf8(winrt::to_hstring(uuid).c_str());
    }
    std::cout<<sout<<std::endl;
    std::cout<<std::flush;
}
int main()
{
    winrt::init_apartment(winrt::apartment_type::multi_threaded);
    std::cout<<"Starting BLE Scanner......"<<std::endl;
    g_watcher = BluetoothLEAdvertisementWatcher();
    g_watcher.ScanningMode(BluetoothLEScanningMode::Active);
    g_watcher.Received(OnAdvertisementReceived);
    g_watcher.Start();
    std::cout<<"Scanning.....Press Enter to stop."<<std::endl;
    std::cin.get();
    g_watcher.Stop();
    std::cout<<"Stopped."<<std::endl;
    return 0;
}

三、编译链接

1、动态编译

cl /EHsc /MD /std:c++17 /Fe:Ble.exe main.cpp windowsapp.lib

查看文件大小和依赖

dir

dumpbin /dependents Ble.exe

2、静态编译

cl /EHsc /std:c++17 /MT /Fe:BleStatic.exe main.cpp windowsapp.lib

查看文件大小和依赖

dir

dumpbin /dependents BleStatic.exe

3、关于静态链接的说明

当使用/MT进行静态编译时,并不是生成了一个完全独立、不依赖任何系统DLL的程序

C++标准库(MSVCP140.dll)和C++运行时支持(VCRUNTIME140.dll)被静态打包进了.exe

Windows API依然是动态链接的。windowsapp.lib本质上是一个导入库(Import Library),里面是函数名,符号什么的。

也就是说,即使加了/MT,程序依然只能在Windows上运行,且依赖系统组件

四、在开发环境上演示

在开发环境Windows11上运行程序

1、动态链接的程序

可以明显看到,同一个设备(同一mac地址)发送过来的广播报文,总是先出现有uuid的报文,后出现有name的报文。uuid和name并不在同一条报文里。

2、静态链接的程序

可以看到,静态链接的程序运行效果和动态链接的程序运行效果是一样的

五、在非开发环境上演示

将程序复制到另一台没有开发环境的Windows10电脑上运行

1、动态链接的程序

可以看到,程序由于找不到MSVCP140.dll和VCRUNTIME140.dlll而无法运行

2、静态链接的程序

程序能够运行,但扫描不到设备。

按Enter键程序不能正常退出。

按两次Enter键之后,窗口关闭。

相关推荐
落羽的落羽16 分钟前
【C++】现代容器: tuple、optional、variant、any、string_view
linux·开发语言·数据结构·c++·人工智能·计算机网络·算法
闻缺陷则喜何志丹1 小时前
【Assimp】三维模型读取库
c++·算法·assimp·三维模型库
小飞在努力2 小时前
【实现负载均匀在线判题④】接入 cpp-httplib 实现编译服务 HTTP 网络化
c语言·开发语言·c++·笔记·http
-梅4 小时前
linux(番外)线程,锁,条件变量封装;日志与线程池
linux·c语言·前端·c++
S_WEAN4 小时前
【C++】STL - Priority_queue的使用及模拟实现与仿函数
开发语言·c++
Vex2une4 小时前
把 Windows 装进 U 盘随身携带:Windows To Go 现状与 WinToUSB 制作完全指南
windows·vhdx·windows to go·wintousb·便携windows·系统带进u盘·随身系统
俊俊几年的5 小时前
嵌入式 Linux 应用开发:CMake 详解与交叉编译实践
linux·c++·嵌入式·cmake·linux应用开发
wefg115 小时前
【项目】C++ 基于 Vibe Coding 的 OJ 平台
开发语言·c++
郝学胜-神的一滴21 小时前
Effective STL 条款 07:指针容器销毁前先删除
开发语言·c++·vscode·leetcode·rpc·visual studio
Lucas. Wáng1 天前
Qt学习之旅
c++·qt