0

需要一些有关新的 Capture SDK for Android 的帮助。我在活动中使用以下代码。

Capture.builder(getApplicationContext())
                    .enableLogging(BuildConfig.DEBUG)
                    .appKey(appKey)
                    .build();
            captureClient = new CaptureClient(appKey);
            captureClient.setListener(this);
            captureClient.connect(connectionCallback);

我在顶级活动中使用了我的正确 appKey 和 appid。

我想询问电池电量。我该怎么做呢?我已经实现了一个监听器,但不确定如何发出请求。

好的,接下来我有这个要求:

if (mDevice != null) {
    mDevice.getBatteryLevel(propertyCallback);
}

PropertyCallback propertyCallback = new PropertyCallback() {
        @Override
        public void onComplete(@Nullable CaptureError captureError, @Nullable Property property) {
            if (captureError != null) {
                Log.d("onComplete", String.format("capture error %s", captureError.getMessage()));
            } else {
                if (property != null) {
                    Log.d("onComplete", String.format("property set %d", property.getId()));
                    Preference socketCharge = (Preference) findPreference("bt_scanner_battery_charge");
                    if (socketCharge != null) {
                        try {
                            int i = property.getInt();
                            socketCharge.setSummary(String.format("Current charge level: %d%%", i));
                        } catch (Exception e) {
                            Log.e("SocketChargeError", "" + e.getMessage());
                        }
                    }
                }
            }
        }
    };

上面的代码进行了往返并调用了属性回调,但属性中包含的信息不是百分比。

即使我的班级实现了它并订阅了“事物”,这也是侦听器(从未被调用)。

@Override public void onBatteryLevelChanged(int i) {

4

1 回答 1

0

好的,在与 Socket Mobile 的支持人员交谈后,一位软件工程师告诉我电池电量信息是如何打包到属性响应中的。在这里……现在只需将其格式化以供显示!

Code:
    int value = property.getInt();
    int current = value >>> 8 & 0xFF;
    int min = value >>> 16 & 0xFF;
    int max = value >>> 24 & 0xFF;
    Double batteryPercent = current * 100.0 / (max - min);

于 2019-08-14T22:12:19.443 回答