Apple introduced bluetooth 来自 iOS 13 的许可。
因此,对于 iOS 12 及更低版本,您无需请求或检查蓝牙权限。
要启用蓝牙,必须在 plist 文件中添加两个键。隐私 — 蓝牙外围设备使用说明和隐私 — 蓝牙始终使用说明。请说明您为什么要在应用中使用蓝牙。
Xamarin.Forms 共享 PCL 代码
依赖服务接口
public interface IPermissionService
{
bool HasBluetoothPermission();
void RequestBluetoothPermission(Action bluetoothAction);
}
查看状态和请求蓝牙权限的UI页面
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="StackQA2XF.BluetoothPermissionPage">
<ContentPage.Content>
<StackLayout Orientation="Vertical" Spacing="50" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<StackLayout Orientation="Horizontal" HeightRequest="60" >
<Button Text="Has Bluetooth Permission" Clicked="HasBluetooth_Clicked" />
<Label x:Name="LabelHasPermission" VerticalOptions="Center" FontSize="Micro" Margin="10,0,0,0" TextColor="OrangeRed" Text="No" />
</StackLayout>
<Button Text="Request Bluetooth Permission" Clicked="RequestBluetooth_Clicked"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
public partial class BluetoothPermissionPage : ContentPage
{
readonly IPermissionService service;
public BluetoothPermissionPage()
{
InitializeComponent();
service = DependencyService.Get<IPermissionService>();
}
protected override void OnAppearing()
{
base.OnAppearing();
CheckBluetooothPermission();
}
void HasBluetooth_Clicked(System.Object sender, System.EventArgs e)
{
CheckBluetooothPermission();
}
private void CheckBluetooothPermission()
{
LabelHasPermission.Text = service.HasBluetoothPermission() ? "YES" : "NO";
}
void RequestBluetooth_Clicked(System.Object sender, System.EventArgs e)
{
service.RequestBluetoothPermission(() =>
{
CheckBluetooothPermission();
});
}
}
iOS平台端(原生代码)
依赖服务实现。
[assembly: Dependency(typeof(StackQA2XF.iOS.Service.PermissionService))]
namespace StackQA2XF.iOS.Service
{
public class PermissionService : IPermissionService
{
Action? _bluetoothAction = null; //Optional, if you wanted to notify user that you have performed action (allow or deny) on the permission request dialog
public bool HasBluetoothPermission()
{
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
return CBCentralManager.Authorization == CBManagerAuthorization.AllowedAlways;
}
else
{
return true;
}
}
public void RequestBluetoothPermission(Action bluetoothAction)
{
_bluetoothAction = bluetoothAction;
var myDelegate = new PermissionCBCentralManager(this);
var centralManger = new CBCentralManager(myDelegate, DispatchQueue.MainQueue, new CBCentralInitOptions() { ShowPowerAlert = false });
}
internal void CurrentUpdatedState(CBCentralManager central)
{
_bluetoothAction?.Invoke();
}
}
public class PermissionCBCentralManager : CBCentralManagerDelegate
{
PermissionService permissionService = null;
public PermissionCBCentralManager(PermissionService controller)
{
permissionService = controller;
}
public override void UpdatedState(CBCentralManager central)
{
permissionService.CurrentUpdatedState(central);
}
}
}
Info.plist
为了向后支持旧 iOS 版本,
NSBluetoothPeripheralUsageDescription 需要在 info.plist 中定义
注意:如果CBCentralManager.ShowPowerAlert is true,当用户蓝牙设置为OFF时,会导致设备蓝牙设置显示ON的系统对话框。 IE。 CBCentralManager.ShowPowerAlert is true 如果蓝牙权限关闭或您的应用程序和您的 iPhone 中的蓝牙功能已关闭,将产生两个对话框。
ShowPowerAlert is false 时,不会显示此系统对话框。
不要使用模拟器来检查这个实现。
权限请求对话框将出现在 iOS 13 及更高版本中。
在 iOS 中,权限为 request dialog will display only once。因此,只有在第一次安装后,单击“请求蓝牙权限”时才能看到蓝牙权限请求对话框。一旦您拒绝并且稍后第二次单击“请求蓝牙权限”按钮时,将不会显示权限请求对话框。您可以通过显示带有按钮的自定义对话框来导航到应用程序的设置来处理这些场景。
请使用此测试应用程序供您参考LINK