Giter Site home page Giter Site logo

Comments (10)

smsissuechecker avatar smsissuechecker commented on June 2, 2024

Hi @Auto72,

I'm the friendly issue checker.
It seems like (100.00 %) you haven't used our issue template 😢 I think it is very frustrating for the repository owners, if you ignore them.

If you think it's fine to make an exception, just ignore this message.
But if you think it was a mistake to delete the template, please close the issue and create a new one.

Thanks!

from dotnet-bluetooth-le.

axa88 avatar axa88 commented on June 2, 2024

I wonder if the DeviceAdvertised event is effected by the Adapter.ScanMatchMode. I wouldn't think so but you might try setting Adapter.ScanMatchMode = STICKY and see if that avoids your device from processing duplicate packets from the same device as it does during a Scan.

Otherwise you might try setting Adapter.ScanMode = Passive, but i really doubt that is going to have any impact on the DeviceAdvertised event either.

That said i don't believe DeviceAdvertised is called back on the UI thread, so you might look at other ways you might be tying up the UI thread unwittingly, but honestly I'm not confident in any of the advice im giving you here will help so...

from dotnet-bluetooth-le.

Auto72 avatar Auto72 commented on June 2, 2024

Thanks for your tips, I will try them and report here the result.

NOTE: On Android and iOS I found the MainThread.IsMainThread = true inside the event DeviceAdvertised, but not on Windows. I tried this.

private void Adapter_DeviceAdvertised(object? sender, Plugin.BLE.Abstractions.EventArgs.DeviceEventArgs e)
{
    Debub.WriteLine($"IsMainThread: {MainThread.IsMainThread}");    // returns True on Android and iOS, but not on Windows.
}

from dotnet-bluetooth-le.

Auto72 avatar Auto72 commented on June 2, 2024

I used to set Adapter.ScanMode = ScanMode.LowLatency in Xamarin with no problem.
In .NET MAUI I have the issue reported above on Android.
Adapter.ScanMatchMode is and has been always STICKY.
Using Adapter.ScanMode = ScanMode.Balanced is better, but sometimes the behaviour is similar to ScanMode.LowLatency
With Adapter.ScanMode = Passive I don't receive any scan device.

from dotnet-bluetooth-le.

axa88 avatar axa88 commented on June 2, 2024

e is and has been always STICKY.
Using Adapter.ScanMode = ScanMode.Balanced is better, but sometimes the behaviour is similar to ScanMode.LowLatency
With Adapter.ScanMode = Passive I don't receive any scan device.

Unsure. im able to get good response both on windows and Android
Perhaps post a gist and someone might be able to comment...

from dotnet-bluetooth-le.

janusw avatar janusw commented on June 2, 2024

Are you saying that the DeviceAdvertised event on Android is only raised on the main thread in MAUI, but not in Xamarin? Or are you just saying that the performance is worse with MAUI?

from dotnet-bluetooth-le.

janusw avatar janusw commented on June 2, 2024

How to handle this situation probably depends a bit on your kind of setup and what you're trying to achieve.

First question: Are all of these advertisements even relevant for you? If not, you should probably use a scan filter to reduce the number of advertisements you have to process.

Then, of course, you should do as little work as possible in response to an advertisement (but you're probably already doing that, I hope).

And finally you can try to do the processing in the background, e.g. via a ThreadPool (which re-uses the threads in the pool and avoids the overhead of creating a new thread for each task).

AFAICS the library does nothing specific to put these events on the main thread, but just passes them on as they come from the OS.

from dotnet-bluetooth-le.

Auto72 avatar Auto72 commented on June 2, 2024

Are you saying that the DeviceAdvertised event on Android is only raised on the main thread in MAUI, but not in Xamarin? Or are you just saying that the performance is worse with MAUI?

No, I'm saying that in Xamarin as in .NET MAUI the DeviceAdvertised event is raised on the main thread.
While in Xamarin the event is raised 9 times per seconds, in .NET MAUI it is raised 106 times per seconds.
I tested the same environment with Xamarin App and .NET MAUI App.
In Xamarin the advertisements are only for my device, in .NET MAUI from all kind of devices.
The advertising interval for my BLE device is 2 or 3 times per second.

from dotnet-bluetooth-le.

Auto72 avatar Auto72 commented on June 2, 2024

How to handle this situation probably depends a bit on your kind of setup and what you're trying to achieve.

First question: Are all of these advertisements even relevant for you? If not, you should probably use a scan filter to reduce the number of advertisements you have to process.

Then, of course, you should do as little work as possible in response to an advertisement (but you're probably already doing that, I hope).

And finally you can try to do the processing in the background, e.g. via a ThreadPool (which re-uses the threads in the pool and avoids the overhead of creating a new thread for each task).

AFAICS the library does nothing specific to put these events on the main thread, but just passes them on as they come from the OS.

First answer:
No, I'm interested only in my devices, that have the name that start with a prefix.
I see how to put a filter, but it seems I can't specify a mask, like "Prefix*".
https://github.com/dotnet-bluetooth-le/dotnet-bluetooth-le#scan-filtering

I use the DeviceAdvertised event for two reasons:
a) I get the device before the event DeviceDiscovered is raised, so I can show it soon.
b) I get the value of RSSI updated.

Sure, I tried with Task.Run() but it doesn't work.
I think the library put all the advertisements in a queue and raise the event until the queue is empty, even if the scan has been stopped.

from dotnet-bluetooth-le.

Auto72 avatar Auto72 commented on June 2, 2024

I reviewed my code and now I used Task.Run() also in the DeviceDiscovered event and other adjustments.
I set Adapter.ScanMode = Balanced for Android.
Now seems a little bit better, but anyway it raises 90 events per minute.

I solved setting a filter by ServiceUuids.

var scanFilterOptions = new ScanFilterOptions { ServiceUuids = [Guid.Parse(MyStringGUID)] };
adapter.StartScanningForDevicesAsync(scanFilterOptions);

Thanks.

from dotnet-bluetooth-le.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.