Search code examples
androidsamsung-mobilewifi-direct

Can WiFi Direct do device to device communication other than file sharing?


I am playing with WiFi Direct on a Samsung Galaxy S3 smartphone and a Galaxy tablet. I can share pictures and files between the two using WiFi Direct, either from the Android built-in Share option or some third party apps. But what I am really interested is to do some real time communication programmatically between the two.

Is it true that an Android app can do any kind of normal WiFi communication once I have a working WiFi Direct connection? If yes, any references(ideally some example code) on how to set up a normal connection (say a socket connection) using WiFi Direct? I want to understand the underlying process like how to set up a WiFi Direct connection from within the app or how to obtain the peer device IP.


Solution

  • Yes, it can be done. you can check out the android developers site for more info. Here's a short synopsis of the same:

    • Setting up Permissions:

    The following permissions would have to be added to the app by modifying

    the manifest file: CHANGE_WIFI_STATE, ACCESS_WIFI_STATE,

    and INTERNET

    • Creating Broadcast Reciever and P2P Manager

    This is done by instantiating the following classes:

    ♠ IntentFilter() : To check for changes in the connection/peers. *Any

    problem in connection & changes in the peer list should be detected here

    ♠ WifiP2pManager() : for managing Wi-Fi peer-to-peer connectivity

    ♠ BroadCastReciever() :Base class for code that will receive intents

    • Obtaining List of peers:

    Initiate Peer discovery by giving a call to discoverPeers() method

    Implement PeerListListener. Use peerList.getDeviceList() to obtain the list

    of peers; and pass it to an arraylist

    • Connecting to a peer:

    Create a WifiP2pDevice objects, that will store information about each p2p

    device

    Instantiate WifiP2pConfig object; and copy data into it from the

    wifip2pdevice

    The detailed description of this process can be found out over here: http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html

    I've just tried to simplify & shorten it.

    beyond this; you can use traditional socket programming (UserDatagramSocket or DatagramSocket) for communication. One of the connected devices assumes role of group owner; and can be used as the main device.