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).

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 includes the following:

Note

You can test the Wi-Fi Direct 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, follow the steps below:

  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:

    XML
    Copy
    <privileges> <privilege>http://tizen.org/privilege/wifidirect</privilege> </privileges>
  2. To use the methods and properties of the Tizen.Network.WiFiDirect namespace, include it in your application:

    C#
    Copy
    using Tizen.Network.WiFiDirect;

Manage events

To manage events related to Wi-Fi Direct operations, follow these steps:

  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).
    C#
    Copy
    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. Register the event handlers for the DeviceStateChanged, DiscoveryStateChanged, and ConnectionStatusChanged events of the Tizen.Network.WiFiDirect.WiFiDirectManager class:

    C#
    Copy
    WiFiDirectManager.DeviceStateChanged += EventHandlerDeviceStateChanged; WiFiDirectManager.DiscoveryStateChanged += EventHandlerDiscoveryStateChanged; WiFiDirectManager.ConnectionStatusChanged += EventHandlerConnectionStatusChanged;
  3. When they are no longer needed, deregister the event handlers:

    C#
    Copy
    WiFiDirectManager.DeviceStateChanged -= EventHandlerDeviceStateChanged; WiFiDirectManager.DiscoveryStateChanged -= EventHandlerDiscoveryStateChanged; WiFiDirectManager.ConnectionStatusChanged -= EventHandlerConnectionStatusChanged;

Activate Wi-Fi Direct

To activate a Wi-Fi Direct local device and check the device state, follow the steps below:

  1. Activate Wi-Fi Direct on the local device using the Activate() method of the Tizen.Network.WiFiDirect.WiFiDirectManager class:

    C#
    Copy
    try { WiFiDirectManager.Activate(); } catch (Exception e) { Console.WriteLine("Exception occurred" + e.ToString()); }
  2. Check the Wi-Fi Direct state.

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

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

Get the Wi-Fi Direct peer device information

To discover nearby Wi-Fi Direct peer devices and retrieve their details, follow these steps:

  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:

    C#
    Copy
    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. Register the event handler for the PeerFound event and start the discovery:

    C#
    Copy
    try { WiFiDirectManager.PeerFound += EventHandlerPeerFound; WiFiDirectManager.StartDiscovery(false, 30); } catch (Exception e) { Console.WriteLine("Exception occurred" + e.ToString()); }
  3. When you have found all the peer devices you need, deregister the event handler:

    C#
    Copy
    WiFiDirectManager.PeerFound -= EventHandlerPeerFound;

Connect to a specific Wi-Fi Direct peer device

To connect to a specific device, follow the steps below:

  1. Start the discovery and retrieve the peer device you want to connect to:

    C#
    Copy
    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. Connect to a peer device by following these steps:

    • 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:

      C#
      Copy
      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); } }
    • 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:

      C#
      Copy
      try { peer.ConnectionStateChanged += EventHandlerConnectionStateChanged; peer.Connect(); } catch (Exception e) { Console.WriteLine("Exception occurred" + e.ToString()); }
  3. When the connection is no longer needed, disconnect the peer from the Wi-Fi Direct device and deregister the event handler:

    C#
    Copy
    try { peer.Disconnect(); peer.ConnectionStateChanged -= EventHandlerConnectionStateChanged; } catch (Exception e) { Console.WriteLine("Exception occurred" + e.ToString()); }

Manage Wi-Fi direct groups

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

  1. To create the group, follow these steps:

    • 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:

      C#
      Copy
      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"); } }
    • Register the event handler for the ConnectionStatus event of the Tizen.Network.WiFiDirect.WiFiDirectManager class, and create the group using the CreateGroup() method:

      C#
      Copy
      try { WiFiDirectManager.ConnectionStatusChanged += EventHandlerConnectionStatusChanged; WiFiDirectManager.CreateGroup(); } catch (Exception e) { Console.WriteLine("Exception occurred" + e.ToString()); }
  2. When the group is no longer needed, destroy it and deregister the event handler:

    C#
    Copy
    try { WiFiDirectManager.DestroyGroup(); WiFiDirectManager.ConnectionStatusChanged -= EventHandlerConnectionStatusChanged; } catch (Exception e) { Console.WriteLine("Exception occurred" + e.ToString()); }

Deactivate Wi-Fi Direct

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

  1. Deactivate Wi-Fi Direct on the local device using the Deactivate() method of the Tizen.Network.WiFiDirect.WiFiDirectManager class:

    C#
    Copy
    try { if (WiFiDirectManager.State == Activated) { WiFiDirectManger.Deactivate(); } } catch (Exception e) { Console.WriteLine("Exception occurred" + e.ToString()); }
  2. Check the device state using the State property of the Tizen.Network.WiFiDirect.WiFiDirectManager class:

    C#
    Copy
    try { if (WiFiDirectManager.State == Deactivated) { Console.WriteLine("Wi-Fi Direct is deactivated"); } } catch (Exception e) { Console.WriteLine("Exception occurred" + e.ToString()); }
  • Dependencies
    • Tizen 4.0 and Higher
Wi-Fi
Next Bluetooth
Submit your feedback to GitHub