Sample code for developing cross-platform applications on Android and iOS using Xamarin. The demos in this repository are stored on separate branches. To navigate to a demo, please click branches.
Other
8
stars
11
forks
source link
Asking for bluetooth permission for android 12 and above #12
For Android 12 and above, apps have to ask bluetooth permission explicitly to be able to scan and print.
I didn't want to create a PR, i don't want to change your sample, but the code below can be added to help developers about asking permissions for bluetooth. You guys can add this in a suitable place in the demo if you want.
1st step:
Install Xamarin.Essentials to be able to use Permissions API, and make sure you are targeting at least Android 12.
2nd step:
In shared project, create an interface like below
public interface IPlatformHelpers
{
Task<PermissionStatus> CheckAndRequestBluetoothPermissions();
}
3rd step:
In Android project, create a class like below (Don't forget to change namespace , YourAppName to your app name.)
[assembly: Dependency(typeof(PlatformHelpers))]
namespace YourAppName.Droid
{
public class PlatformHelpers : IPlatformHelpers
{
public async Task<PermissionStatus> CheckAndRequestBluetoothPermissions()
{
PermissionStatus status;
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.S)
{
status = await Permissions.CheckStatusAsync<BluetoothSPermission>();
if (status == PermissionStatus.Granted)
return status;
status = await Permissions.RequestAsync<BluetoothSPermission>();
}
else
{
status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
if (status == PermissionStatus.Granted)
return status;
status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
}
return status;
}
}
public class BluetoothSPermission : Xamarin.Essentials.Permissions.BasePlatformPermission
{
public override (string androidPermission, bool isRuntime)[] RequiredPermissions => new List<(string androidPermission, bool isRuntime)>
{
(Android.Manifest.Permission.BluetoothScan, true),
(Android.Manifest.Permission.BluetoothConnect, true)
}.ToArray();
}
}
4th step:
Make sure you added this lines to your Android app manifest
Hello.
For Android 12 and above, apps have to ask bluetooth permission explicitly to be able to scan and print.
I didn't want to create a PR, i don't want to change your sample, but the code below can be added to help developers about asking permissions for bluetooth. You guys can add this in a suitable place in the demo if you want.
1st step:
Install Xamarin.Essentials to be able to use Permissions API, and make sure you are targeting at least Android 12.
2nd step: In shared project, create an interface like below
3rd step: In Android project, create a class like below (Don't forget to change namespace , YourAppName to your app name.)
4th step:
Make sure you added this lines to your Android app manifest
5th step: Before scanning for bluetooth devices or printing, check and ask for the bluetooth permission
You can scan , connect and print after the app has the permission granted.
I just needed to do this modifications to my app because of the changes for bluetooth permissions for Android 12 and above.
Hope this helps people like me.