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 CaptureSDK. Additionally, many Socket Mobile 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:

  • Swift
  • ObjectiveC
if let device = lastDevice as CaptureHelperDevice! {
    device.getBatteryLevelWithCompletionHandler({ (result :SKTResult, battery: UInt?) in
        print("getting the battery level returned \(result.rawValue)")
        if result == SKTCaptureErrors.E_NOERROR {
            if let level = battery as UInt! {
                let percentage = SKTHelper.getCurrentLevel(fromBatteryLevel: Int(level))
                print("battery level: \(percentage)%")
            }
        }
    })
}
SKTCaptureHelperDevice* device = _lastDeviceConnected;
[device getBatteryLevelWithCompletionHandler:^(SKTResult result, NSInteger levelInPercentage) {
    NSLog(@"Getting the device battery level returns %ld", result);
    if(result == SKTCaptureE_NOERROR){
        [self displayBatteryLevel:levelInPercentage forDevice:device];
    }
}];

#pragma mark - Utility methods
-(void)displayBatteryLevel:(NSInteger)batteryLevelInPercentage forDevice:(SKTCaptureHelperDevice*)device{
    dispatch_async(dispatch_get_main_queue(), ^{
        _statusLabel.text = [NSString stringWithFormat:@"%@ Battery: %ld%%",device.friendlyName, (long)batteryLevelInPercentage];
    });
}

The lastDevice is a CaptureHelperDevice instance that has been set in the didNotifyArrivalForDevice delegate.

Set up the Battery Level Change Notification

In order to receive the battery level change notifications, we need to derive the class from the CaptureHelperDevicePowerDelegate protocol and provide a handler for the didChangeBatteryLevel delegate:

  • Swift
  • ObjectiveC
func didChangeBatteryLevel(_ batteryLevel: Int, forDevice device: CaptureHelperDevice){
    print("new battery level: \(SKTHelper.getCurrentLevel(fromBatteryLevel: Int(batteryLevel)))%")
}
-(void)didChangeBatteryLevel:(NSInteger) batteryPercentage forDevice:(SKTCaptureHelperDevice*) device withResult:(SKTResult)result{
    if(result == SKTCaptureE_NOERROR){
        NSLog(@"Receive a battery level change notification: %ld%%", batteryPercentage);
        [self displayBatteryLevel:batteryPercentage forDevice:device];
    }
}

Note

The CaptureHelperDevicePowerDelegate protocol also has a didChangePowerState delegate that could be left empty if not required by the application.

Power State Change and other Events

The same principle can be followed for the other events of the device, such as the didChangePowerState delegate which notifies the application each time the scanner is plugged into a power source, or unplugged or dropped in the charger cradle.

Other events such as buttons state can be tracked by deriving from the CaptureHelperDeviceButtonsDelegate protocol and by implementing its didChangeButtonsState delegate.