none
How to Enable ou Disable Bluetooth in c# UWP RRS feed

  • Question

  • Hello,

    I try to enable the Bluetooth with an API automaticaly and also disable the bluetooth in C# with UWP ( Windows 10 tablet and desktop ).

    I try to read the status with:

                var radios = await Radio.GetRadiosAsync();
                var bluetoothRadio = radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth);
                return bluetoothRadio != null && bluetoothRadio.State == RadioState.On;

    i try to change the status in enable, but the changement is refused:

                var radios = await Radio.GetRadiosAsync();
                var bluetoothRadio = radios.FirstOrDefault(radio => (radio.Kind == RadioKind.Bluetooth) );
                var Status = await bluetoothRadio.SetStateAsync(RadioState.On);
                return (Status.Equals(1));

    It is possible to allow the change status on Bluetooth?

    Friday, December 16, 2016 9:29 AM

All replies

  • Hi seblefur.

    for solving the problem first add this capability in your Package.appxmanifest :

        <DeviceCapability Name="bluetooth" />
        <DeviceCapability Name="radios" />

    then use this method :

    private async Task ChangeBluetoothStateAsync(bool bluetoothState)
            {
                try
                {
                    var access = await Radio.RequestAccessAsync();
                    if (access != RadioAccessStatus.Allowed)
                    {
                        return;
                    }
                    BluetoothAdapter adapter = await BluetoothAdapter.GetDefaultAsync();
                    if (null != adapter)
                    {
                        var btRadio = await adapter.GetRadioAsync();
                        if (bluetoothState)
                        {
                            await btRadio.SetStateAsync(RadioState.On);
                        }
                        else
                        {
                            await btRadio.SetStateAsync(RadioState.Off);
                        }
                    }
                    else
                    {
                    }
    
                }
                catch
                {
                }
            }


    Wednesday, December 13, 2017 10:51 AM
  • Calling GetRadioAsync() from a classic desktop application (C# + WinForms) returns HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)

    Tuesday, October 9, 2018 8:48 AM