SocketScan 在 Swift 中获取电池电量

SocketScan Getting the Battery Level in Swift

无论我怎么尝试,我目前都无法从 iOS/SocketScan API 恢复电池电量。我使用的是 10.3.36 版,这是我目前的代码:

func onDeviceArrival(result: SKTRESULT, device deviceInfo: DeviceInfo!) {
    print("onDeviceArrival:\(deviceInfo.getName())")
scanApiHelper.postGetBattery(deviceInfo, target: self, response: #selector(onGetBatteryInfo))

}

func onGetBatteryInfo(scanObj: ISktScanObject) {

    let result:SKTRESULT = scanObj.Msg().Result()
    print("GetBatteryInfo status:\(result)")

    if (result == ESKT_NOERROR) {
        let batterylevel = scanObj.Property().getUlong()
        print("Battery is:\(batterylevel)")
    } else {
        print("Error GetBatteryInfo status:\(result)")
    }

然而,我得到的值是:

GetBatteryInfo status:0

Battery is:1677741312

如果我的代码是正确的,那么我如何使电池结果返回有意义的结果,例如百分比?
如果我偏离了方向,那么我如何返回电池等信息级别、固件版本等?

谢谢

大卫

编辑:Swift 不支持 SKTBATTERY_GETCURLEVEL。但是,docs 说明电池电量响应包括分别编码在第一、第二和第三字节中的最小、当前和最大电量。

以下等同于使用SKTBATTERY_GETCURLEVEL

Swift

func onGetBatteryInfo(scanObj: ISktScanObject) {

    let result:SKTRESULT = scanObj.Msg().Result()

    if(SKTSUCCESS(result)){

        let batteryInfo = scanObj.Property().getUlong();

        let batteryMin = ((batteryInfo >> 4) & 0xff);
        let batteryCurrent = ((batteryInfo >> 8) & 0xff);
        let batteryMax = ((batteryInfo >> 12) & 0xff);

        let batteryPercentage = batteryCurrent / (batteryMax - batteryMin);
        print("Battery is:\(batteryPercentage)")

        self.setBatteryLevel = batteryPercentage
        self.tableView.reloadData

    } else {

        print("Error GetBatteryInfo status:\(result)")
    }
}

Objective-C

-(void) onGetBatteryInfo:(ISktScanObject*)scanObj {

SKTRESULT result=[[scanObj Msg]Result];
if(SKTSUCCESS(result)){

    long batteryLevel = SKTBATTERY_GETCURLEVEL([[scanObj Property] getUlong]);
    NSLog(@"BatteryInfo %ld", batteryLevel);

    [self setBatteryLevel:batteryLevel];
    [self.tableView reloadData];

} else {

    NSLog(@"Error GetBatteryInfo status: %ld",result);
}
}

这是我使用的代码。在 appDelegate 中为 batteryPercentage 定义了一个变量,当需要 v 值时会读取该变量。该值由计时器每 120 秒更新一次,这样可以在级别下降等时发生操作。

 func onBatteryLevel (scanObj: ISktScanObject) {
    let result: SKTRESULT = scanObj.Msg().Result()
    if (SKTRESULT(result) > -1) {
        let property: ISktScanProperty = scanObj.Property()
        var batteryLevel = property.getUlong()

        #if arch(x86_64) || arch(arm64)
            batteryLevel = (batteryLevel<<(48))>>(56)
            #else
            batteryLevel = (batteryLevel<<(48-32))>>(56-32)
        #endif
        batteryPercentage = Int(batteryLevel)
    } else {
        debug ("data error \(result)")
    }
}

For Swift 4 我刚遇到这个问题,想出了以下解决方案。

var lastDeviceConnected : CaptureHelperDevice? {

    didSet {

        guard let lastDevice = self.lastDeviceConnected else { return }
        lastDevice.getBatteryLevelWithCompletionHandler { result, batteryLevel in

            guard result == SKTResult.E_NOERROR, let batteryLevel = batteryLevel else { return }

            let minimum = SKTHelper.getMinimumLevel(fromBatteryLevel: Int(batteryLevel))
            let maximum = SKTHelper.getMaximumLevel(fromBatteryLevel: Int(batteryLevel))
            let current = SKTHelper.getCurrentLevel(fromBatteryLevel: Int(batteryLevel))

            print("minimum: \(minimum)")
            print("maximum: \(maximum)")
            print("current: \(current)")

            // current is out battery level in %
            // minimum and maximum could for example be used for 
            // for a slider or something that visually presents
            // the battery status
        }
    }
}

在我的示例中,我处理可能没有设备或可能未按预期检索电池状态的情况。我只是guard / return。在您的示例中,您可能希望处理该问题。