PeerFinder.Role Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the role of the app in peer-to-peer connections.
public:
static property PeerRole Role { PeerRole get(); void set(PeerRole value); };
static PeerRole Role();
static void Role(PeerRole value);
public static PeerRole Role { get; set; }
var peerRole = PeerFinder.role;
PeerFinder.role = peerRole;
Public Shared Property Role As PeerRole
Property Value
The role of the app in peer-to-peer connections.
Windows requirements
App capabilities |
ID_CAP_NETWORKING [Windows Phone]
ID_CAP_NETWORKING [Windows Phone]
proximity
ID_CAP_PROXIMITY [Windows Phone]
|
Examples
// Default to the peer role.
Windows.Networking.Proximity.PeerRole appRole = Windows.Networking.Proximity.PeerRole.Peer;
bool launchedByTap = false;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
LaunchActivatedEventArgs launchArgs = e.Parameter as LaunchActivatedEventArgs;
if (launchArgs.Arguments.Contains("Windows.Networking.Proximity.PeerFinder:StreamSocket"))
{
launchedByTap = true;
if (launchArgs.Arguments.Contains("Role=Host"))
{
appRole = Windows.Networking.Proximity.PeerRole.Host;
}
else if (launchArgs.Arguments.Contains("Role=Client"))
{
appRole = Windows.Networking.Proximity.PeerRole.Client;
}
else
{
appRole = Windows.Networking.Proximity.PeerRole.Peer;
}
}
}
bool started = false;
// Click event for "Advertise" button.
void AdvertiseForPeers(object sender, RoutedEventArgs e)
{
if (!started)
{
Windows.Networking.Proximity.PeerFinder.DisplayName = DisplayNameTextBox.Text;
if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) ==
Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered)
{
Windows.Networking.Proximity.PeerFinder.TriggeredConnectionStateChanged +=
TriggeredConnectionStateChanged;
WriteMessageText("You can tap to connect a peer device that is " +
"also advertising for a connection.\n");
}
else
{
WriteMessageText("Tap to connect is not supported.\n");
}
if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
Windows.Networking.Proximity.PeerDiscoveryTypes.Browse) !=
Windows.Networking.Proximity.PeerDiscoveryTypes.Browse)
{
WriteMessageText("Peer discovery using Wi-Fi Direct is not supported.\n");
}
// Set the peer role selected by the user.
if (launchedByTap)
{
Windows.Networking.Proximity.PeerFinder.Role = appRole;
}
else
{
switch (GetRoleFromUser())
{
case "Peer":
Windows.Networking.Proximity.PeerFinder.Role =
Windows.Networking.Proximity.PeerRole.Peer;
break;
case "Host":
Windows.Networking.Proximity.PeerFinder.Role =
Windows.Networking.Proximity.PeerRole.Host;
break;
case "Client":
Windows.Networking.Proximity.PeerFinder.Role =
Windows.Networking.Proximity.PeerRole.Client;
break;
}
}
// Set discoveryData property with user supplied text.
var discData = GetDiscoveryDataFromUser();
var writer = new Windows.Storage.Streams.DataWriter(
new Windows.Storage.Streams.InMemoryRandomAccessStream());
writer.WriteString(discData);
Windows.Networking.Proximity.PeerFinder.DiscoveryData =
writer.DetachBuffer();
Windows.Networking.Proximity.PeerFinder.Start();
started = true;
}
}
// Write a message to MessageBlock on the UI thread.
private Windows.UI.Core.CoreDispatcher messageDispatcher = Window.Current.CoreWindow.Dispatcher;
async private void WriteMessageText(string message, bool overwrite = false)
{
await messageDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
if (overwrite)
MessageBlock.Text = message;
else
MessageBlock.Text += message;
});
}
Remarks
The Role property is used in multi-peer app connections to identify whether the peer app is the Host or Client, or if the peer app is participating in a two-peer connection as a Peer. For multi-peer app connections, you must set the Role property before calling the Start method. If the Role property is not set, the default is Peer.
In a multi-peer app scenario, the Role identifies the capability of the apps to connect. A Host app can connect to up to five Client apps. Host apps can only discover apps that advertise as Client apps. Client apps can only discover apps that advertise as Host apps. The Peer role identifies a two-app scenario. Therefore, Peer apps can only discover other Peer apps. The same rules apply for peer apps connected using a tap gesture. For example, apps advertising as Host apps can only tap to connect with apps advertising as Client apps.