3

无论我似乎尝试什么,我目前都无法从 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

如果我的代码是正确的,那么我如何使电池结果得到一个有意义的结果,比如百分比?
如果我离开了,那么我如何取回电池电量、固件版本等信息?

谢谢

大卫

4

3 回答 3

2

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

以下等价于使用SKTBATTERY_GETCURLEVEL

迅速

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);
}
}
于 2016-07-07T21:19:43.733 回答
0

这是我使用的代码。在 appDelegate 中为电池百分比定义了一个变量,当需要 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)")
    }
}
于 2016-07-29T14:54:39.697 回答
0

对于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。在您的示例中,您可能想要处理该问题。

于 2018-09-09T21:35:19.807 回答