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 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:

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)%")
            }
        }
    })
}

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

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.

device.getNotificationsWithCompletionHandler({ (result: SKTResult, notifications: SKTCaptureNotifications?) in
    print("getting notifications returned \(result.rawValue)")
    if result == SKTCaptureErrors.E_NOERROR {
        if var currentNotification = notifications as SKTCaptureNotifications! {
            if !currentNotification.contains(.batteryLevelChange) {
                print("adding the battery level change notification")
                currentNotification.insert(.batteryLevelChange)
                device.setNotifications(currentNotification, withCompletionHandler: { (result: SKTResult) in
                    print("changing notification to battery level change returned \(result.rawValue)")
                })
            } else {
                print("the battery level change notification is already set")
            }
        }
    }
})

The device is a CaptureHelperDevice instance that has been received in the argument of the didNotifyArrivalForDevice delegate.

Notice that we also use this didNotifyArrivalForDevice delegate to ask for the current battery level so it can be displayed right away.

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:

func didChangeBatteryLevel(_ batteryLevel: Int, forDevice device: CaptureHelperDevice){
    print("new battery level: \(SKTHelper.getCurrentLevel(fromBatteryLevel: Int(batteryLevel)))%")
}

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.