Check if Bluetooth is enabled

0

I have the following code which works perfectly on a device with WP8.1 to determine if Bluetooth is enabled or not. When I run it on a Windows Mobile 10 device there is no exception. The app works as expected when Bluetooth is enabled.

private async Task<bool> CheckBluetoothEnabled()
{
    try
    {
        Windows.Networking.Proximity.PeerFinder.Start();
        var peers = await Windows.Networking.Proximity.PeerFinder.FindAllPeersAsync();
        return true;
    }
    catch (Exception ex)
    {
        if ((uint)ex.HResult == 0x8007048F)
        {
            return false;
        }
        return true;
    }
}

The project targets WP8.1. How can I get the same behavior on both OS?

c#
bluetooth
asked on Stack Overflow Mar 10, 2016 by stefana

1 Answer

2

you can also use a radio class for this.

To check if Bluetooth is enabled:

public static async Task<bool> GetBluetoothIsEnabledAsync()
{
    var radios = await Radio.GetRadiosAsync();
    var bluetoothRadio = radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth);
    return bluetoothRadio != null && bluetoothRadio.State == RadioState.On;
}
answered on Stack Overflow Mar 10, 2016 by vijay7790

User contributions licensed under CC BY-SA 3.0