Last active
February 17, 2024 16:07
-
-
Save zuckerthoben/ee097b816b88491f5874bb327ec6819c to your computer and use it in GitHub Desktop.
View Mac Address of device in the settings page (UWP, Android, iOS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a workaround to get the Mac address of a device. The user has to manually copy the MAC address from the device, | |
// because you are not able to directly query the MAC from UWP, iOS and Android. | |
// Supported Platforms: UWP (Mobile & Desktop), Android, iOS | |
// In PCL | |
namespace Project.Interfaces | |
{ | |
/// <summary> | |
/// Interface to implement a function to open wifi settings on each platform | |
/// </summary> | |
public interface IOpenWifiSettings | |
{ | |
/// <summary> | |
/// Opens wifi settings on the device | |
/// </summary> | |
void OpenWifiSettings(); | |
} | |
} | |
// Android | |
using Project.Droid.Helpers; | |
using Xamarin.Forms; | |
[assembly: Dependency(typeof(OpenWifiSettingsHelper))] | |
namespace Project.Droid.Helpers | |
{ | |
using System; | |
using Android.Content; | |
using WerDaXamarin.Interfaces; | |
/// <summary> | |
/// Implements <see cref="IOpenWifiSettings"/> to get into Wifi settings quickly on Android | |
/// </summary> | |
public class OpenWifiSettingsHelper : IOpenWifiSettings | |
{ | |
/// <summary> | |
/// Opens wifi settings on this Android device | |
/// </summary> | |
public void OpenWifiSettings() | |
{ | |
try | |
{ | |
using (var intent = new Intent(Android.Provider.Settings.ActionWifiSettings)) | |
{ | |
Forms.Context.StartActivity(intent); | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine(ex.Message); | |
throw new InvalidOperationException("Opening Wifi settings was not possible"); | |
} | |
} | |
} | |
} | |
// iOS | |
using Project.IOS.Helpers; | |
using Xamarin.Forms; | |
[assembly: Dependency(typeof(OpenWifiSettingsHelper))] | |
namespace Project.IOS.Helpers | |
{ | |
using System; | |
using System.Diagnostics; | |
using Foundation; | |
using UIKit; | |
using WerDaXamarin.Interfaces; | |
/// <summary> | |
/// Implementation of <see cref="IOpenWifiSettings"/> on iOS | |
/// </summary> | |
public class OpenWifiSettingsHelper : IOpenWifiSettings | |
{ | |
/// <summary> | |
/// Opens the wifi settings on the device | |
/// </summary> | |
public void OpenWifiSettings() | |
{ | |
try | |
{ | |
var wifiUrl = new NSUrl("prefs:root=WIFI"); | |
if (UIApplication.SharedApplication.CanOpenUrl(wifiUrl)) | |
{ | |
// Pre iOS 10 | |
UIApplication.SharedApplication.OpenUrl(wifiUrl); | |
} | |
else | |
{ | |
// iOS 10 | |
using (var nSUrl = new NSUrl("App-Prefs:root=WIFI")) | |
{ | |
UIApplication.SharedApplication.OpenUrl(nSUrl); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Debug.WriteLine(ex.Message); | |
throw new InvalidOperationException("Could not open Wifi Settings"); | |
} | |
} | |
} | |
} | |
// UWP | |
using Project.UWP.Helpers; | |
using Xamarin.Forms; | |
[assembly: Dependency(typeof(OpenWifiSettingsHelper))] | |
namespace Project.UWP.Helpers | |
{ | |
using System; | |
using WerDaXamarin.Interfaces; | |
using Windows.System.Profile; | |
/// <summary> | |
/// Implements <see cref="OpenWifiSettingsHelper"/> to open wifi settings on UWP | |
/// </summary> | |
public class OpenWifiSettingsHelper : IOpenWifiSettings | |
{ | |
/// <summary> | |
/// Opens wifi settings on this Windows device | |
/// </summary> | |
public void OpenWifiSettings() | |
{ | |
// Open the about page when on Windows 10 Mobile, because you find the MAC address there. | |
var success = AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Mobile" ? | |
Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:about")).GetAwaiter().GetResult() : | |
Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:network-wifi")).GetAwaiter().GetResult(); | |
if (!success) | |
{ | |
throw new InvalidOperationException("Could not open Wifi settings"); | |
} | |
} | |
} | |
} | |
// Usage in PCL | |
private static void ExecuteOpenWifiSettingsCommand() | |
{ | |
try | |
{ | |
DependencyService.Get<IOpenWifiSettings>().OpenWifiSettings(); | |
} | |
catch (InvalidOperationException invopex) | |
{ | |
// Catch the exception | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment