A community member has associated this post with a similar question:
bluetooth dependency servece

Only moderators can edit this content.

Bluetooth dependency injection

Eduardo Gomez 3,416 Reputation points
2022-08-06T08:37:30.587+00:00

Hello everyone

I have a Xamarin forms app, that needs Bluetooth permissions, I have already created the interface, and implemented it on IOS, but I cannot figure out how to implement it on Android

    // Xamarin essentials, does not provide a way to ask for Bluetooth permission  
    public interface IBluetoothService  
    {  
        PermissionStatus CheckPermission();  
        PermissionStatus RequestPermissionA();  
    }  

IOS

namespace Test.APP.iOS.Interfaces  
{  
    public class BluetoothService : IBluetoothService  
    {  
        public PermissionStatus CheckPermission()  
        {  
            switch (CBManager.Authorization)  
            {  
                case CBManagerAuthorization.AllowedAlways:  
                    return PermissionStatus.Granted;  
                case CBManagerAuthorization.Restricted:  
                    return PermissionStatus.Restricted;  
                case CBManagerAuthorization.NotDetermined:  
                    return PermissionStatus.Unknown;  
                default:  
                    return PermissionStatus.Denied;  
            }  
        }  
        public PermissionStatus RequestPermission()  
        {  
            // Creating an instance of CBCentralManager will present the permission dialog.  
            new CBCentralManager();  
            return CheckPermission();  
        }  
    }  

What I want to do at the end, is to call it here to see if it has been granted

  private async void PermissionActionAsync()  
        {  
            var NetPermission = await Permissions.CheckStatusAsync<Permissions.NetworkState>();  
            var locationPermission = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();  
// Here I would create a Bluetooth variable  
  
            if (NetPermission != PermissionStatus.Granted   
            && locationPermission != PermissionStatus.Granted)  
            && // Concatenate the Bluetooth permission  
            {  
  
            }  
        }  

So in the end I am checking if I have permission for location, network state an Bluetooth

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2022-08-08T07:05:14.86+00:00

    Hello,​

    If you target-framework set to android 11 or before in the android, you do not need to request Bluetooth permission. Just request location permission.

    If you set target-framework version to android 12 or later, you need to request Manifest.Permission.BluetoothScan, Manifest.Permission.BluetoothConnect and Manifest.Permission.BluetoothAdvertise permission at runtime. For details, please refer to this document: Bluetooth permission.

    You can use the following code to request bluetooth permission. Please do not forget to add public static MainActivity Instance; then set a value with Instance = this; in MainActivity.cs, and add public static int RequestCode=10023; as well.

    Based on your screenshot and other thread, If you want to use depdenceServce and async method to achieve it You need to change your interface like following code.

       public interface IBluetoothService  
           {  
               Task<PermissionStatus> CheckPermission();  
               Task<PermissionStatus> RequestPermissionA();  
       }  
    

    When you need to check permission and request permission, you need to achieve it like following code. Note, if you want to handle other PermissionStatus, you can add judgment conditions.

       [assembly: Dependency(typeof(BluetoothService))]  
       namespace BluetoothPermission.Droid  
       {  
           internal class BluetoothService : IBluetoothService  
           {  
                
               public async Task<PermissionStatus> CheckPermission()  
               {  
         
                   if (Build.VERSION.SdkInt >= BuildVersionCodes.S)  
                   {  
                       if ( MainActivity.Instance.CheckSelfPermission(Manifest.Permission.BluetoothScan) == (int)Permission.Granted )  
                       {  
                           return await Task.FromResult(PermissionStatus.Granted);  
                       }  
                   }  
                   else  
                   {  
                       if ( MainActivity.Instance.CheckSelfPermission(Manifest.Permission.AccessFineLocation) == (int)Permission.Granted)  
                       {  
                           return await Task.FromResult(PermissionStatus.Granted);  
                       }  
                   }  
                         
         
                   return await Task.FromResult(PermissionStatus.Denied);  
               }  
         
               public async Task<PermissionStatus> RequestPermissionA()  
               {  
                   ArrayList permissions = new ArrayList();  
         
         
                   if (Build.VERSION.SdkInt >= BuildVersionCodes.S)  
                   {  
                       //if Android version bigger that Android12  
         
                       permissions.Add(Manifest.Permission.BluetoothScan);  
                       permissions.Add(Manifest.Permission.BluetoothConnect);  
                       permissions.Add(Manifest.Permission.BluetoothAdvertise);  
                   }  
                   else  
                   {  
                       // Android version less than  Android12               
                       permissions.Add(Manifest.Permission.AccessFineLocation);  
                   }  
         
         
                   string[] requestPermission= (String[])permissions.ToArray(typeof(string)) ;  
                   MainActivity.Instance.RequestPermissions(requestPermission, MainActivity.RequestCode);  
         
                   return await CheckPermission();  
               }  
           }  
    

    In the end, you can use it. I create two buttons for testing.

       private async void Button_Clicked(object sender, EventArgs e)  
               {  
                 var permissionStatus= await DependencyService.Get<IBluetoothService>().CheckPermission();  
             
               }  
         
               private async void Button_Clicked_1(object sender, EventArgs e)  
               {  
                 var res=await  DependencyService.Get<IBluetoothService>().RequestPermissionA();  
         
               }  
    

    Best Regards,

    Leon Lu


    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.