Quantcast
Viewing all articles
Browse latest Browse all 204402

Scanning Wifi from PCL project using Dependency Service

Hello!
I need to scan the wifi programatically and return a list of all SSID's I get. I've read about it and concluded I can't make it on iOS in a way I can publish the App on the Apple Store(ref).
So, I decided to at least implement it for Android Users, I'm trying to implement via Dependency Service but I'm doing it wrong, here is what I'm doing:

This is the Wifi.cs class in the Android project:

using AirSenseApp.Services;
using Android.Content;
using Android.Net.Wifi;
using System.Collections.Generic;

[assembly: Xamarin.Forms.Dependency(typeof(AirSenseApp.Droid.Classes.Wifi))]
namespace AirSenseApp.Droid.Classes
{
    public class Wifi:IWifi
    {
        private Context context = null;
        private static WifiManager wifi;
        private WifiReceiver wifiReceiver;
        public static List<string> WiFiNetworks;

        public Wifi(Context ctx)
        {
            this.context = ctx;
        }

        public void GetWifiNetworks()
        {
            WiFiNetworks = new List<string>();

            // Get a handle to the Wifi
            wifi = (WifiManager)context.GetSystemService(Context.WifiService);

            // Start a scan and register the Broadcast receiver to get the list of Wifi Networks
            wifiReceiver = new WifiReceiver();
            context.RegisterReceiver(wifiReceiver, new IntentFilter(WifiManager.ScanResultsAvailableAction));
            wifi.StartScan();
        }

        public class WifiReceiver:BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {
                IList<ScanResult> scanwifinetworks = wifi.ScanResults;
                foreach(ScanResult wifinetwork in scanwifinetworks)
                {
                    WiFiNetworks.Add(wifinetwork.Ssid);
                }
            }
        }
    }
}

This is my Interface that I'm trying to implement on the PCL project:

using System;
using System.Collections.Generic;
using System.Text;

namespace AirSenseApp.Services
{
    public interface IWifi
    {
        void GetWifiNetworks();

    }
}

Is this the correct way to implement it? I tried to put it in my ViewModel this way:

public class CreateDeviceViewModel:INotifyPropertyChanged
{
    private readonly IWifi _wifiService;
    public CreateDeviceViewModel()
    {
        this._wifiService = DependencyService.Get<IWifi>();
    }
    public ScanWifiButtonPressed()
    {
        _wifiService.GetWifiNetworks();
    }
}

But I receive this error:
[0:] System.MissingMethodException: Default constructor not found for type AirSenseApp.Droid.Classes.Wifi

This means I'm not implementing the Wifi.cs in the Android project in the correct way or the problem is in the Interface?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>