Prompt User to enable bluetooth in Xamarin Forms

Published

Are there any workaround which I can use to prompt a user to enable bluetooth in Xamarin Forms / C#?

Like a Display Alert with ‘Yes’ or ‘No’ for prompting the user to switch on the bluetooth in case its not enabled.

If the user selects, ‘Yes’, the bluetooth is enabled.

Please help me to achieve this in both Android and iOS ! Thanks in advance.

Source: Xamarin.ios Questions

Published
Categorised as Uncategorised Tagged , , ,

Answers

In Xamarin Forms

if(await DisplayAlert(null,"Enable Bluetooth","Yes", "No"))
{
    // Enablebluetooth here via custom service
}

For bluetooth implementation download Xamarin.BluetoothLE in nugget or you can implement your own

In PCL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test.App
{
    public interface IBluetoothService
    {
        void OpenBluetooth();
        
    }
}

In Android

using System;
using BluetoothLE.Core;
using Android.Bluetooth;
using Java.Util;
using System.Collections.Generic;
using BluetoothLE.Core.Events;

namespace Test.App.Droid.Services
{
    public class BluetoothService : IBluetoothService
    {
    
        public void OpenBluetooth()
        {
        //native code here to open bluetooth
        }
    
    }
    
}

// register it on MainActivity


// do the same in ios


Christ Upton IV

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq