Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gihugui opened this issue Jul 12, 2024 · 2 comments

Comments

@gihugui
Copy link

gihugui commented Jul 12, 2024

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_entrywinrt::Windows::Devices::Radios::Radio,winrt::Windows::Devices::Radios::IRadioStatics::call<<lambda_d6ec5b0ce89a03db82adf8d9e7c2c76f> & __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'::<lambda_1>::operator()+0x358
0000008078affd50 00007ffb8abc1bb2 : 000001d39c1c53d0 0000000000000000 0000000000000000 0000000000000000 : loveSockets!std::thread::_Invoke<std::tuple<my_server::handle_open'::2'::<lambda_1> >,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
Copy link
Author

gihugui commented Jul 15, 2024

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 <unordered_set>
#include
#include
#include
#include
#include <io.h>
#include <fcntl.h>

#include <windows.h>

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;
}

}

@gihugui gihugui closed this as completed Jul 15, 2024
@kdewald kdewald reopened this Jul 16, 2024
@kdewald
Copy link
Member

kdewald commented Jul 16, 2024

Hey @gihugui, thanks for the report.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants