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.