0

I have an UWP project which communicates with a Bluetooth LE device. The communication is working fine but I would like to add one step before establishing the communication which is checking if the Bluetooth is enabled from Windows (10) side:

Bluetooth switch

At the moment, I'm using the class Windows::Devices::Bluetooth::BluetoothAdapter and calling the method getDefaultAsync() to get the actual interface. When I turn if off using the Windows switch, the interface is still there. This is what happens if I turn off the switch:

Switch ON ---------> Switch off

Here's a snippet of the code:

HANDLE done = CreateEvent(NULL, FALSE, FALSE, NULL);
auto getadapter_op = Windows::Devices::Bluetooth::BluetoothAdapter::GetDefaultAsync();
auto getadapter_optask = create_task(getadapter_op);
getadapter_optask.then([done](Windows::Devices::Bluetooth::BluetoothAdapter^ adapter) {
    if (adapter != nullptr)
    {
        defaultBtInterface = adapter;
    }
    SetEvent(done);
    });

if (WaitForSingleObject(done, 10000) != WAIT_OBJECT_0)
{
}
else
{
}
CloseHandle(done);

if (defaultBtInterface == nullptr) // bluetooth interface is not available
{
    printf("Warning: No bluetooth adpter was found\n");
    return;
}

The problem is that even if I turn off the switch, the interface is still there. I just get a nullptr if I disable it directly on the Device Manager, like this:

disabling

I also tried to use the DeviceInformation.IsEnabled Property but same behavior. Is there some way to check if the Bluetooth Switch is enabled on Windows? Thanks in advance.

Persike
  • 63
  • 5
  • 1
    You have to use SetupAPI or Configuration Manager API to be able to disable/enable drivers. Probably is also can be done with WMI. – Mike Petrichenko Jan 27 '20 at 18:58
  • 1
    turn off the switch is not equal to disable the device, This is the difference between hardware and software。If you want to disable Bluetooth devices, you should consider disabling that port. – Suarez Zhou Jan 28 '20 at 09:25
  • 1
    The following link is for your reference.[https://social.msdn.microsoft.com/Forums/vstudio/en-US/13d152ac-18ab-409d-977a-a4e12cc4e1c2/turn-device-onoff?forum=vcgeneral](https://social.msdn.microsoft.com/Forums/vstudio/en-US/13d152ac-18ab-409d-977a-a4e12cc4e1c2/turn-device-onoff?forum=vcgeneral) – Suarez Zhou Jan 28 '20 at 09:43
  • Hi @SuarezZhou-MSFT, thanks for the answer, although it's not exactly what I need. I would like my code to check the status of that "switch" before starting the Bluetooth communication. For example, if the switch if turned "off", an error message pops up stating that the Bluetooth is not enabled on the Windows panel. – Persike Jan 28 '20 at 14:40
  • 1
    https://stackoverflow.com/questions/33013275/how-to-check-if-bluetooth-is-enabled-on-a-device – Alex Vallo Jan 28 '20 at 14:55
  • Hi @AlexV. your suggestion was exactly what I needed. The Radio class has the properties and methods that fixed my problem. Thank you very much. – Persike Jan 29 '20 at 13:10

1 Answers1

1

So that there is an answer to this question. Copying the link provided in the comment section. This is a duplicated question. Here is a link to the answer:

How to check if bluetooth is enabled on a device

Alex Vallo
  • 142
  • 2
  • 7