Accessing Device Capabilities in .NET MAUI

.NET MAUI gives you access to native device features like GPS, camera, battery status, accelerometer, and more using a unified API across platforms.


MAUI Essentials: Unified Device APIs

MAUI integrates .NET MAUI Essentials, a library that provides cross-platform APIs for native device functionality such as:

  • 📍 Device Info & Battery
  • 📷 Camera & Media Picker
  • 📡 Connectivity & Network Access
  • 📦 Secure Storage
  • 🧭 Geolocation & Sensors

Getting Device Info

using Microsoft.Maui.Devices;

string model = DeviceInfo.Model;
string manufacturer = DeviceInfo.Manufacturer;
string version = DeviceInfo.VersionString;

Accessing Geolocation

Get the current GPS coordinates using Geolocation:

using Microsoft.Maui.Essentials;

var location = await Geolocation.GetLastKnownLocationAsync();
if (location != null)
{
    Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}");
}

Picking Photos from Gallery

Use MediaPicker to pick or take a photo:

var file = await MediaPicker.PickPhotoAsync();
if (file != null)
{
    var stream = await file.OpenReadAsync();
    image.Source = ImageSource.FromStream(() => stream);
}

Secure Storage Example

Safely store key-value pairs using SecureStorage:

await SecureStorage.SetAsync("user_token", "123456");

string token = await SecureStorage.GetAsync("user_token");

Handling Permissions

Always check for permissions before accessing sensitive features:

var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
if (status != PermissionStatus.Granted)
{
    status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
}

With .NET MAUI Essentials, you can create powerful, device-aware applications using a unified C# codebase. This concludes our beginner journey through the .NET ecosystem!

You're now ready to build, test, and deploy cross-platform .NET apps — from web APIs to mobile and desktop UIs!