OpenBluetoothToolbox / SimpleBLE

SimpleBLE - the all-in-one Bluetooth library for MacOS, iOS, Windows, Linux and Android.
https://www.simpleble.org
Other
658 stars 110 forks source link

SimpleBLE::Safe::Adapter::bluetooth_enabled() Calling it twice will cause a crash #329

Open gihugui opened 1 month ago

gihugui commented 1 month ago

This function will collapse when the second adjustment is applied to the current position.

auto bluetooth_enabled = SimpleBLE::Safe::Adapter::bluetooth_enabled(); if (!bluetooth_enabled.has_value()) { outMsg = "Bluetooth enabled status err"; }

STACK_TEXT:
0000008078affab0 00007ff619e43039 : 0000000000000000 000001d39c1c2830 0000000000000000 0000000000000000 : loveSockets!winrt::impl::factory_cache_entry<winrt::Windows::Devices::Radios::Radio,winrt::Windows::Devices::Radios::IRadioStatics>::call< & __ptr64>+0x52 0000008078affb80 00007ff619de0c68 : 0000008078affc38 0000000000000018 ffffffffffffffff 00007ff619de03c0 : loveSockets!SimpleBLE::AdapterBase::bluetooth_enabled+0x29 0000008078affbd0 00007ff619e05f5e : 0000000000000000 000001d39c1c2830 0000000000000000 0000000000000000 : loveSockets!my_server::handle_open'::2'::::operator()+0x358 0000008078affd50 00007ffb8abc1bb2 : 000001d39c1c53d0 0000000000000000 0000000000000000 0000000000000000 : loveSockets!std::thread::_Invoke<std::tuple<my_server::handle_open'::2':: >,0>+0xe 0000008078affd80 00007ffb8b237374 : 0000000000000000 0000000000000000 0000000000000000 0000000000000000 : ucrtbase!thread_start<unsigned int (__cdecl)(void ),1>+0x42 0000008078affdb0 00007ffb8d23cc91 : 0000000000000000 0000000000000000 0000000000000000 0000000000000000 : KERNEL32!BaseThreadInitThunk+0x14 0000008078affde0 0000000000000000 : 0000000000000000 0000000000000000 0000000000000000 0000000000000000 : ntdll!RtlUserThreadStart+0x21

gihugui commented 1 month ago

Finally, the function of windows has been solved.

include <winrt/Windows.Foundation.h>

include <winrt/Windows.Devices.Bluetooth.Advertisement.h>

include <winrt/Windows.Devices.Bluetooth.h>

include <winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h>

include <winrt/Windows.Foundation.Collections.h>

include <winrt/Windows.Storage.Streams.h>

include <winrt/Windows.Devices.Radios.h>

include

include

include

include

include

include

include

include

include

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

bool BLEIsLowEnergySupported() {

try
{
    auto getadapter_op = Windows::Devices::Bluetooth::BluetoothAdapter::GetDefaultAsync();
    auto adapter = getadapter_op.get();
    if (adapter == nullptr) {
        std::cerr << "No Bluetooth adapter found." << std::endl;
        return false;
    }
    auto supported = adapter.IsLowEnergySupported();
    if (supported == false) {
        return false;
    }

    auto async = adapter.GetRadioAsync();
    auto radio = async.get();
    auto t = radio.State();                       // 获取电脑蓝牙状态 0未知,1打开,2关闭,3硬件关闭或禁用
    if (t != winrt::Windows::Devices::Radios::RadioState::On) {
        return false;
    }
    return  true;
}
catch (const winrt::hresult_error& e) {
    std::wcerr << L"Exception: " << e.message().c_str() << std::endl;
    return false;
}
catch (const std::exception& e) {
    std::cerr << "Standard exception: " << e.what() << std::endl;
    return false;
}
catch (...) {
    std::cerr << "Unknown exception occurred." << std::endl;
    return false;
}

}

kdewald commented 1 month ago

Hey @gihugui, thanks for the report.

I'll leave it open so that I'll remember to go over it soon.