以前,我使用插件Bluetooth LE plugin for Xamarin添加了代码以扫描ble设备。 它已经工作了一段时间,但是我已经更新到了最新版本的Xamarin和Visual Studio,现在它只返回列表中的0个设备。我正在使用Android版本8.1进行编译。我的代码有问题还是更新有问题?
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Plugin.BLE;
using Plugin.BLE.Abstractions.Exceptions;
using System.Collections.Generic;
using Android.Bluetooth;
using Whale_Watch.Droid;
namespace Whale_Watch.Droid
{
[Activity(Label = "BLE_Test", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected async override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
await scanAsync();
}
public async System.Threading.Tasks.Task scanAsync()
{
List<Plugin.BLE.Abstractions.Contracts.IDevice> deviceList = new List<Plugin.BLE.Abstractions.Contracts.IDevice>();
var ble = CrossBluetoothLE.Current;
var adapter = CrossBluetoothLE.Current.Adapter;
var state = ble.State;
ble.StateChanged += (s, e) =>
{
};
adapter.DeviceDiscovered += (s, a) => deviceList.Add(a.Device);
await adapter.StartScanningForDevicesAsync();
}
}
}