Handling a device Presence change

This topic provides example code for handling a device Presence change.

This topic covers the following:

Subscribing to a device Presence change​

Flat C API

uint64_t xuid{ 123 };

HRESULT hr = XblPresenceSubscribeToDevicePresenceChange(
    xboxLiveContext,
    xuid,
    &state.devicePresenceChangeSubscription
);

Unsubscribing from a device Presence change​

Flat C API

HRESULT hr = XblPresenceUnsubscribeFromDevicePresenceChange(
    xboxLiveContext,
    state.devicePresenceChangeSubscription
);

state.devicePresenceChangeSubscription = nullptr;

Adding a device Presence change handler​

Flat C API

state.devicePresenceChangedHandlerToken = XblPresenceAddDevicePresenceChangedHandler(
    xboxLiveContext,
    [](void* context, uint64_t xuid, XblPresenceDeviceType deviceType, bool isUserLoggedOnDevice)
    {
        UNREFERENCED_PARAMETER(context);
        LogToFile("Device presence change notification received:");
        LogToFile("Xuid = %u, deviceType = %u, isUserLoggedOnDevice = %u", xuid, deviceType, isUserLoggedOnDevice);
    },
    nullptr
);

For more information, see XblPresenceDeviceType.

Removing a device Presence change handler

Flat C API

HRESULT hr = XblPresenceRemoveDevicePresenceChangedHandler(
    xboxLiveContext,
    state.devicePresenceChangedHandlerToken
);

state.devicePresenceChangedHandlerToken = 0;