TriggeredConnectionStateChangedEventArgs class

Applies to Windows and Windows Phone

Contains properties that the TriggeredConnectionStateChanged event passes to an application.

Syntax


public sealed class TriggeredConnectionStateChangedEventArgs

Attributes

[MarshalingBehavior(Agile)]
[Version(0x06020000)]

Members

The TriggeredConnectionStateChangedEventArgs class has these types of members:

Methods

The TriggeredConnectionStateChangedEventArgs class inherits methods from the Object class (C#/VB/C++).

Properties

The TriggeredConnectionStateChangedEventArgs class has these properties.

PropertyAccess typeDescription

Id

Read-onlyGets the unique identifier for the connection in progress.

Socket

Read-onlyGets the StreamSocket instance for the connection.

State

Read-onlyGets the state of the connection in progress.

 

Examples


    // Click event for "Start" button.

    private void StartFindingPeersButton_Click(object sender, RoutedEventArgs e)
    {
        if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
             Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) ==
             Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered)
        {
            Windows.Networking.Proximity.PeerFinder.Start();

            Windows.Networking.Proximity.PeerFinder.TriggeredConnectionStateChanged += 
                TriggeredConnectionStateChanged;

            WriteMessageText("Waiting for peers. Enter proximity to connect. " +
                "Call PeerFinder.start() on the peer to send and receive messages.\n");
        }
        else {
            WriteMessageText("Peer discovery using tapping is not supported.\n");
        }
    }

    private void TriggeredConnectionStateChanged(object sender,
        Windows.Networking.Proximity.TriggeredConnectionStateChangedEventArgs e)
    {
        if (e.State == Windows.Networking.Proximity.TriggeredConnectState.Completed)
        {
            WriteMessageText("Connected. You may now send a message.\n");
            SendMessage(e.Socket);
        }
    }

    Windows.Networking.Sockets.StreamSocket proximitySocket;
    Windows.Storage.Streams.DataWriter dataWriter;

    // Reference socket streams for writing and reading messages.
    private void SendMessage(Windows.Networking.Sockets.StreamSocket socket) {
        // Get the network socket from the proximity connection.
        proximitySocket = socket;

        // Create DataWriter for writing messages to peers.
        dataWriter = new Windows.Storage.Streams.DataWriter(proximitySocket.OutputStream);

        // Listen for messages from peers.
        Windows.Storage.Streams.DataReader dataReader = 
                new Windows.Storage.Streams.DataReader(proximitySocket.InputStream);
        StartReader(proximitySocket, dataReader);
    }

    // Send a message to the socket.
    private async void SendMessageText() {
        string msg = SendMessageTextBlock.Text;

        if (msg.Length > 0) {
            var msgLength = dataWriter.MeasureString(msg);
            dataWriter.WriteInt32(msg.Length);
            dataWriter.WriteString(msg);
            try
            {
                await dataWriter.StoreAsync();
                WriteMessageText("Message sent: " + msg  + "\n");
            }
            catch (Exception e)
            {
                WriteMessageText("Send error: " + e.Message + "\n");
            }
        }
    }

    // Read out and print the message received from the socket.
    private async void StartReader(
        Windows.Networking.Sockets.StreamSocket socket, 
        Windows.Storage.Streams.DataReader reader)
    {
        uint initialLength = 4;

        try
        {
            await reader.LoadAsync(initialLength);
            uint msgLength = (uint)reader.ReadInt32();

            try
            {
                await reader.LoadAsync(msgLength);
                string message = reader.ReadString(msgLength);
                WriteMessageText("Received message: " + message + "\n");

                // After receiving a message, listen for the next message.
                StartReader(socket, reader);
            }
            catch (Exception e)
            {
                WriteMessageText("Error: " + e.Message + "\n");
                socket.Dispose();
            }
        }
        catch (Exception e)
        {
            WriteMessageText("Error: " + e.Message + "\n");
            socket.Dispose();
        }
    }

    // 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;
            });
    }


Requirements

Minimum supported client

Windows 8 [Windows Store apps only]

Minimum supported server

Windows Server 2012 [Windows Store apps only]

Minimum supported phone

Windows Phone 8

Namespace

Windows.Networking.Proximity
Windows::Networking::Proximity [C++]

Metadata

Windows.winmd

Capabilities

proximity
ID_CAP_PROXIMITY [Windows Phone]

See also

TriggeredConnectionStateChanged
Proximity and Tapping (JavaScript)
Proximity and Tapping (C#/VB/C++)
Samples
Proximity sample

 

 

Show:
© 2014 Microsoft