Class NearbySessionState
- Namespace
- Plugin.Maui.NearbyConnectivity
- Assembly
- Plugin.Maui.NearbyConnectivity.dll
A bindable view of "is nearby working right now": whether this device is advertising and discovering, whether anything blocks it, and why either activity last stopped.
public sealed class NearbySessionState : INotifyPropertyChanged, IDisposable
- Inheritance
-
NearbySessionState
- Implements
- Inherited Members
Examples
// Bind IsReady, Availability, IsAdvertising, IsDiscovering, DiscoveryStop.Error.
public NearbySessionState Status { get; } = new(nearby, dispatcher);
public void Dispose() => Status.Dispose();
Remarks
Merges three events. AvailabilityChanged carries radios and permissions; Changed on the advertiser and the discoverer carries a fault that ended a scan. A label bound to availability alone shows "Ready" while discovery is dead, because a platform fault reaches only the activity.
The same shape as NearbyDeviceCollection. This is a bindable façade over the session, constructed with the same dispatcher, seeded from the session's properties, and disposed by whoever owns it. The service itself stays free of user-interface affinity.
Every property is raised on the dispatcher's thread and should be read there, exactly as with the device collection. Constructed on that thread, every property already holds the session's current value when the constructor returns.
Constructors
NearbySessionState(INearbyConnectivity, IDispatcher?)
Initializes a new instance of the NearbySessionState class and begins watching.
public NearbySessionState(INearbyConnectivity nearby, IDispatcher? dispatcher = null)
Parameters
nearbyINearbyConnectivityThe session to watch.
dispatcherIDispatcherWhere property changes are raised. null uses the current thread's dispatcher, so a view model constructed on the UI thread can omit it.
Remarks
Dispatch(Action)'s return value is ignored. A refused dispatch means the dispatcher is tearing down, when there is no bound view left to update.
Exceptions
- ArgumentNullException
nearbyis null.- InvalidOperationException
dispatcheris null and the current thread has none.
Properties
AdvertisingStop
Gets how advertising last stopped, or null while it runs.
public NearbyActivityStop? AdvertisingStop { get; }
Property Value
- NearbyActivityStop
LastStop of Advertiser: null while advertising runs, and until it first stops.
Remarks
A Faulted stop is the one worth showing: the platform ended advertising that the application did not stop, Error says why, and Availability may still read as ready. Starting advertising again clears it.
Availability
Gets what currently blocks nearby connectivity, or Available when nothing does.
public NearbyAvailability Availability { get; }
Property Value
Remarks
On Android this moves when the user turns a radio on or off, and is re-read when the app returns to the foreground, so a permission granted in Settings is reflected on return.
DiscoveryStop
Gets how discovery last stopped, or null while it runs.
public NearbyActivityStop? DiscoveryStop { get; }
Property Value
- NearbyActivityStop
LastStop of Discoverer: null while discovery runs, and until it first stops.
Remarks
A Faulted stop is the one worth showing: the platform ended a scan that the application did not stop, Error says why, and Availability may still read as ready. Starting discovery again clears it.
IsAdvertising
Gets whether this device is currently advertising.
public bool IsAdvertising { get; }
Property Value
IsDiscovering
Gets whether this device is currently discovering.
public bool IsDiscovering { get; }
Property Value
IsReady
Gets whether advertising and discovery can start right now.
public bool IsReady { get; }
Property Value
- bool
true when nothing blocks a start. WifiDisabled counts as ready: it slows a connection rather than preventing one, and the start methods proceed on it.
Methods
Dispose()
Stops following the session and unsubscribes from its events. Idempotent.
public void Dispose()
Remarks
Whoever constructed this owns disposing it, exactly as with NearbyDeviceCollection.
Events
PropertyChanged
Occurs when a property value changes.
public event PropertyChangedEventHandler? PropertyChanged