> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Handling a device Presence change

> Code samples for subscribing to XBOX Live device Presence change events and adding or removing handlers when a user logs on or off a device.

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

This topic covers the following:

* [Subscribing to a device Presence change​](#stadpc)
* [Unsubscribing from a device Presence change​](#ufadpc)
* [Adding a device Presence change handler​](#aadpch)
* [Removing a device Presence change handler](#radpch)

<a id="stadpc" />

## Subscribing to a device Presence change​

#### Flat C API

```cpp theme={null}
uint64_t xuid{ 123 };

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

<a id="ufadpc" />

## Unsubscribing from a device Presence change​

#### Flat C API

```cpp theme={null}
HRESULT hr = XblPresenceUnsubscribeFromDevicePresenceChange(
    xboxLiveContext,
    state.devicePresenceChangeSubscription
);

state.devicePresenceChangeSubscription = nullptr;
```

<a id="aadpch" />

## Adding a device Presence change handler​

#### Flat C API

```cpp theme={null}
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](/reference/live/xsapi-c/presence_c/enums/xblpresencedevicetype).

<a id="radpch" />

## Removing a device Presence change handler

#### Flat C API

```cpp theme={null}
HRESULT hr = XblPresenceRemoveDevicePresenceChangedHandler(
    xboxLiveContext,
    state.devicePresenceChangedHandlerToken
);

state.devicePresenceChangedHandlerToken = 0;
```


## Related topics

- [Rich Presence example code](/services/xbox-services/community/presence/how-to/live-presence-howto-nav.md)
- [How to](/services/xbox-services/community/presence/how-to/index.md)
- [Handling a title Presence change](/services/xbox-services/community/presence/how-to/live-handling-a-title-presence-change.md)
- [XblPresenceDevicePresenceChangedHandler](/reference/live/xsapi-c/presence_c/functions/xblpresencedevicepresencechangedhandler.md)
- [XblPresenceRemoveDevicePresenceChangedHandler](/reference/live/xsapi-c/presence_c/functions/xblpresenceremovedevicepresencechangedhandler.md)
