MAUI 应用程序未扫描 BLE

问题描述 投票:0回答:1

在我的 MAUI 应用程序中,我需要扫描 BLE 设备。我的代码在 Windows 上运行良好,但是在 Android 上,扫描没有发现任何设备。我知道我必须在清单中声明所需的权限并以交互方式请求它们。

我的 C# 最小示例:

using Plugin.BLE;
using Plugin.BLE.Abstractions.Contracts;

namespace BLE2
{
    public partial class MainPage : ContentPage
    {
        private IAdapter adapter;
        private IBluetoothLE ble;
        private List<IDevice> devices = [];

        public MainPage()
        {
            InitializeComponent();

            ble = CrossBluetoothLE.Current;
            adapter = ble.Adapter;
            adapter.ScanTimeout = 3000;
            adapter.ScanMode = ScanMode.Balanced;

            adapter.DeviceDiscovered += (sender, eventArgs) =>
            {
                devices.Add(eventArgs.Device);
            };
        }

        private void OnScanBtnClicked(object sender, EventArgs e)
        {
            adapter.StartScanningForDevicesAsync();
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            await RequestPermissionsAsync();
        }

        async Task RequestPermissionsAsync()
        {
            var locationPermitStatus = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
            if (locationPermitStatus != PermissionStatus.Granted)
            {
                await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
            }

            var bluetoothPermitStatus = await Permissions.CheckStatusAsync<Permissions.Bluetooth>();
            if (bluetoothPermitStatus != PermissionStatus.Granted)
            {
                await Permissions.RequestAsync<Permissions.Bluetooth>();
            }
        }
    }

}

AnddoidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:icon="@mipmap/appicon" android:supportsRtl="true"></application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <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-sdk />
</manifest>

调试时“观察”属性显示已授予 ale 权限。但是,事件adapter.deviceDiscovered 永远不会被触发(再次使用断点进行测试)。

我错过了什么?

android bluetooth bluetooth-lowenergy maui
1个回答
0
投票

我使用您的代码创建了一个新项目,并在 Android 14.0 的物理设备上对其进行了测试。但是当我点击按钮时应用程序会崩溃。我检查了nuget包github repo后发现原因是权限问题:

您可以查看Android权限部分

Android 12 及更高版本可能需要以下一项或多项附加运行时权限,具体取决于您所使用的库的功能(请参阅 Android 蓝牙权限文档)

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

将上述权限添加到 AndroidManifest.xml 后,

adapter.deviceDiscovered
被触发。

© www.soinside.com 2019 - 2024. All rights reserved.