Go to Content



Library

Home > Library

How to setup the Wi-Fi Direct network

By bada Master | 13122 views | Aug 22, 2011

In an earlier article (http://developer.bada.com/article/Setup-your-own-WiFi-network-and-play-with-it), you saw how to setup your own adhoc Wi-Fi network. This article looks at an alternative approach to setting up a Wi-Fi network, which is more secure than adhoc Wi-Fi networks.

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.

class MyWifiDirectClass :
	public Osp::Net::Wifi::IWifiDirectDeviceListener,
	public Osp::Net::Wifi::IWifiDirectGroupOwnerListener,
	public Osp::Net::Wifi::IWifiDirectGroupClientListener

{
	Osp::Net::Wifi::WifiDirectDevice* __pWifiDirectLocalDevice;
}

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.

result
MyWifiDirectClass::Construct()
{

	result r = E_SUCCESS;
	IList* pDeviceInfoList = WifiDirectDeviceManager::GetAllDeviceInfoN();
	if (!pDeviceInfoList)
		goto CATCH;
	WifiDirectDeviceInfo* pLocalDeviceInfo = static_cast (pDeviceInfoList->GetAt(0));
	if (!pDeviceInfoList)
		goto CATCH;
		
	__pWifiDirectLocalDevice = WifiDirectDeviceManager::GetWifiDirectDeviceN(pLocalDeviceInfo);
	if (!pDeviceInfoList)
		goto CATCH;
		
	__pWifiDirectLocalDevice->AddWifiDirectDeviceListener(*this);
	__pWifiDirectLocalDevice->AddWifiDirectGroupClientListener(*this);
	__pWifiDirectLocalDevice->AddWifiDirectGroupOwnerListener(*this);

	__pWifiDirectLocalDevice->SetLocalDeviceName(L"Samsung bada");

	if (!__pWifiDirectLocalDevice->IsActivated())
		r = __pWifiDirectLocalDevice->Activate();

	if (IsFailed(r))
		AppLog("Failed to Activate Wi-Fi Direct");
		
CATCH:

	if (pDeviceInfoList)
	{
		pDeviceInfoList->RemoveAll(true);
		delete pDeviceInfoList;
	}

	return r;
}

Once Wi-Fi Direct is activated, you get the OnWifiDirectDeviceActivated notification.

void
WifiDirectMainForm::OnWifiDirectDeviceActivated(WifiDirectDeviceId localDeviceId, result r)
{
	AppLog("Wi-Fi Direct Activated");
}

After activating Wi-Fi Direct, you can Scan for other Wi-Fi Direct devices, create a group, or join an already existing group.

result
MyWifiDirectClass::Scan()
{
	result r = E_SUCCESS;
	
	r = __pWifiDirectLocalDevice->Scan();

	if (IsFailed(r))
		AppLog("Failed to start scan");
		
	return r;
}

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.

void
WifiDirectMainForm::OnWifiDirectGroupCreatedN(WifiDirectDeviceId localDeviceId, const WifiDirectGroupInfo &wifiDirectGroupInfo, const WifiDirectDeviceInfo &wifiDirectGroupOwnerDeviceInfo, WifiDirectGroupMember* pWifiDirectMember, result r)
{
	if (pWifiDirectMember)
	{
		if (pWifiDirectMember->GetGroupMemberType() ==       
                 WIFI_DIRECT_GROUP_MEMBER_TYPE_CLIENT)
		{
			AppLog("Device Is Group Client");
		}
		else if (pWifiDirectMember->GetGroupMemberType()== 
                 WIFI_DIRECT_GROUP_MEMBER_TYPE_OWNER)
		{
			AppLog("Device Is Group Owner");
		}
	}
	delete pWifiDirectMember;
}

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.

result
MyWifiDirectClass::Disassociate()
{
	result r = E_SUCCESS;
	
	r = __pWifiDirectLocalDevice->Disassociate(); 

	if (IsFailed(r))
		AppLog("Failed to disassociate");
		
	return r;
}

The following method destroys the Wi-Fi Direct group.

On completion, the OnWifiDirectGroupDestroyed() method notifies that the Wi-Fi Direct Group Service has been stopped.

result
MyWifiDirectClass::DestroyGroup()
{
	result r = E_SUCCESS;
	
	r = __pWifiDirectGroupOwner->DestroyGroup();

	if (IsFailed(r))
		AppLog("Failed to destroy group");
		
	return r;
}

Did this document help you? Yes | It's good,but ... | Not Helpful