100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Accessing Device Features

Explore the Microsoft.Maui.Essentials device APIs for geolocation, camera, sensors, and connectivity in .NET MAUI.

Navigation & PlatformIntermediate10 min readJul 10, 2026
Analogies

The Essentials APIs

What used to be the separate Xamarin.Essentials package is now built directly into .NET MAUI as the Microsoft.Maui.Devices, Microsoft.Maui.Storage, Microsoft.Maui.Media, and Microsoft.Maui.Sensors namespaces, giving shared C# code a single cross-platform API surface for things like geolocation, the camera, the accelerometer, and battery status. Under the hood each API is backed by a platform-specific implementation, but the calling code — for example await Geolocation.Default.GetLocationAsync() — is identical whether running on Android, iOS, or Windows.

🏏

Cricket analogy: This is like the DRS (Decision Review System) presenting the same on-screen interface to broadcasters worldwide, while the underlying Hawk-Eye and Snicko hardware differs by stadium and vendor behind the scenes.

Geolocation and Sensors

Geolocation.Default.GetLocationAsync(GeolocationRequest) returns a single Location reading with configurable accuracy and timeout, while Geolocation.Default.GetLastKnownLocationAsync() returns a cached position instantly without triggering the GPS hardware, useful when you need a rough starting point before a precise fix arrives. Sensors like Accelerometer, Gyroscope, and Compass follow a subscribe/event pattern instead — call Start(SensorSpeed) and handle the ReadingChanged event, then always call Stop() when you're done to avoid draining battery.

🏏

Cricket analogy: GetLastKnownLocationAsync is like consulting yesterday's pitch report for a quick read before the toss, while GetLocationAsync is like waiting for the ground staff's fresh, precise pitch inspection just before play starts.

csharp
// One-shot precise location
var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
Location location = await Geolocation.Default.GetLocationAsync(request);

// Continuous accelerometer readings
Accelerometer.Default.ReadingChanged += (s, e) =>
{
    var data = e.Reading;
    Console.WriteLine($"X:{data.Acceleration.X} Y:{data.Acceleration.Y} Z:{data.Acceleration.Z}");
};
Accelerometer.Default.Start(SensorSpeed.UI);

// later, when the page disappears
Accelerometer.Default.Stop();

Camera, Media, and Connectivity

MediaPicker.Default.CapturePhotoAsync() launches the native camera UI and returns a FileResult you can stream into a Stream or save locally, while MediaPicker.Default.PickPhotoAsync() opens the gallery picker instead — both return null if the user cancels rather than throwing. Connectivity.Current.NetworkAccess reports whether the device currently has internet access, and subscribing to Connectivity.ConnectivityChanged lets the app react live to Wi-Fi drops or switching to cellular data.

🏏

Cricket analogy: CapturePhotoAsync is like calling for a fresh third-umpire camera angle right now, while PickPhotoAsync is like pulling an existing replay clip from the broadcast archive instead of shooting new footage.

Every Essentials API that touches hardware — Geolocation, MediaPicker, Sensors — requires the matching permission to be granted first (see the Permissions and Lifecycle topic); calling these APIs without checking permission first throws a PermissionException on most platforms rather than silently failing.

Forgetting to call Stop() on a sensor like Accelerometer or Gyroscope keeps it actively sampling in the background even after the page has been navigated away from, which drains battery and can throw if the subscribing page's handler references disposed UI elements — always unsubscribe and Stop() in OnDisappearing.

  • Device APIs live in Microsoft.Maui.Devices, .Storage, .Media, and .Sensors, unified from the old Xamarin.Essentials package.
  • Geolocation.GetLocationAsync gets a fresh fix; GetLastKnownLocationAsync returns a cached position instantly.
  • Sensors use a Start/Stop plus ReadingChanged event subscription pattern rather than a single async call.
  • MediaPicker.CapturePhotoAsync opens the camera; PickPhotoAsync opens the gallery, both returning null on cancel.
  • Connectivity.Current.NetworkAccess and ConnectivityChanged let the app detect and react to network state.
  • Hardware APIs require the matching runtime permission or they throw a PermissionException.
  • Always Stop() sensors in OnDisappearing to avoid battery drain and stale-handler crashes.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#NETMAUIStudyNotes#AccessingDeviceFeatures#Accessing#Device#Features#Essentials#StudyNotes#SkillVeris#ExamPrep