Power Management¶
It is important to know the power state of the scanner as it is for the host to make sure it does not need to be recharged during the time the scanner is needed and the operation has a chance to change batteries or charge the scanner.
The application can check the battery level of the scanner using Capture SDK. Additionally, many SocketMobile scanners support battery level change notification.
Following are some examples on how to configure the scanner for these features.
Reading the Battery Level¶
Here is example code showing how to request the scanner battery level information:
// ask for the Battery Level
CaptureHelperDevice.BatteryLevelResult resultBattery = await Device.GetBatteryLevelAsync();
labelBatteryLevel.Text = resultBattery.Percentage;
In this example, the battery percentage is displayed in a UI Text Label.
The Device is a CaptureHelperDevice
object that has been received in the
argument of the DeviceArrival
event.
Set up the Battery Level Change notification¶
Setting up the scanner to make it send a notification each time the battery level changes is a 2 step process. Since this setting is persistent in the scanner, the first time the scanner connects to the application we query the notification setting, if the battery level change notification is not activated then we activate it. The initial battery level needs to be read at this time because it could take a long time before the first battery level change notification is received.
// ask for the notifications
CaptureHelperDevice.NotificationsResult resultNotifications = await Device.GetNotificationsAsync();
checkBoxBatteryLevel.Checked = resultNotifications.Notifications.BatteryLevel;
The Device is a CaptureHelperDevice
object that has been received in the
argument of the DeviceArrival
event.
Since we need to check only once if a particular scanner has the battery level change notification activated, a good place for doing this is in the handler of the device arrival. If the notification is not already set, it can be set right there. Here is an example of such code:
First install the device arrival handler when initializing CaptureHelper in the application:
// initialize CaptureHelper
capture = new CaptureHelper();
capture.ContextForEvents = WindowsFormsSynchronizationContext.Current;
capture.DeviceArrival += CaptureDeviceArrival;
capture.DeviceRemoval += CaptureDeviceRemoval;
...
Then the device arrival event handler could look something like this:
async void CaptureDeviceArrival(object sender, CaptureHelper.DeviceArgs e)
{
CaptureHelperDevice Device = e.CaptureDevice;
CaptureHelperDevice.NotificationsResult resultNotifications = await Device.GetNotificationsAsync();
if(resultNotifications.isSuccessful()){
// the battery level change notification is not set... do it now...
if(!resultNotifications.Notifications.BatteryLevel){
resultNotifications.Notifications.BatteryLevel = true;
CaptureHelper.AsyncResult result = await Device.SetNotificationsAsync(resultNotifications.Notifications);
if(!result.IsSuccessful()){
// display an error message...
}
}
}
else{
// display an error message...
}
// ask for the Battery Level
CaptureHelperDevice.BatteryLevelResult resultBattery = await Device.GetBatteryLevelAsync();
labelBatteryLevel.Text = resultBattery.Percentage;
}
Notice that we also use this DeviceArrival
event handler to ask for the
current battery level so it can be displayed right away.
Note
Since the DeviceArrival
event handler is updating the UI, it’s
important to specify the UI context for events by passing the current UI context
to the ContextForEvents
property of CaptureHelper object:
...
// initialize CaptureHelper
capture = new CaptureHelper();
capture.ContextForEvents = WindowsFormsSynchronizationContext.Current;
...
So now that the battery level notification has been setup, we need to install a handler for the battery notification event:
Device.DeviceBatteryLevel += OnDeviceBatteryLevel;
This time we can use the CaptureHelperDevice
object to add the
DeviceBatteryLevel
event handler. This could also be done in the
DeviceArrival
event handler. Note that this event is also available in
the CaptureHelper
object and it can be setup during the initialization of
this object. In both cases, the arguments of this event contain the device from
which it has been fired along with the battery level information.
Such event handler might look like this:
void OnDeviceBatteryLevel(object sender, CaptureHelper.BatteryLevelArgs e)
{
string percentage = CaptureHelper.ConvertBatteryLevelInPercentage(e.MinLevel, e.MaxLevel, e.CurrentLevel);
labelBatteryLevel.Text = percentage;
}
Power State Change and other events¶
The same principle can be followed for the other events of the device, such as
the DevicePowerState
event which can notify the application each time the scanner is
plugged into a power source, or unplugged or dropped in the charger cradle.