Related Article

SDK Version

2.0

Wi-Fi Direct emulates a Wi-Fi Access Point (AP). It embeds a soft AP to which other Wi-Fi Direct and legacy Wi-Fi devices can connect. Unlike adhoc mode, Wi-Fi Direct uses Wi-Fi Protected Setup (WPS), which prevents unauthorized access. To communicate with other devices using Wi-Fi Direct, first a Wi-Fi Direct Group must be created. A Wi-Fi Direct Group has a Group Owner, which acts as the soft AP, and up to 8 Group Clients.

The code sample below serves as further clarification. Consider a sample class named MyWifiDirectClass that implements IWifiDirectDeviceListener, IWifiDirectGroupOwnerListener and IWifiDirectGroupClientListener.

Next create a local Wi-Fi Direct Device instance and activate Wi-Fi Direct. Also, add Wi-Fi Direct Device, Group Owner, and Group Client listeners so that you can receive the corresponding notifications. You can also modify the device name that is displayed when other devices search for your device. Remember that the local device name set by the application is valid only during the life cycle of the application. The local device name cannot be modified if Wi-Fi Direct is already activated.

Once the Scan is completed, you get the OnWifiDirectScanCompletedN notification. From the scan information, you can select a device to create a group or you can select a Group Owner to join that particular group.

void
WifiDirectMainForm::OnWifiDirectScanCompletedN(WifiDirectDeviceId localDeviceId, Osp::Base::Collection::IList *pWifiDirectDeviceInfoList, result r)
{
// free the memory allocated to pWifiDirectDeviceInfoList when no longer required.
}
It is not necessary to scan to create a group. When no device is specified, group formation is initiated with whichever device has the same device name and is found first.
result
MyWifiDirectClass::CreateGroup()
{
result r = E_SUCCESS;
r = __pWifiDirectLocalDevice->CreateGroup();
if (IsFailed(r))
AppLog("Failed to create group");
return r;
}

Once the Group is created, you get the OnWifiDirectGroupCreatedN notification. Remember that during group formation either of the 2 devices can become the Group Owner. The device that has a greater intent value (parameter in WifiDirectGroupInfo) becomes the Group Owner. If both the devices have the same intent, one of the devices randomly becomes the Group Owner. Note that, two devices with intent of 15 cannot form a group.

While creating group, authentication procedure has to be followed. The default WPS configuration mode is PBC (Push Button Configuration). The request receiving device shows a pop-up to allow connection from the other device. If the WPS configuration mode is selected as PIN DISPLAY, a PIN is displayed on the other device. You must enter the same PIN on your device.

To join an existing group, you require Group Owner information, which is available from the scan result.

The following API establishes a connection to a specific Group Owner.

If a connection to another access point is already established, the previous connection must be disconnected before using this method.

On completion of the Associate () method, the device is notified through the OnWifiDirectAssociationCompleted () method, indicating that the connection to a Wi-Fi Direct Group Owner has been completely established.

result
MyWifiDirectClass::Associate()
{
result r = E_SUCCESS;
// Group Owner info can be retrieved from the scan result.
r = __pWifiDirectLocalDevice->Associate(*__pWifiDirectGroupOwnerInfo);
if (IsFailed(r))
AppLog("Failed to associate");
return r;
}

The following function closes the connection that was established through Associate ().

On completion of Disassociate(), the OnWifiDirectDisassociationCompleted() method is called, Indicating that the connection to the Wi-Fi Direct Group Owner has been terminated.