Share via

bluetooth dependency servece

Eduardo Gomez 4,316 Reputation points
2022-08-05T00:34:26.777+00:00

I tried to implement the dependency service on Android but it did no work

 public interface IBluetoothService  
    {  
        Task<PermissionStatus> CheckStatusAsync();  
        Task<PermissionStatus> RequestAsync();  
  
    }  

IOS

public Task<PermissionStatus> CheckStatusAsync()  
        {  
            switch (CBManager.Authorization)  
            {  
                case CBManagerAuthorization.AllowedAlways:  
                    return Task.FromResult(PermissionStatus.Granted);  
                case CBManagerAuthorization.Restricted:  
                    return Task.FromResult(PermissionStatus.Restricted);  
                case CBManagerAuthorization.NotDetermined:  
                    return Task.FromResult(PermissionStatus.Unknown);  
                default:  
                    return Task.FromResult(PermissionStatus.Denied);  
  
  
            }  
        }  
  
        public Task<PermissionStatus> RequestAsync()  
        {  
            // Creating an instance of CBCentralManager will present the permission dialog.  
            new CBCentralManager();  
            return CheckStatusAsync();  
        }  
    }  

Android

public Task<PermissionStatus> CheckStatusAsync()  
        {  
            if (Build.VERSION.SdkInt >= BuildVersionCodes.S)  
            {  
                if (MainActivity.Instance.CheckSelfPermission(Manifest.Permission.BluetoothScan) == (int)Permission.Granted)  
                {  
                    return Task.FromResult(PermissionStatus.Granted);  
                }  
            }  
            else  
            {  
                if (MainActivity.Instance.CheckSelfPermission(Manifest.Permission.AccessFineLocation) == (int)Permission.Granted)  
                {  
                    return Task.FromResult(PermissionStatus.Granted);  
                }  
            }  
  
            return Task.FromResult(PermissionStatus.Denied);  
  
        }  
        public Task<PermissionStatus> RequestAsync()  
        {  
            var 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);  
            }  
            var requestPermission = (string[])permissions.ToArray(typeof(string));  
            return Task.FromResult(MainActivity.Instance.RequestPermissions(requestPermission  
                , MainActivity.RequestCode)); // Error  
        }  

I want to call it in my view model

 private async void PermissionActionAsync()  
        {  
            var bluePermissions = DependencyService.Get<IBluetoothService>();  
            var NetPermission = await Permissions.CheckStatusAsync<Permissions.NetworkState>();  
            var locationPermission = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();  
  
            if (NetPermission != PermissionStatus.Granted   
            && locationPermission != PermissionStatus.Granted)  
            && bluePermissions.CheckPermission != PermissionStatus.Granted //error  
            {  
  
            }  
        }  
Developer technologies | .NET | Xamarin

Answer accepted by question author

Anonymous
2022-08-08T08:50:58.1+00:00

Hello,

I do not see how to ask Bluetooth permission

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.

===================
Update=========================
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.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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