1

are new to the NET MAUI mobile app development. I'm creating an app that scans for wifi networks nearby, and if it finds a particular network, it has to do some operations. I want this app to work even in the background, continuing to update the list of networks found and run the method I created.

how can this be done?

  • I am pretty sure you need to search on how to do it in android and ios - then translate that to C# - native APIs are available to you – Krzysztof Skowronek Aug 31 '23 at 09:21
  • Before you continue with your project ask yourself this: "Is one scan per half hour enough"? And if the answer is "NO", scrap it. – H.A.H. Aug 31 '23 at 10:17

1 Answers1

-1

If your purpose is not to block the UI while scanning the networks, you can simply use async/await. Consider the following simplified code example:

class WifiNetworkViewModel : ViewModelBase
{
    public WifiNetworkViewModel()
    {
        Networks = new ObservableCollection<WifiNetwork>();
    }

    public ObservableCollection<WifiNetwork> Networks { get; }

    public async Task ScanWifiNetworks()
    {
        while (true)
        {
            // Scanning logic here (use await)
            // When found one, add it to the Networks collection
            Networks.Add(new WifiNetwork { Name = "Wifi Network 1" });
            // Break the while loop with timeout etc.
        }
    }
}

class WifiNetwork
{
    public string Name { get; set; }
}

To use this view model, I assume you have a list control such as ListView and bind it to the Networks property like

<ListView ItemsSource="{Binding Networks}"/>

When a found network is added to the Networks property then the ListView will populate automatically. To start scanning, call the ScanWifiNetworks() method from a Button for example.

Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16
  • I have to find a method that allows me to run the app in the backgriud, continuing to run the network scan method, the user should see a notification that the app is active and working. – peppe loria Aug 31 '23 at 14:18
  • How is this ACCEPTED answer? How? It creates class Network with field String Name, and adds it. Another answer without a single line of code for scanning. @peppeloria do you have any expertise at programming? Why do you think new Network("123") is useful answer? – H.A.H. Sep 01 '23 at 13:13
  • @H.A.H.: Hey this is just a template (or pseudo) code to show the skeleton of the program to perform background processing while holding the UI responsive. Moreover as I say in the beginning of the question, I intentionally simplified the code to emphasize the main points. Don't expect me to provide a ready-to-use production code here. I am just giving a hint to the OP. – Mustafa Özçetin Sep 01 '23 at 13:30
  • By the way, SO never requires users to have expertise in programming. Enthusiast or newbie programmers are also welcome. – Mustafa Özçetin Sep 01 '23 at 13:34
  • @MustafaÖzçetin all your code does is assigning hard coded string in a constructor. On a question about Background Network scan on mobile devices. Even a newbie programmer will know that this is not enough. – H.A.H. Sep 01 '23 at 14:12
  • @H.A.H.: How are you reading code? My code does not consist only of a constructor having a hard coded initialization. There are also a `Networks` property and a simulated `ScanWifiNetworks()` method with brief instructions. Also view and view model info. Let the OP decides what is or is not enough or *useful*. There is nothing preventing you from writing a better answer, by the way. – Mustafa Özçetin Sep 01 '23 at 15:28
  • @MustafaÖzçetin your code looks like something ChatGPT will answer, and does not call any relevant to the question API. In other words, compiling this will not produce a single processor instruction, that will have anything to do with background network scan on mobile device. new Network("123") is not an answer. – H.A.H. Sep 01 '23 at 15:52
  • First you need to learn what is pseudo code and then continue discussing. Give up locking on a single line of code and provide your *great* answer. – Mustafa Özçetin Sep 01 '23 at 17:57