首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >.net Maui蓝牙在Android设备上的运行时权限检查和设置

.net Maui蓝牙在Android设备上的运行时权限检查和设置
EN

Stack Overflow用户
提问于 2022-06-10 09:11:35
回答 3查看 1.9K关注 0票数 0

既然微软已经决定蓝牙(显然是Wi)不值得出现在毛伊岛的路线图上,我想我们必须自己去做:叹息。https://github.com/dotnet/maui/discussions/277见David的评论

我承认,即使是从1977年开始用PDP-8纸带对电脑进行断断续续的编程之后,我也在努力为我的代码找到合适的结构,甚至理解它是如何工作的,所以如果我做错了事情,请原谅我,让我走上正确的轨道。

因为这对未来的新手有好处--万岁!我会尽我所能详细说明的。

我有一个Maui应用程序,它有一个外壳飞出菜单和一些ViewModels,我正在使用CommunityToolkit.Mvvm (通过NuGet)使我的编码更容易绑定。

文件夹结构是Pages>BluetoothPage.xaml,它是文件BluetoothPage.xaml.cs和其他页面后面的相应代码。有一个用于类的Models文件夹,其中包含一个BluetoothPeripheral.cs类,该类保存了我所连接的外围设备所需的属性类,还有一个ViewModels文件夹,其中有一个BluetoothPeripheralsViewModel.cs,用于绑定到我的BluetoothPage。

当我启动时,我可以从shell中选择蓝牙,然后转到BluetoothPage.xaml,它就在我开始工作的文件后面的代码中。

我成功地使用了plugin.ble来启动,但是当我想要扫描时,我马上就在Android上得到了这个问题(因为version23 (马赛洛?))该应用程序需要提示用户接受使用蓝牙的许可。

我还在Platforms>Android>Resources>AndroidManifest.xml文件中添加了以下行

代码语言:javascript
运行
复制
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

  <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
  <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
</manifest>

下面是我的基本代码和注释--(我猜想有一种更复杂的方法可以做到这一点,但就目前而言,这是我的大脑所能处理的。)

BluetoothPage.xaml.cs -后面的代码

代码语言:javascript
运行
复制
//other imports are in a GlobalUsing.cs file
using Android;
using Android.OS;

namespace TSDZ2Monitor.Pages;

public partial class BluetoothPage : ContentPage
{
    public BluetoothPage()
    {
        InitializeComponent();

        *//so I start my code here, but I suspect that I should be using some kind of page displayed, on startup event?*

    Console.WriteLine("Bluetooth here");

    getBLEPermission();
    startScan();
    
    *//Need to get user to accept bluetooth permissions in Android 6.0+,v23 that is the Run-time Permissions check - should this be in the constructor?*
    async void getBLEPermission()
    {
      bool blePermissionGranted = false;

      int sdk = (int)Build.VERSION.SdkInt; //<-- if you want to check > 23


      //Do the check to see if permission already granted - how?


      if (!blePermissionGranted)  //so go and get permission, display an alert
      {  

        blePermissionGranted = await App.Current.MainPage.DisplayAlert("Alert", "Allow use of bluetooth for scanning for your peripheral devices such as heart rate monitor etc?", "Allow", "Deny");
        if (blePermissionGranted)
        {
          //Set the permission on android - how

        }
        //Console.WriteLine($"SDK version {sdk}");
        //Console.WriteLine($"BLE permission granted: {blePermissionGranted}");

      }

    }

    async void startScan()
    {
       //TODO 
    }
  }

}

Q1如何测试蓝牙权限是否设置?

如果不是Q2,我该如何设置它?

非常感谢你的建议等。

编辑(补充):所以我已经想出了如何使用.net Maui提供的权限。例如,下面是在docs https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/appmodel/permissions?tabs=android中使用位置的示例

代码语言:javascript
运行
复制
//using InTheHand.Bluetooth;

namespace TSDZ2Monitor.Pages;

public partial class BluetoothPage : ContentPage
{
    public BluetoothPage()
    {
        InitializeComponent();

        Console.WriteLine("Bluetooth here");

    getBLEPermission();

    static async void getBLEPermission()
    {
      PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
      if (status != PermissionStatus.Granted)
      {
        status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
        if (status != PermissionStatus.Granted)
        {
          //well kill the app because it's no use if bluetooth not enabled
        }
      }
    }


  }

}

请注意,我不需要做警告,Android是为我做的。

当然,蓝牙权限不是这个场景的一部分。加上这个会有多难呢?如果您是MS,请这样做。

有人有什么想法吗?

EN

Stack Overflow用户

回答已采纳

发布于 2022-06-12 07:41:52

有关有效的解决方案,请参见此处的讨论https://github.com/dotnet/maui/discussions/277

和完整的代码解决方案在这里https://gist.github.com/salarcode/da8ad2b993e67c602db88a62259d0456#gistcomment-4197943

我非常感谢所有贡献过的人,尤其是关于github的salarcode。干杯。

(我将把它留给其他人来标记答案:)

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72571827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档