Device Sample API

This tutorial shows common operations done on an Android device to gather information. The sample app is divided into several Activities, one per each topic.

Info

The Datalogic Xamarin SDK provides advanced information about the device in the class  SYSTEM , which exposes Wi-Fi type, keyboard type and others as static members. Example usage:

Com.Datalogic.Device.Info.SYSTEM.SdkVersionInt;

Power

To get information about the battery, Xamarin broadcasts the Intent  ActionBatteryChanged which carry information in its extra properties. The intent is fired every time the status of the battery changes and once when you register a receiver, notice that this particular behavior happens because the battery intent is a STICKY one  SendStickyBroadcast .

Intent currentBatteryStatus = RegisterReceiver(null, new IntentFilter(
Android.Content.Intent.ActionBatteryChanged));

Location

The standard Android SDK doesn't allow turning on or off the Location providers (GPS, network, etc...), thus an application must ask to the user to manually do it. Datalogic SDK overcome this limit by providing the class  LocationManager . Here's how to use it:

Com.Datalogic.Device.Location.LocationManager loc = null;
try
{
loc = new Com.Datalogic.Device.Location.LocationManager();
loc.setLocationMode(enable ? LocationMode.SensorsAndNetwork : LocationMode.Off);
}
catch (DeviceException e)
{
Log.Error(this.LocalClassName, "Exception while switching location mode ", e);
}

Nfc

Standard Android SDK doesn't allow to turn on or off the NFC adapter, thus an application must ask to the user to manually do it. Datalogic SDK overcomes this limit providing the class  NfcManager , here how to use it:

NfcManager nfc = new Com.Datalogic.Device.Nfc.NfcManager();
nfc.EnableNfcAdapter(enable);

Notifications

Datalogic SDK allows to control LEDs on the device (*). The standard Android APIs for controlling the notification LED via the notifications system still works, though you are limited to 1 LED, while Datalogic devices may have more LEDs, moreover using Datalogic  LedManager the control is easier.

LedManager led = new LedManager();
led.blinkLed(Led.LedGreenSpot, 1, 500, 500);

*Please notice that not all LEDs on a device can be freely controlled by a user application, as some are reserved to the system.

Touch

The class  TouchManager can be used to lock the touchscreen.

TouchManager tm = new TouchManager();
tm.lockInput(true);
Thread.sleep(2000);
tm.lockInput(false);

Sleep and Wakeup

Datalogic  PowerManager allows to configure the screen off timeout and the wakeup sources of the device.

The  SuspendTimeout can be set with a condition: the device running on battery or the device plugged to a power source, the function  PowerManager.SetSuspendTimeout() has a boolean argument for it.

PowerManager pm = new PowerManager();

pm.setSuspendTimeout(SuspendTimeout.Minutes5, false); // battery
pm.setSuspendTimeout(SuspendTimeout.Never, true); // ext power

Please notice that not all the  WakeupSource s available in the SDK are supported by a device, thus is better to check for the support before enabling/disabling them.

if (pm.isWakeupSupported(WakeupSource.TrigLeft) &&
!pm.isWakeupActive(WakeupSource.TrigLeft)) {
pm.activateWakeup(WakeupSource.TrigLeft);
}

Reset

With the class  PowerManager is possible to perform several type of resets and reboot of the device.

 EnterpriseReset and  FactoryReset reset types clear the configuration of the device setting it to a custom one or to the out-of-the-box one respectively, while  Reset is a software reboot of the device.