Search code examples
androidsocketmobilecapture-sdk

Capture SDK request battery level not working


Need some help with the new Capture SDK for Android. I'm using the following code on an activity.

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

I'm using my proper appKey with appid at the top level activity.

I would like to request battery level. How do I do this? I've implemented a listener but am not sure how to make the request.

Ok next I have this as the request:

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());
                        }
                    }
                }
            }
        }
    };

The code above does the round trip and the property callback is called but the information contained in the property is not the percentage.

This is the listener (which is never called) even though my class implements it and subscribes to 'things'.

@Override public void onBatteryLevelChanged(int i) {


Solution

  • Ok, after talking to support at Socket Mobile a software engineer informed me how the battery level information was packed into the property response. Here it is...now it's just a matter of formatting it for display!

    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);