Table of Contents

Class NearbyConnectionRequest

Namespace
Plugin.Maui.NearbyConnectivity
Assembly
Plugin.Maui.NearbyConnectivity.dll

An inbound connection request from a nearby device, held on Request while the device reports RequestReceived.

[SuppressMessage("Design", "CA1001:Types that own disposable fields should be disposable", Justification = "The session owns a request's lifetime and disarms its countdown at the single claim that settles accept, decline, and expiry. Making the type IDisposable would put that release on the public surface, where a consumer disposing a request the session still holds would silence an expiry nobody asked to silence — the same reason NearbyConnection exposes CloseAsync rather than IDisposable.")]
public sealed class NearbyConnectionRequest
Inheritance
NearbyConnectionRequest
Inherited Members

Remarks

Answer it with AcceptAsync(CancellationToken) or DeclineAsync(CancellationToken) before ExpiresAt elapses. A request can be answered once, and only while it is outstanding.

Under a consent policy that answers for you, the session answers on the application's behalf, the device never reports RequestReceived, and it moves straight to Connected on DeviceChanged instead.

Properties

Expired

Gets a task that completes when this request stops being answerable: it expired, a connection to the device opened another way, or the session stopped.

public Task Expired { get; }

Property Value

Task

A task that completes when the request is withdrawn. It never faults and never completes for a request that was answered in time. A view races its prompt against it with WhenAny(Task, Task) to dismiss the prompt when the offer lapses; MAUI's own prompts take no CancellationToken, so a task is the shape they can race.

Examples

var prompt = page.DisplayAlertAsync(
    "Connect?", request.RemoteDevice.Presence.DisplayName, "Accept", "Decline");

if (await Task.WhenAny(prompt, request.Expired) != prompt)
{
    return;   // The window closed; the plugin already declined it.
}

await (await prompt ? request.AcceptAsync(ct) : request.DeclineAsync(ct));

ExpiresAt

Gets when this offer expires.

public DateTimeOffset ExpiresAt { get; }

Property Value

DateTimeOffset

The one deadline both sides share: declared by the initiating device as its OfferWindow, sent on the wire with the request, and clamped locally. After it passes, the request is no longer answerable.

Remarks

A fixed point in time, computed once at receipt. Durations travel on the wire rather than timestamps, because the two devices share no clock. This value is in local time, read from the registered TimeProvider: compare it against that provider's GetUtcNow(), not UtcNow.

ExpiresIn

Gets how long is left to answer, or Zero once the window has closed.

public TimeSpan ExpiresIn { get; }

Property Value

TimeSpan

The remaining window, never negative. Recomputed on every read from ExpiresAt, so it is a live value rather than a stale snapshot.

Remarks

A convenience over ExpiresAt for a countdown that ticks on the application's own timer. The deadline stays the value to store, because this one is only true at the instant it is read. Read from the session's injected clock, like every other deadline this library owns, so a test can advance past one virtually.

IsPending

Gets whether this request is still answerable.

public bool IsPending { get; }

Property Value

bool

true until the request is accepted, declined, expires, or the session stops.

Remarks

A snapshot; the offer can expire between reading this and acting on it, which is why AcceptAsync(CancellationToken) reports the authoritative answer by throwing rather than expecting the caller to have checked.

RemoteDevice

Gets the device asking to connect.

public NearbyDevice RemoteDevice { get; }

Property Value

NearbyDevice

The remote device. Its display name is remote-chosen, sanitized input.

Methods

AcceptAsync(CancellationToken)

Accepts the request and completes the handshake.

public Task<NearbyConnection> AcceptAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A token to stop waiting for the handshake.

Returns

Task<NearbyConnection>

The established connection, the same instance Connection returns.

Exceptions

NearbyConnectException

The handshake did not produce a connection. Reason is TimedOut when the offer's deadline passed, whether it expired before this call, or during the handshake, which is the same moment the initiating device gives up on its own ConnectAsync(CancellationToken).

OperationCanceledException

cancellationToken was canceled.

DeclineAsync(CancellationToken)

Declines the request. The remote device observes a declined handshake.

public Task DeclineAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A token to stop waiting for the decline to be sent.

Returns

Task

A task that completes once the decline is signaled to the platform.

Remarks

No-op once the request is no longer pending: an offer that already expired needs no answer, and declining it changes nothing the remote device can still observe.

See Also