【问题标题】:xamarin-bluetooth-le does not scan devicesxamarin-bluetooth-le 不扫描设备
【发布时间】:2021-07-22 03:14:40
【问题描述】:

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

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

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

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

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

环境是
Visual Studio 2019 16.10.0
Xamarin 16.10.000.228
Plugin.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 bluetooth bluetooth-lowenergy


【解决方案1】:

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

在 Marshmallow 之后你必须提示用户获得许可 -> https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多