一、Active和Passive微软官方文档BluetoothLEScanningMode Enum (Windows.Devices.Bluetooth.Advertisement) - Windows apps | Microsoft Learn翻译二、Active1、代码#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 sizeWideCharToMultiByte(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_caststd::chrono::milliseconds(now.time_since_epoch()).count()%10000; std::string mac BluetoothAddressToString(args.BluetoothAddress()); sout[; soutstd::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]; } soutUUIDS: ; for(const auto uuid : args.Advertisement().ServiceUuids()){ sout WStringToUtf8(winrt::to_hstring(uuid).c_str()); } std::coutsoutstd::endl; std::coutstd::flush; } int main() { winrt::init_apartment(winrt::apartment_type::multi_threaded); std::coutStarting BLE Scanner......std::endl; g_watcher BluetoothLEAdvertisementWatcher(); g_watcher.ScanningMode(BluetoothLEScanningMode::Active); g_watcher.Received(OnAdvertisementReceived); g_watcher.Start(); std::coutScanning.....Press Enter to stop.std::endl; std::cin.get(); g_watcher.Stop(); std::coutStopped.std::endl; return 0; }2、编译连接打开Developer Command Prompt for VS 2022cl /EHsc /MD /std:c17 /Fe:BleActive.exe main.cpp windowsapp.lib3、运行程序打开蓝牙运行程序三、Passive1、代码#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 sizeWideCharToMultiByte(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_caststd::chrono::milliseconds(now.time_since_epoch()).count()%10000; std::string mac BluetoothAddressToString(args.BluetoothAddress()); sout[; soutstd::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]; } soutUUIDS: ; for(const auto uuid : args.Advertisement().ServiceUuids()){ sout WStringToUtf8(winrt::to_hstring(uuid).c_str()); } std::coutsoutstd::endl; std::coutstd::flush; } int main() { winrt::init_apartment(winrt::apartment_type::multi_threaded); std::coutStarting BLE Scanner......std::endl; g_watcher BluetoothLEAdvertisementWatcher(); g_watcher.ScanningMode(BluetoothLEScanningMode::Passive); g_watcher.Received(OnAdvertisementReceived); g_watcher.Start(); std::coutScanning.....Press Enter to stop.std::endl; std::cin.get(); g_watcher.Stop(); std::coutStopped.std::endl; return 0; }2、编译连接cl /EHsc /MD /std:c17 /Fe:BlePassive.exe main.cpp windowsapp.lib3、运行程序四、对比运行结果可以明显看到Active模式下能收到两种不同的广播报文一条含UUID一条含Name而Passive模式下只能收到一种广播报文即只含有UUID的广播报文。