Understanding Device Information in .NET MAUI
.NET MAUI provides powerful capabilities to access and utilize device-specific information. This is crucial for creating adaptive and context-aware user experiences across various platforms like iOS, Android, macOS, and Windows.
This tutorial will guide you through accessing essential device details, such as:
- Device manufacturer and model
- Operating system version
- Screen dimensions and density
- Battery status
- Connectivity information
Important: Permissions might be required for certain device information, especially on mobile platforms. Always consult platform-specific documentation for managing permissions.
Accessing Basic Device Properties
The Microsoft.Maui.Devices namespace offers a straightforward way to get fundamental device information. The DeviceInfo class is your primary entry point.
Device Model and Manufacturer
You can easily retrieve the name of the device manufacturer and its model:
C#
string manufacturer = DeviceInfo.Current.Manufacturer;
string model = DeviceInfo.Current.Model;
Console.WriteLine($"Manufacturer: {manufacturer}");
Console.WriteLine($"Model: {model}");
Operating System Version
Get the version of the operating system running on the device:
C#
string version = DeviceInfo.Current.VersionString;
int versionNumber = DeviceInfo.Current.Version; // Numeric representation
Console.WriteLine($"OS Version: {version}.{versionNumber}");
Platform Type
Identify the current operating system:
C#
DevicePlatform platform = DeviceInfo.Current.Platform;
switch (platform)
{
case DevicePlatform.Android:
Console.WriteLine("Running on Android.");
break;
case DevicePlatform.iOS:
Console.WriteLine("Running on iOS.");
break;
case DevicePlatform.macOS:
Console.WriteLine("Running on macOS.");
break;
case DevicePlatform.WinUI:
Console.WriteLine("Running on Windows.");
break;
default:
Console.WriteLine("Running on an unknown platform.");
break;
}
Working with Screen and Display Information
The DeviceDisplay class provides details about the device's screen and display properties.
Screen Dimensions
Obtain the screen width and height in device-independent units:
C#
double width = DeviceDisplay.Current.MainDisplayInfo.Width;
double height = DeviceDisplay.Current.MainDisplayInfo.Height;
double density = DeviceDisplay.Current.MainDisplayInfo.Density; // Pixels per inch
Console.WriteLine($"Screen Width: {width} units");
Console.WriteLine($"Screen Height: {height} units");
Console.WriteLine($"Screen Density: {density} PPI");
Screen Orientation
Check the current screen orientation:
C#
DisplayOrientation orientation = DeviceDisplay.Current.MainDisplayInfo.Orientation;
if (orientation == DisplayOrientation.Landscape)
{
Console.WriteLine("Screen is in Landscape mode.");
}
else
{
Console.WriteLine("Screen is in Portrait mode.");
}
Idiom
Determine the device idiom (phone, tablet, desktop, etc.):
C#
DeviceIdiom idiom = DeviceInfo.Current.Idiom;
switch (idiom)
{
case DeviceIdiom.Phone:
Console.WriteLine("This is a phone.");
break;
case DeviceIdiom.Tablet:
Console.WriteLine("This is a tablet.");
break;
case DeviceIdiom.Desktop:
Console.WriteLine("This is a desktop device.");
break;
default:
Console.WriteLine("Unknown device idiom.");
break;
}
Monitoring Battery Status
.NET MAUI allows you to monitor the device's battery status, including charge level and whether it's charging.
Battery Information
The Battery class provides access to battery-related events and properties.
C#
BatteryState batteryState = Battery.Current.State;
BatteryPowerSource powerSource = Battery.Current.PowerSource;
float batteryLevel = Battery.Current.Level; // 0.0 to 1.0
Console.WriteLine($"Battery State: {batteryState}");
Console.WriteLine($"Power Source: {powerSource}");
Console.WriteLine($"Battery Level: {batteryLevel * 100:F1}%");
// Subscribe to battery changes
Battery.Current.BatteryChanged += (sender, args) =>
{
Console.WriteLine($"Battery Level Changed: {args.Level * 100:F1}%");
Console.WriteLine($"Battery State Changed: {args.State}");
Console.WriteLine($"Power Source Changed: {args.PowerSource}");
};
Note: Accessing battery information may require specific permissions on some platforms.
Getting Network Connectivity Information
The Connectivity class enables you to check the network connectivity status of the device.
Network Access
Check if the device has access to the internet:
C#
bool hasInternet = Connectivity.Current.NetworkAccess == NetworkAccess.Internet;
if (hasInternet)
{
Console.WriteLine("Device is connected to the Internet.");
}
else
{
Console.WriteLine("Device is not connected to the Internet.");
}
Current Connection Type
Determine the current type of network connection:
C#
NetworkAccess currentAccess = Connectivity.Current.NetworkAccess;
foreach (var item in Connectivity.Current.LocalNetworkInformations)
{
Console.WriteLine($"Network Type: {item.AccessKind}, IP Address: {item.IPAddress}");
}
Connectivity Changes
Subscribe to events to be notified of network connectivity changes:
C#
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
{
Console.WriteLine($"Connectivity Changed: {e.NetworkAccess}");
if (e.NetworkAccess == NetworkAccess.Internet)
{
Console.WriteLine("Internet connection restored.");
}
else
{
Console.WriteLine("Internet connection lost.");
}
}
Further Exploration
This tutorial covered the basics of accessing device information in .NET MAUI. For more advanced scenarios and detailed API references, please refer to the official documentation:
Official .NET MAUI Device Information API Reference