[UWP to WPF][C#] How to know whether bluetooth is on or off?

Joon 101 Reputation points
2020-09-22T08:49:36.3+00:00

I want to know whether bluetooth is on or off in Windows 10.
I found microsoft.sdk.contract package in nuget to use Windows 10 API including bluetooth-related methods.

I tried to get 'Radio' instance using 'GetRadioAsync()' but it always throws Class Not Found Exception with Message: HRESULT 0x80040154 (REGDB_E_CLASSNOTREG)

But the same source code works well in UWP app.
What am I missing?

void func () {
    BluetoothAdapter bluetoothAdapter = await BluetoothAdapter.GetDefaultAsync();

    try
    {
        Radio awaitedRadio = await bluetoothAdapter.GetRadioAsync();
        if (awaitedRadio != null)
        {
            addText("radio state:" + awaitedRadio.State);
        }
        else
        {
            addText("awaitedRadio is null");
        }
    }
    catch (Exception exception)
    {
        addText(exception.Message + "\n\n");
    }
}
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,633 questions
0 comments No comments
{count} votes

Accepted answer
  1. Joon 101 Reputation points
    2020-09-22T10:01:34.403+00:00

    I found the answer.

    Refer to Radio.GetRadiosAsync Method

    When this method is called from a desktop application (Win32), it will retrieve radio instances only when the application is natively compiled for the target architecture (in other words, when the application matches the computer architecture; so an x86 application running on an x64 architecture computer won't see any radio instances).

    So, we should build with target architecutre.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful