From d455ed781e02bb9c0d69ddb67f2bef35c172bb75 Mon Sep 17 00:00:00 2001 From: Editor Lionbridge Date: Tue, 13 Jun 2017 14:45:59 +0300 Subject: [PATCH] Add .NET Wi-Fi Direct Guide Note that since the general structure of the .NET content is still open, the new topic has not been added to any index files, and there is no parent topic introducing the .NET guides. Also, since there is no .NET AR content in git, all AR links lead directly to TD. PS2: Fixed whitespace issues. Change-Id: Ieed1f4bbe2ed3bf4b10b78a430eb25b27a549d96 --- org.tizen.guides/html/dotnet/wifi_direct.htm | 392 +++++++++++++++++++++++++++ 1 file changed, 392 insertions(+) create mode 100644 org.tizen.guides/html/dotnet/wifi_direct.htm diff --git a/org.tizen.guides/html/dotnet/wifi_direct.htm b/org.tizen.guides/html/dotnet/wifi_direct.htm new file mode 100644 index 0000000..f7ac047 --- /dev/null +++ b/org.tizen.guides/html/dotnet/wifi_direct.htm @@ -0,0 +1,392 @@ + + + + + + + + + + + + + + Wi-Fi Direct® + + + + +
+
+

Mobile C#

+
+ + +
+ +
+ +

Wi-Fi Direct®

+ +

Wi-Fi Direct® is a technology that allows you to find nearby Wi-Fi Direct devices and form a Wi-Fi Direct group to communicate over a peer-to-peer link without wireless access points (base stations) in the infrastructure mode. Wi-Fi Direct is a synonym for Wi-Fi P2P (Peer-to-Peer).

+ +

This feature is supported in mobile applications only.

+ +

In a Wi-Fi Direct group, the group owner works as an access point in the Wi-Fi infrastructure mode and the other devices join the group as clients. A group can be created either by negotiation between 2 devices or in an autonomous mode by a single group owner device. In a negotiation-based group creation, 2 devices compete based on the group owner intent value and the higher intent device becomes a group owner, while the other device becomes a group client. In an autonomous group creation, a device becomes a group owner by itself without any group client.

+ +

A Wi-Fi Direct device can join an existing group by associating itself with the group owner, as long as the allowed number of clients is not exceeded.

+ +

The main features of the Tizen.Network.WiFiDirect namespace include:

+ + + +
+Note +You can test Wi-Fi functionality on a target device only. The emulator does not support this feature. +
+ +

Prerequisites

+

To enable your application to use the Wi-Fi Direct functionality:

+
    +
  1. To use the Tizen.Network.WiFiDirect namespace, the application has to request permission by adding the following privilege to the tizen-manifest.xml file: +
    +<privileges>
    +   <privilege>http://tizen.org/privilege/wifidirect</privilege>
    +</privileges>
    +
    +
  2. +
  3. To use the methods and properties of the Tizen.Network.WiFiDirect namespace, include it in your application: +
    +using Tizen.Network.WiFiDirect;
    +
    +
  4. +
+ +

Managing Events

+

To manage events related to Wi-Fi Direct operations:

+
    +
  1. Define event handlers: +
      +
    • EventHandlerDeviceStateChanged() is triggered when the device state changes (Wi-Fi Direct is activated or deactivated on a local device).
    • +
    • EventHandlerDiscoveryStateChanged() is triggered when the discovery state changes (for example, when discovery starts or stops, or a peer device is found).
    • +
    • EventHandlerConnectionStatusChanged() is triggered when the Wi-Fi Direct connection state changes (for example, the device is disconnected from or connected to peer devices, or a group is created or destroyed).
    • +
    + +
    +public static void EventHandlerDeviceStateChanged(object sender, EventArgs e)
    +{
    +    Console.WriteLine("Device state: " + e.DeviceState);
    +}
    +public static void EventHandlerDiscoveryStateChanged(object sender, EventArgs e)
    +{
    +    Console.WriteLine("Discovery state: " + e.DiscoveryState);
    +}
    +public static void EventHandlerConnectionStatusChanged(object sender, EventArgs e)
    +{
    +    Console.WriteLine("WiFi Direct connection state: " + e.ConnectionState);
    +}
    +
    +
  2. +
  3. Register the event handlers for the DeviceStateChanged, DiscoveryStateChanged, and ConnectionStatusChanged events of the Tizen.Network.WiFiDirect.WiFiDirectManager class: +
    +WiFiDirectManager.DeviceStateChanged += EventHandlerDeviceStateChanged;
    +
    +WiFiDirectManager.DiscoveryStateChanged += EventHandlerDiscoveryStateChanged;
    +
    +WiFiDirectManager.ConnectionStatusChanged += EventHandlerConnectionStatusChanged;
    +
    +
  4. +
  5. When they are no longer needed, deregister the event handlers: +
    +WiFiDirectManager.DeviceStateChanged -= EventHandlerDeviceStateChanged;
    +
    +WiFiDirectManager.DiscoveryStateChanged -= EventHandlerDiscoveryStateChanged;
    +
    +WiFiDirectManager.ConnectionStatusChanged -= EventHandlerConnectionStatusChanged;
    +
    +
  6. +
+ +

Activating Wi-Fi Direct

+ +

To activate a WiFi-Direct local device and check the device state:

+
    +
  1. Activate Wi-Fi Direct on the local device using the Activate() method of the Tizen.Network.WiFiDirect.WiFiDirectManager class. +
    +try
    +{
    +    WiFiDirectManager.Activate();
    +}
    +catch (Exception e)
    +{
    +    Console.WriteLine("Exception occurred" + e.ToString());
    +}
    +
    +
  2. +
  3. Check the Wi-Fi Direct state. +

    You can check the Wi-Fi Direct local device state using the Tizen.Network.WiFiDirect.WiFiDirectManager.State property. Since Wi-Fi Direct has just been activated on the device, the device state is Activated.

    +
    +try
    +{
    +    if (WiFiDirectManager.State == Activated)
    +    {
    +        Console.WriteLine("Wi-Fi Direct is activated");
    +    }
    +}
    +catch (Exception e)
    +{
    +    Console.WriteLine("Exception occurred" + e.ToString());
    +}
    +
    +
  4. +
+ +

Getting the Wi-Fi Direct Peer Device Information

+

To discover nearby Wi-Fi Direct peer devices and retrieve their details:

+ +
    +
  1. Define an event handler that is triggered each time a peer device is found during discovery. +

    In this example, the event handler displays the name and MAC address of each found peer device. You can also get other information, such as the connection state, device type, Wi-Fi display information, and WPS type.

    + +
    +public static void EventHandlerPeerFound(object sender, EventArgs e)
    +{
    +    Console.WriteLine("Discovery state: " + e.DiscoveryState);
    +    if (e.DiscoveryState == WiFiDirectDiscoveryState.Found && e.Peer != null)
    +    {
    +        Console.WriteLine("Peer device name: " + e.Peer.Name);
    +        Console.WriteLine("Peer mac address: " + e.Peer.MacAddress);
    +    }
    +}
    +
    +
  2. +
  3. Register the event handler for the PeerFound event and start the discovery: +
    +try
    +{
    +    WiFiDirectManager.PeerFound += EventHandlerPeerFound;
    +    WiFiDirectManager.StartDiscovery(false, 30);
    +}
    +catch (Exception e)
    +{
    +    Console.WriteLine("Exception occurred" + e.ToString());
    +}
    +
    +
  4. +
  5. When you have found all the peer devices you need, deregister the event handler: +
    +WiFiDirectManager.PeerFound -= EventHandlerPeerFound;
    +
    +
  6. +
+ +

Connecting to a Specific Wi-Fi Direct Peer Device

+

To connect to a specific device:

+
    +
  1. Start the discovery and retrieve the peer device you want to connect to: +
    +string peerName = "PeerDeviceName";
    +public static void EventHandlerPeerFound(object sender, EventArgs e)
    +{
    +    Console.WriteLine("Discovery state: " + e.DiscoveryState);
    +    if (e.DiscoveryState == WiFiDirectDiscoveryState.Found && e.Peer.Name == peerName)
    +    {
    +        Console.WriteLine("Peer device found");
    +        WiFiDirectPeer peer = e.Peer;
    +    }
    +}
    +
    +try
    +{
    +    WiFiDirectManager.PeerFound += EventHandlerPeerFound;
    +    WiFiDirectManager.StartDiscovery(false, 30);
    +}
    +catch (Exception e)
    +{
    +    Console.WriteLine("Exception occurred" + e.ToString());
    +}
    +
    +
  2. +
  3. Connect to the peer device: +
      +
    1. Define an event handler that is triggered each time the peer device connection state changes. +

      In this example, the event handler displays the connection state and MAC address of the connected device:

      +
      +public static void EventHandlerConnectionStateChanged(object sender, EventArgs e)
      +{
      +    Console.WriteLine("Connection state: " + e.State);
      +    if (e.Error == WiFiDirectError.None && e.State == ConnectionRsp)
      +    {
      +        Console.WriteLine("Connected device mac address: " + e.MacAddress);
      +    }
      +}
      +
      +
    2. +
    3. Register the event handler for the ConnectionStateChanged event of the Tizen.Network.WiFiDirect.WiFiDirectPeer class and connect the peer with the local Wi-Fi Direct device using the Connect() method: +
      +try
      +{
      +    peer.ConnectionStateChanged += EventHandlerConnectionStateChanged;
      +    peer.Connect();
      +}
      +catch (Exception e)
      +{
      +    Console.WriteLine("Exception occurred" + e.ToString());
      +}
      +
      +
    4. +
    +
  4. +
  5. When the connection is no longer needed, disconnect the peer from the Wi-Fi Direct device and deregister the event handler: +
    +try
    +{
    +    peer.Disconnect();
    +    peer.ConnectionStateChanged -= EventHandlerConnectionStateChanged;
    +}
    +catch (Exception e)
    +{
    +    Console.WriteLine("Exception occurred" + e.ToString());
    +}
    +
    +
  6. +
+ +

Managing Wi-Fi Direct Groups

+

To create an autonomous Wi-Fi Direct group and to make your device the autonomous group owner:

+
    +
  1. Create the group: +
      +
    1. Define an event handler that is triggered each time the connection status changes. +

      In this example, the event handler checks whether the group has been successfully created:

      +
      +public static void EventHandlerConnectionStatusChanged(object sender, EventArgs e)
      +{
      +    Console.WriteLine("Connection state: " + e.State);
      +    if (e.Error == WiFiDirectError.None && e.State == GroupCreated)
      +    {
      +        Console.WriteLine("Group created successfully");
      +    }
      +    if (e.State == GroupDestroyed)
      +    {
      +        Console.WriteLine("Group destroyed successfully");
      +    }
      +}
      +
    2. +
    3. Register the event handler for the ConnectionStatus event of the Tizen.Network.WiFiDirect.WiFiDirectManager class, and create the group using the CreateGroup() method: +
      +try
      +{
      +    WiFiDirectManager.ConnectionStatusChanged += EventHandlerConnectionStatusChanged;
      +    WiFiDirectManager.CreateGroup();
      +}
      +catch (Exception e)
      +{
      +    Console.WriteLine("Exception occurred" + e.ToString());
      +}
      +
      +
    +
  2. +
  3. When the group is no longer needed, destroy it and deregister the event handler: +
    +try
    +{
    +    WiFiDirectManager.DestroyGroup();
    +    WiFiDirectManager.ConnectionStatusChanged -= EventHandlerConnectionStatusChanged;
    +}
    +catch (Exception e)
    +{
    +    Console.WriteLine("Exception occurred" + e.ToString());
    +}
    +
    +
  4. +
+ +

Deactivating Wi-Fi Direct

+

To deactivate Wi-Fi Direct when it is no longer needed (or the application is exiting):

+
    +
  1. Deactivate Wi-Fi Direct on the local device using the Deactivate() method of the Tizen.Network.WiFiDirect.WiFiDirectManager class: +
    +try
    +{
    +    if (WiFiDirectManager.State == Activated)
    +    {
    +        WiFiDirectManger.Deactivate();
    +    }
    +}
    +catch (Exception e)
    +{
    +    Console.WriteLine("Exception occurred" + e.ToString());
    +}
    +
    +
  2. +
  3. Check the device state using the Tizen.Network.WiFiDirect.WiFiDirectManager.State property: +
    +try
    +{
    +    if (WiFiDirectManager.State == Deactivated)
    +    {
    +        Console.WriteLine("Wi-Fi Direct is deactivated");
    +    }
    +}
    +catch (Exception e)
    +{
    +    Console.WriteLine("Exception occurred" + e.ToString());
    +}
    +
    +
  4. +
+ + + +
+ +Go to top + + + + + + + -- 2.7.4