Opening bluetooth settings page and returning to application

Hello Every one,

I have created application . In which i have to open bluetooth setting page of my Android application via clicking on a button created . As soon as the settings open i need to pair device similar to paring in normal device . If i click back button it should return back to my created application.

I am new to xamarin application please help me

Answers

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai
    Intent intentOpenBluetoothSettings = new Intent();
    intentOpenBluetoothSettings.SetAction(Android.Provider.Settings.ActionBluetoothSettings);
    StartActivity(intentOpenBluetoothSettings);
    
  • rahul_krahul_k Member ✭✭

    @LucasZhang It show error for StartActivity function.

  • rahul_krahul_k Member ✭✭
    edited May 28

    @LucasZhang this is my script for button click

          private void ConnectDevice_Button(object sender, EventArgs e)
         {
    
            Intent intentOpenBluetoothSettings = new Intent();
            intentOpenBluetoothSettings.SetAction(Android.Provider.Settings.ActionBluetoothSettings);
          StartActivity(intentOpenBluetoothSettings);
    
         }
    

    it shows error "Name StartActivity does not exist in current context"

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    StartActivity is a Built-in method of Android .

    using System;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Support.Design.Widget;
    using Android.Support.V7.App;
    using Android.Views;
    using Android.Widget;
    

    And you can delete the folder bin and obj in your solution .Then clean and rebuild your project.

  • rahul_krahul_k Member ✭✭

    @LucasZhang Do i need to create this file in Android part? I.e appname.Andriod , Or is it alright creating this in Common ?

  • rahul_krahul_k Member ✭✭
    edited May 28

    @LucasZhang This how i have stored my file appliaction

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    Right click your android project ->manger nuget packages
    And update the package

    then add the following code

    using Android.Support.V7.App;
    
  • rahul_krahul_k Member ✭✭

    @LucasZhang No xamarin.Essentials avilable in browse and installed packages

  • rahul_krahul_k Member ✭✭

    Where should i call this then . I have code for xaml in form when button clicks its should open settings

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    You can use DependencyService to implement this function:

    in forms ,create an interface

    public interface IOpenBluetoothPage
    {
       void OpenBluetoothSettingPage();
    }
    

    in Android Project

    using Android.Content;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    using xxx;
    using xxx.Droid;
    
    [assembly: Dependency(typeof(OpenBluetoothImplementation))]
    namespace xxx.Droid
    {
        public class OpenBluetoothImplementation : IOpenBluetoothPage
        {
            public void OpenBluetoothSettingPage()
            {
                Intent intentOpenBluetoothSettings = new Intent();
                intentOpenBluetoothSettings.SetAction(Android.Provider.Settings.ActionBluetoothSettings);
                Android.App.Application.Context.StartActivity(intentOpenBluetoothSettings);
            }
        }
    }
    

    And in your button click handle

    private void ConnectDevice_Button(object sender, EventArgs e)
    {
        DependencyService.Get<IOpenBluetoothPage>().OpenBluetoothSettingPage();
     }
    
Sign In or Register to comment.