Android WiFi Direct Example

Deprecated now. This blog is old now. Also blog is intend to cover only basics of wifi direct technology.

Wi-Fi Direct allows Android 4.0 (API level 14) or later devices with the right hardware to connect directly to each other via Wi-Fi without an intermediate access point. Using these APIs, you can discover and connect to other devices when each device supports Wi-Fi Direct, then communicate over a speedy connection across distances much longer than a Bluetooth connection. This is useful for applications that share data among users, such as a multiplayer game or a photo sharing application.

The WifiP2pManager class provides methods to allow you to interact with the Wi-Fi hardware on your device to do things like discover and connect to peers. The following actions are available:

You can find all set up details http://developer.android.com/guide/topics/connectivity/wifip2p.html

I will try to explain few more things about wi-fi direct.

  • For peer-to-peer data transmission it does not use any kind of traditional home, office or hotspot network.
  • For Security purpose it uses WPA2 encryption protection.
  • It can transfer data at the speed of 2.5 to 3.0 Mbps. (Tested on Galaxy Tab-2 and Nexus 7 Tab)
  • Wi-Fi Direct can operate at up to 100m. Some reference site says 656 feet too.
  • We can also set up group between devices for which hardware support is offered for wifi direct.

Below is the process explained to perform this feature.

  1. Check whether wifi direct is available or not using WifiP2pManager.WIFI_P2P_STATE_ENABLED.
  2. To discover peers that are available to connect to, call discoverPeers() to detect available peers that are in range.
  3. If the discovery process succeeds and detects peers, the system broadcasts the WIFI_P2P_PEERS_CHANGED_ACTION intent, which you can listen for in a broadcast receiver to obtain a list of peers.
  4. When you have figured out the device that you want to connect to after obtaining a list of possible peers, call the connect() method to connect to the device.
  5. To transfer the data it uses Sockets. So combination of ServerSocket and Socket class from network package performs actual sending and receiving of the file.

 

Find a simple example : WifiDemo.zip

86 thoughts on “Android WiFi Direct Example

    1. Hi Akhazhu please read previous comments I already suggested some demo app to other visitors.

  1. i tried your code but its transferring files from client to server. This is unidirectional transfer. i want to achieve Bi Directional transfer of data. Tell me how can i achieve that.

  2. great tutorial,

    can you give example oh android app that uses wifi to send digital values like 1’s and 0’s to be received by a router and directly give the info to ARDUINO for comparison.. its like a switch.. i am working on our thesis about home automation… hoping for positive feedback 🙂

    1. I don’t have that much customized example. But you can surely send 0 and 1 as bit using this example. Needs lot of modification though.

  3. hello I want to detect the device’s mac address if there wifi is on not the hotspot and list of such type of devices should be shown in my mobile so what should i do..

    1. Hi Lalit,

      MAC address of your wifi and wifi-direct can be different . Be careful about that.

      GET WIFI MAC ADDRESS

      WifiManager wifiMan = (WifiManager) this
      .getSystemService(Context.WIFI_SERVICE);
      WifiInfo wifiInf = wifiMan.getConnectionInfo();
      MAC_ADDRESS = wifiInf.getMacAddress();

      GET WIFI-DIRECT MAC ADDRESS

      WifiP2pDevice device = (WifiP2pDevice) intent
      .getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);

      String myMac = device.deviceAddress;

  4. i want to scan the list of devices and get their mac address
    whose wifi is on not hotspot in my mobile is it possible and how….

    1. Hello sir I have solved this problem thank you for reply now I want to know is it possible to read the content from kismon in kismet os with raspberry pi device and using comfortable programming language insert that data in database…

  5. How can i show my data transfer speed when mobile data is enabled ,and also this could change dynamically sec-sec?

  6. Sir, I want to discuss deeply about an idea of an app to be made on wifi-direct, and i think and hope that you can help me with it. It would be very nice if you could reach me through Email ASAP.
    I want create multi hop connections between devices using wifi direct, Device A wants to reach C but is not in range, so another device B which is both in range of A and C will take the message from A and pass it to C.
    Really need help sir.

    Thanks

  7. i want to send all types of files using wifi direct . . i use the wifi direct demo source code but i am being able to send only images. . how would i modify it so that i would be able to send all types of files and the receiver be able to receive it.

  8. Hi Hardik!
    We are working on developing an app where we need to send push notification to another app through wifi. Is it possible with this?
    Would Appreciate a detailed guidance as I am new Android Development.

    Thanks in Advance
    Satish Sawra

  9. Hi Sir,
    We are also working on developing an app where we need to send push notification to another app through wifi. Is it possible with this?
    Please give a detailed guidance as I am new Android Development.

    Thanks in Advance
    Hemant Khatri

  10. I installed this demo app on two mobile phones and no matter from which device i make the connect request…ONE OF THE DEVICE ALWAYS ACTS AS A SERVER AND THE OTHER AS CLIENT….how do i make a client to my device? please help!

  11. hello sir…
    Sir i want to develope a app that detects wifi direct clients and it needs to save the clients name in sql lite and need to restrict some clients, is it possible …

    if any code samples are already there please send the link…

    thanx in advance..

  12. Hi Hardik,
    I was curious to know if sharing to mutiple devices is possible using WifiP2pManager, just like how Xender does it. Can you share something if you have or know of?

  13. Hi,
    Good tutorial. I want to know if its possible that we can connect Android device with PC through wifi direct having our custom app on both android and PC to send signal from android to PC to start a task on PC.

  14. I tried you app like below mentioned steps
    i used 3 mobile for it
    > created hotspot in mobile 1
    > connect mobile 2 and 3 with mobile 1
    > In mobile 2 i installed your app
    > now searching peer from mobile 2, but nothing was came
    all mobile have jelly bean

  15. I installed your app in one device and and that is connected to router
    one more anderoid lolipop device connected to router
    now i m searching peers from your app but no result spinner is running….. running….. and running….
    can you please help me that i am going on correct way or not?

  16. Hii,
    i want to develop Group Chat Applicaton using wifi could u please help me to develop this…

  17. hi,
    I am developing an application.in that I need to discover the devices connected on a device’s hotspot and I need to do a data transfer from all the connected clients to the owner.need your help on that bro

  18. can we use wifi direct to gaming purpose is it possible because i developed one app for chatting and sharing file but i want add multiple player game in it is it possible dear hardik

    1. Wifi Direct requires users to be in the same network. If you and user are not in same network, wifi direct will not work.

  19. I like your app. I just want to know that is there any modification that will help me to send data to my PC.
    Actually I developed a software in which the input came from an app. I’m using arduino and wifi module to connect the mobile through wifi it get connected but I can’t find a way to send data or input to the software. if you can help me it will be great for me.

  20. hey Mr. Hardik, I am Keval Patel, I am doing my project on the wifi transfer of audio, video and image from android to PC. I am stucked into the backend coding, so can u send me the link for the code of similar app for refrence so that I can get out of this.

  21. Hey Mr. Hardik , I am Keval Patel. I am working on the project that sends image, audio and video from android device to PC via Wifi. But I am stucked in backend coding. So can u send me links of code for same for refrence so that I can get out of this.

  22. Hi ,

    I created an application for exchanging data using wifi direct. It created sockets in serverthread and client thread class to interchange data. Now , the problem which i am getting is the connection is very unrealible. It takes a hell lot of time to discover peers or sometimes breaks the connection in between. Can you please tell how can i make this connection set up stable .

    Thanks in advance

    1. Yes, the Wi-Fi Direct specifications explain it is possible to create a 1 to many connection. One of the devices will act as a group owner (think it as an access point). I have been able to create a Wi-Fi Direct network with 3 devices during some tests.

      As all the devices have to be in the range of the group owner, you are sure that your message will arrive to the second client. 1st client -> group owner -> 2nd client

  23. Please I want to know if the wifi p2p is just for two devices. can you connect to like 30 devices with wifi p2p. im walking on a project. please help. Thanks a lot for the great tutorial

    1. Yes, the Wi-Fi Direct specifications explain it is possible to create a 1 to many connection. One of the devices will act as a group owner (think it as an access point). I have been able to create a Wi-Fi Direct network with 3 devices during some tests.

      As all the devices have to be in the range of the group owner, you are sure that your message will arrive to the second client. 1st client -> group owner -> 2nd client

  24. Hi ,
    The code is very good.Can u plz tell how to take real-time camera pictures with the use of wifi-direct.

  25. sir

    i want to make an app for, remote of a toy car. can you help please.
    i want to send four characters to my toy car F,R,L,R

  26. Hello Hardik
    Nice job you are doing…
    i am Harsh…working as you but in electronics….
    i am beginner in android because i from electronics field. well i need to create two android app. 1st one work as server(wifi hot spot) and continuously receive data over wifi port 80 (http) and second one work as client which sends specified string on some button press over wifi. can you help me to do this task??

  27. Hello man!
    Nice tutorial, I’d like to know if it’s possible simulating the connection between the Android Studio emulator and a real device, and if it is, how could I do that?

    Thanks in advance.

  28. Hey i would like to get totla transmitted and recived data using mobile tethering. Can you please, Suggest me how can i do this.

    Below is my requirement like-

    Name of App Wifibud How it works: User #1 has extra cellular data to sell using tethering. User #2 Needs more data. User#1 starts Wifibud app in a cafe. User #2 Gets “Wireless Networks Available” notification. He clicks the Wifibud network named “Wifibud”. Then gets a splashpage that provides user with 4 options. Buy 50mb . buy 150 mb . buy 500mg. buy 1gb.. User #2 Chooses one and gets charged using app store.(in store purchases).

  29. Hi Sir,
    Actually, instead of wifi p2p I want to use wifi and hotspot to send data b/w two android devices. I hv already done connecting two device in a same N/W using wifi nd hotspot. Can you tell me how to send data b/w them further.

  30. Hello;
    i’m very beginner in Android , so please be patient , how can i open your demo file in Android studio !! I try to :
    Open project from Studio then i click on your demo folder in my desktop then the project opened but without Green Run command, so how can i run it?
    thanks

  31. Hi,

    My requirement is to connect two devices via Wifi-direct in the background to send a certain data. I have explained the flow below.
    1.In background Device A tries to connect to Device B via Wifi-Direct/WifiP2P this should happen Without User;s interference in the foreground.
    2. Then in background certain text data should be sent to other device. This also should happen inthe background.

    So my question is :- Is it possible to estabnlish the P2P connection the background without users interference.

  32. I really need some help on how to connect two devices to each other and control the other devices setting like increasing the light intensity of the other device. Please help!!!

  33. Do we need any more configuration to make to do peer to peer wifi file sharing between PC and Android?
    I mean any software installation,..wifi configuration.etc…?
    Thanks

  34. Hi Bro….I am planning to implement Buzzer type of functionality for my Quiz App. Some Thing like this :

    http://ibuzzedfirst.com/
    After Searching on the same I came to know That It is Possible By Using WifiP2P Connection between devices.
    Can U please guide me on the same…Thanks Bro.

  35. hello sir.
    i want to make a quiz app for my minor project.In that app i want to connect other devices through hotspot.one device host the quiz and that quiz must be display same on all devices connected to that hotsopt.And result of that qiuz also display on all the connected device.
    plz sir tell me hoe can i resolve this problem.

  36. Dear Sir,
    I used your Wifi printout App giving error

    failed to connect to /192.168.118.1 (port 8988) after 9000ms: isConnected failed: ECONNREFUSED (Connection refused)

    so please provide me solution an soon ass possible

  37. Dear Sir,
    I used your Wifi printout App giving error

    failed to connect to /192.168.118.1 (port 8988) after 5000ms: isConnected failed: ECONNREFUSED (Connection refused)

    so please provide me solution as soon as possible

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s