xamarin-bluetooth-le 不扫描设备

问题描述

我正在使用 xamarin-bluetooth-le 在 Xamarin 上制作 BluetoothLE 客户端。
搜索了一些 BluetoothLE 包(示例代码工作正常)。 因此,我选择了 xamarin-bluetooth-le。

事实上,示例代码工作正常。
但是,我在没有视图的情况下简化了示例代码,因为我不需要视图并且不需要绑定。

但是 xamarin-bluetooth-le 不会通过简化代码扫描设备。

一个问题:
在 xamarin-bluetooth-le 示例中,名为 Devicediscovered 的事件需要分配两次?(一次是 DeviceListviewmodel 构造函数,一次是 scan 方法)。

第二(主要)问题:
为什么 xamarin-bluetooth-le 不通过以下代码扫描设备?

环境是
Visual Studio 2019 16.10.0
Xamarin 16.10.000.228
插件.BLE 2.1.2
其他最新

代码如下:
蓝牙客户端.cs

using Plugin.BLE;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions.EventArgs;
using Plugin.BLE.Abstractions.Extensions;
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;

namespace PlugInBLETest.NetowrkModels
{
    public class BluetoothBLEClient
    {
        private IAdapter Adapter;
        private IBluetoothLE Current;
        public ObservableCollection<IDevice> DeviceList { get; }
        private CancellationTokenSource CancelSource;

        public BluetoothBLEClient()
        {
            Current = CrossBluetoothLE.Current;
            Adapter = CrossBluetoothLE.Current.Adapter;

            Adapter.Devicediscovered += OnDevicediscovered;
            Adapter.ScanTimeoutElapsed += OnScanTimeoutElapsed;
            Adapter.Devicedisconnected += OnDevicedisconnected;
            Adapter.DeviceConnectionLost += OnDeviceConnectionLost;

            DeviceList = new ObservableCollection<IDevice>();
        }


        public async Task SearchDevices()
        {
            if (Current.State == BluetoothState.Off)
            {
                return;
            }
            else
            {
                DeviceList.Clear();

                foreach (var connectedDevice in Adapter.ConnectedDevices)
                {
                    try
                    {
                        await connectedDevice.UpdateRSSiAsync();
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                }

                CancelSource = new CancellationTokenSource();
                Adapter.ScanMode = ScanMode.LowLatency;

                Adapter.ScanTimeout = 30000;

                //Adapter.Devicediscovered += (s,a) => DeviceList.Add(a.Device);
                Adapter.Devicediscovered += (s,a) =>
                {
                    DeviceList.Add(a.Device);
                };
                await Adapter.StartScanningForDevicesAsync(CancelSource.Token);

                var temp = DeviceList.Count;
            }
            return;
        }

        private void OnDeviceConnectionLost(object sender,DeviceErrorEventArgs e)
        {
        }

        private void OnDevicedisconnected(object sender,DeviceEventArgs e)
        {
        }

        private void OnScanTimeoutElapsed(object sender,EventArgs e)
        {
        }

        private void OnDevicediscovered(object sender,DeviceEventArgs args)
        {
        }
    }
}

AndroidManifext.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.pluginbletest">
    <uses-sdk android:targetSdkVersion="30" />
    <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" />
    <application android:label="PlugInBLETest.Android" android:theme="@style/MainTheme"></application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

解决方法

Xamarin.Essentials 有一个 permissions feature,可让您检查并请求访问位置数据的权限

在 Marshmallow 之后,您必须提示用户授予权限 -> https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/