How to check system Bluetooth is On/Off (platform - Windows) using .Net Maui programming?

Sowndarrajan Vijayaragavan 450 Reputation points
2023-06-21T12:10:59.5466667+00:00

BLUETOOTH.cs

using Windows.Devices.Radios;

namespace ZolaApplication.Platforms.Windows
{
    public class Bluetooth : IBluetooth
    {
        public 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;
        }
    }
}

IBLUETOOTH.cs

namespace ZolaApplication.Platforms.Windows
{
    public  interface IBluetooth
    {
        Task<bool> GetBluetoothIsEnabledAsync();
    }
}


User's image

How to call this in my required ViewModel

Developer technologies | .NET | .NET MAUI
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2023-06-22T07:36:11.2933333+00:00

    Hello,

    How to call this in my required ViewModel

    You implement the interface on Windows platform, but you don't know how to call the method in single shared MAUI project, right? If so, you could add a method in your ViewModel class and use conditional compilation to target Windows platforms. Or you can invoke the platform code directly. Please refer to the following code:

    
    #if WINDOWS
    Using ZolaApplication.Platforms.Windows
    #endif
    public  class ViewModel{
        public async void iSEnableAsync()
        {
    #if WINDOWS
            // invoke the method you defined 
            //Bluetooth bluetooth = new Bluetooth();
            //   bool isEnable =    await bluetooth.GetBluetoothIsEnabledAsync();
    
    //invoke the platform code directly
           var accessLevel = await Radio.RequestAccessAsync();// request access
            if (accessLevel != RadioAccessStatus.Allowed)
            {
                Debug.WriteLine("Failed");
            }
            else
            {
                var radios = await Radio.GetRadiosAsync();
                var bluetoothRadio = radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth);
                bool isEnable = bluetoothRadio != null && bluetoothRadio.State == RadioState.On;
                if (isEnable){...}
            }
                
    #endif
        }
    }
    

    And you could call the method.

    private  void OnCounterClicked(object sender, EventArgs e)
        {
            var VM = this.BindingContext as ViewModel;
            VM.iSEnableAsync();
        }
    

    In addition, please pay attention to pay attention to adding capability in Package.appxmanifest :   <DeviceCapability Name="radios"/>

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.