我正在关注上传到此 github 存储库特定代码文件的有关蓝牙连接的视频教程https://github.com/angelsix/blueberry/blob/master/Source/Blueberry.Desktop.WindowsApp.Bluetooth/DnaBluetoothLEAdvertisementWatcher.cs,但按照视频(https://www.youtube.com/watch?v=RVasdDtgLKY)步骤,所以我的实际代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Foundation;
namespace BluetoothConnection.Desktop.BluetoothDotNet
{
/// <summary>
/// Wraps and makes use of the <see cref="BluetoothLEAdvertisementWatcher"/>
/// for easier consumption
/// </summary>
public class DnaBluetoothBluetoothLEAdvertisementWatcher
{
#region Private Members
/// <summary>
/// The underlying bluetooth watcher class
/// </summary>
private readonly BluetoothLEAdvertisementWatcher mWatcher;
#endregion
#region Public Events
/// <summary>
/// Fired when the bluetooth watcher stops listening
/// </summary>
public event Action StoppedListening = () => { };
/// <summary>
/// Fired when the bluetooth watcher starts listening
/// </summary>
public event Action StartedListening = () => { };
#endregion
#region Constructor
/// <summary>
/// The default constructor
/// </summary>
public DnaBluetoothBluetoothLEAdvertisementWatcher()
{
// Create bluetooth listener
mWatcher = new BluetoothLEAdvertisementWatcher
{
ScanningMode = BluetoothLEScanningMode.Active
};
// Listen out for new advertisements
mWatcher.Received += WatcherAdvertisementReceived;
// Listen out for when the watcher stops listening
mWatcher.Stopped += (watcher, e) =>
{
// Inform listeners
StoppedListening();
};
}
#endregion
#region Private Methods
/// <summary>
/// Listens out for watcher advertisements
/// </summary>
/// <param name="sender">The watcher</param>
/// <param name="args">The arguments</param>
private void WatcherAdvertisementReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
}
#endregion
}
}
我遇到的问题是我无法让它工作,因为它引发了以下错误: 该语言不支持属性、索引器或事件“BluetoothLEAdvertisementWatcher.Received”;尝试直接调用访问器方法'BluetoothLEAdvertisementWatcher.add_Received
我从错误中几乎不知道我需要使用 add_Received 方法,但是整个互联网上都没有文档或代码片段可以使用该方法,甚至 stackoverflow 也没有。
使用该函数的正确方法是什么,以便我可以将事件添加到BluetoothLEAdvertisementWatcher?
这样做了大约 3 天,我感到沮丧。
无论我如何使用它,即使在 ChatGPT 的帮助下,我也会收到错误“mscorlib not found”。
BluetoothLEAdvertisementWatcher
仅在 Windows 10.0.10240 中引入。您需要将目标操作系统设置为Windows,并且操作系统版本至少为10.0.10240.0。
我实际上有多个使用
BluetoothLEAdvertisementWatcher
的项目,这些是在我的机器上运行的项目配置:
在 *.csproj 文件中,我将目标框架设置为
net6.0-windows10.0.17763
。然后添加 using Windows.Devices.Bluetooth.Advertisement;
并能够附加代表。
我将目标框架版本设置为
v4.7.2
,并添加了对Win10SDK中的Windows.winmd
文件的引用。我在C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.22000.0\
找到了这个文件。您的文件夹的版本可能有所不同。我还提供了对 System.Runtime.WindowsRuntime.dll
的引用,以访问 CopyTo(this IBuffer source, byte[] destination)
扩展,这对于处理许多 BLE 方法使用的 Windows.Storage.Streams.IBuffer
对象非常有用。我在C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\
中找到了这个DLL,即使这是一个框架项目,我也可以参考它。