BluetoothLEAdvertisementWatcher.Received is not supported by the language; try directly calling accessor methods .add_Received

86 views Asked by At

I am following a video tutorial about Bluetooth connection uploaded to this github repository specific code file https://github.com/angelsix/blueberry/blob/master/Source/Blueberry.Desktop.WindowsApp.Bluetooth/DnaBluetoothLEAdvertisementWatcher.cs, but following the video (https://www.youtube.com/watch?v=RVasdDtgLKY) steps so my actual code is like following:

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
    }
}

The problem I have is I can't get it to work because it throws me the following error: Property, indexer, or event 'BluetoothLEAdvertisementWatcher.Received' is not supported by the language; try directly calling accessor methods 'BluetoothLEAdvertisementWatcher.add_Received

enter image description here

I barely know from the error that I need to use add_Received method, but there is no documentation nor code snippets to use that method around all internet, even stackoverflow.

What is the correct way to use the function so I can add events to the BluetoothLEAdvertisementWatcher?

I am frustrated from doing this for about 3 days.

However I use it, even with the help of ChatGPT, I get the error "mscorlib not found".

1

There are 1 answers

2
LWChris On

BluetoothLEAdvertisementWatcher was only introduced in Windows 10.0.10240. You need to set the target OS to Windows and the OS version to at least 10.0.10240.0.

I actually have multiple projects using the BluetoothLEAdvertisementWatcher, and these are the project configurations that work on my machine:

.NET (Core)

In the *.csproj file, I set the target framework to net6.0-windows10.0.17763. Then I added using Windows.Devices.Bluetooth.Advertisement; and was able to attach a delegate.


.NET Framework

I set the target framework version to v4.7.2 and added a reference to the Windows.winmd file from the Windows 10 or 11 SDKs. My current setup is using the file from C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.22000.0\ which is the "Windows 11 SDK". You can install these SDKs from the VS Installer's "Individual components" tab; just search for "Windows SDK" to get a list of the available kits, any kit newer than 10.0.10240.0 will do. I think the oldest SDK available is 10.0.18362.0 already, so that should work as well.

I also included a reference to the System.Runtime.WindowsRuntime.dll to get access to the CopyTo(this IBuffer source, byte[] destination) extension, which is useful to deal with the Windows.Storage.Streams.IBuffer objects that many BLE methods use. I found this DLL in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\ which I could reference even though this is a framework project.