Ok so I am aware there are some similar questions such as:
Adding and Removing Anonymous Event Handler Unsubscribe anonymous method in C#
But I don't understand the concept of delegates.
I am starting to use the Plugin.BLE in a .Net Maui app.
The scanning operation is started from a button and then either times out (by use of a Timer) or is stopped by pressing the button again.
However in my button command (MVVM) I have the following snippet of code:
...
adapter.DeviceDiscovered += (s, a) =>
{
if (a.Device.Name != null && a.Device.Name != String.Empty)
{
...
}
};
await adapter.StartScanningForDevicesAsync();
...
I note that each time I hit the button I get two more discovered items (I'm not sure why I'm getting 2 yet?) (This is from Pixel 5 emulator)
This makes some kind of sense as I am adding another event to the same adapter!
So I need to convert the anonymous function
adapter.DeviceDiscovered += (s, a) =>
{
}
into a non anonymous function, so that I can add the handler and then remove it when the timer stops or I stop the function.
I have no idea how to go about this, especially in dealing with the s and the a.
I'd be grateful for any pointers, code.
Thanks, G.
Edit: link to Plguin.BLE https://github.com/xabre/xamarin-bluetooth-le