WVR_GetTrackerBatteryLevel

WVR_EXPORT float WVR_GetTrackerBatteryLevel(WVR_TrackerId trackerId)

Function to get the current battery level of the tracker device.

Return
The current battery level of the tracker device. Valid interval: [0.0, 1.0]. Negative value means the tracker device does not support this feature or is not connected.
Version
API Level 8
Parameters
  • trackerId: Indicates what id of the tracker device. (Refer to WVR_TrackerId.)

How to use

Here is an example for the function:

  1. Header & global variables.
#include <wvr/wvr.h> // for enum WVR_SupportedFeature
#include <wvr/wvr_events.h>
#include <wvr/wvr_tracker.h>
#include <wvr/wvr_types.h>

bool gRunningTR = false;

// Global variables for WVR_TrackerId_0
WVR_TrackerCapabilities_t gCapabilitiesId0; // device capabilities
float gBatteryLvId0 = 0.0f;
  1. Start tracker feature first if it has not been started yet. (See also WVR Supported Features)
    • And (Timing 1 - After starting tracker feature successfully) use function WVR_GetTrackerCapabilities to get tracker capabilities to check whether battery level feature (capability supportsBatteryLevel) is supported or not.
    • Then use function WVR_GetTrackerBatteryLevel to first get battery level of tracker device.
if (WVR_GetSupportedFeatures() & WVR_SupportedFeature_Tracker) { // the tracker feature is supported or not
    WVR_Result res = WVR_StartTracker();
    if (res == WVR_Success) gRunningTR = true;
}

if (gRunningTR && WVR_IsTrackerConnected(WVR_TrackerId_0)) {
    // Timing 1: Update the tracker capabilities if the tracker device has been connected before.
    WVR_Result ret = WVR_GetTrackerCapabilities(WVR_TrackerId_0, &gCapabilitiesId0);
    // Needs gCapabilitiesId0.supportsBatteryLevel

    // First update battery level if the tracker device has been connected before.
    if (gCapabilitiesId0.supportsBatteryLevel) {
         gBatteryLvId0 = WVR_GetTrackerBatteryLevel(WVR_TrackerId_0);
    }
}
  1. (Timing 2 - When receiving event WVR_EventType_TrackerConnected) Use function WVR_GetTrackerCapabilities to get tracker capabilities to check whether battery level feature (capability supportsBatteryLevel) is supported or not.
    • Then use function WVR_GetTrackerBatteryLevel to first get battery level of tracker device.
    • When recieving update event WVR_EventType_TrackerBatteryLevelUpdate, battery level should be kept updating.
void ProcessEvent() {
    WVR_Event_t event;
    while(WVR_PollEventQueue(&event)) {
        switch (event.common.type) {
            /* Timing 2: Get tracker capabilities when tracker device has just been connected.*/
            case WVR_EventType_TrackerConnected:
                {
                    if (gRunningTR && event.tracker.trackerId == WVR_TrackerId_0) {
                        WVR_Result ret = WVR_GetTrackerCapabilities(event.tracker.trackerId, &gCapabilitiesId0);
                        LOGI("Tracker capabilities of TrackerId_%d: ret: %d, capabilities: %d, %d, %d, %d, %d, %d",
                            event.tracker.trackerId, ret,
                            gCapabilitiesId0.supportsOrientationTracking,
                            gCapabilitiesId0.supportsPositionTracking,
                            gCapabilitiesId0.supportsInputDevice,
                            gCapabilitiesId0.supportsHapticVibration,
                            gCapabilitiesId0.supportsBatteryLevel,
                            gCapabilitiesId0.supportsExtendedData);

                        // First update battery level when tracker device has just been connected.
                        if (gCapabilitiesId0.supportsBatteryLevel) {
                            gBatteryLvId0 = WVR_GetTrackerBatteryLevel(event.tracker.trackerId);
                        }
                    }
                }
                break;
            /* Keep updating battery level regularly when recieving update event. */
            case WVR_EventType_TrackerBatteryLevelUpdate:
                {
                    if (gRunningTR &&
                        event.tracker.trackerId == WVR_TrackerId_0 &&
                        gCapabilitiesId0.supportsBatteryLevel) {
                        gBatteryLvId0 = WVR_GetTrackerBatteryLevel(event.tracker.trackerId);
                    }
                }
                break;
            default:
                break;
        }
    }
}

Note

  1. This function works when capability supportsBatteryLevel of tracker device is true.
  2. If battery level feature is not supported, the returned value will be -1.