Android Tutorial Android Wifi-Direct Tutorial

Discussion in 'Tutorials & Examples' started by Erel, Jun 20, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Android 4+ devices support Wifi Direct (Wifi P2p). Wifi Direct allows you to connect two devices over wireless without an access point. It is similar to Bluetooth with a much extended range and performance.

    It is recommended to use Android 4.1+ devices as there are several major known issues with Android 4.04 devices and Wifi Direct.


    Establishing a connection over wifi direct is done in two main steps. First you need to discover and make a peer to peer connection. This step ends when you have the IP address of the other device.

    In the second step you use the Network library (together with AsyncStreams) to create a connection with the other device IP address.

    Creating the peer to peer connection

    First we should find the nearby peers by calling manager.DiscoverPeers.
    The PeersDiscovered event will be raised when the process completes.
    This event includes a list with the discovered devices. Note that this event will also be raised during the connection process so your code should be ready to handle multiple calls of this event.

    Once we have a device we can call manager.Connect with the device MAC address. The ConnectionChanged event will be raised.

    This event includes the group owner IP address. If the address is "" then the current device is the owner. You should use a ServerSocket to accept incoming connections.

    Otherwise you need a Socket to connect to the group owner ip.

    The attached example implements a wifi direct connection and then allows you to send a message from one device to the other. The implementation is done with a Service. It is easier to use a Service for such tasks as the service will not be destroyed like the activity.

    BTW, I did a small test to check the maximum range until the connection broke. The result was about 40 - 50 meters (tested with Galaxy Nexus and Nexus 4).

    The library is available here:

    Attached Files:

  2. Rick in North Carolina

    Rick in North Carolina Member Licensed User

    Ah yes, I have been wanting to develop with wifi-direct, Thank you for this library :sign0098:
    Toley likes this.
  3. myriaddev

    myriaddev Active Member Licensed User

    WiFi Direct error

    Hi. After putting library pair into Addlibrary directory, I get the following
    error message, using B4A V2.71 and nexus 7 V4.22.

    Parsing code. 0.03
    Compiling code. 0.16
    Compiling layouts code. 0.02
    Generating R file. 0.33
    Compiling generated Java code. Error
    B4A line: 35
    Dim dev As WifiDevice = discoveredDevices.Get(0)
    javac 1.6.0_22
    src\b4a\example\ package does not exist
    1 error

    Please help. Jerry

    ** Updated reference to android.jar to level 14 and it worked! Thanks Erel
    Last edited: Jun 21, 2013
  4. Erel

    Erel Administrator Staff Member Licensed User

    Make sure that you reference android.jar from platform level 14 or above (Tools - Configure Paths).
  5. bluedude

    bluedude Well-Known Member Licensed User


    Do you think we can do something like Apple Airdrop with this?

  6. Erel

    Erel Administrator Staff Member Licensed User

    I'm not really familiar with Apple Airdrop. However you can send files between two connected devices.
  7. bluedude

    bluedude Well-Known Member Licensed User

    WOuld you happen to have a file sample? Not sure how to write Async streams into a file like a bitmap.

  8. Erel

    Erel Administrator Staff Member Licensed User

    I plan to create a mini-framework based on AsyncStreams that will make it easier to send and receive objects and files.
    luke2012 likes this.
  9. Mahares

    Mahares Well Known Member Licensed User

    I tried to pair a Razr phone (OS: 4.1.2) and a Nexus 7 (OS:4.2.2), but they would not communicate. The Nexus shows : Enable:true, the other shows nothing.:
    1. What else besides installing the example on both devices do you need to pair them?
    2. I can't wait to see your sample for transferring files between the two. That is the real deal. File transfer between paired devices is the practical thing I am looking for. I tried it with the embedded HTTP server, but could not figure it out despite some of your tips.
  10. awama

    awama Active Member Licensed User

    I tried to pair a SGS2 phone (OS: 4.0.3) and a SGS4 (OS:4.2.2), but they would not communicate.
    The SGS2 shows : Enable:true, Peerde Devices: 1, Connected: Sockets are connected :)
    The SGS4 shows : Enable:true, Peerde Devices: 1, Connected: Wifi Direct connected (

    The Button "Send Hello Message" has no effect.

    Is the reason the mentioned OS 4.0.4?
  11. Erel

    Erel Administrator Staff Member Licensed User

    Seems like the other device doesn't support Wifi Direct.

    The process should be:
    - enabled: true (on both devices)
    - Press on discover peers on both devices: Discovered peers: 1
    - Press on Connect on both devices. Socket should be connected (first the p2p connection is established and then the socket connection.)

    - Turn off and on the Wifi.
    - Check the unfiltered logs for any messages.
  12. Scooter Willis

    Scooter Willis Member Licensed User

    i cant get service 1 to work on my program
  13. Erel

    Erel Administrator Staff Member Licensed User

    Which error do you get?
  14. luke2012

    luke2012 Well-Known Member Licensed User

    Hi Erel !
    It's possible to setup multiple wifi direct connections in a many to one scenario ?

    I mean that many devices connet to a single device.
    If this scenario is supported, is possible to send multiple data in the same time (each device sends data) in a many to one scenario ?
  15. Erel

    Erel Administrator Staff Member Licensed User

    I haven't tested it with multiple devices, however I expect the connection process to work correctly. However once connected you will still need to manage a Socket between each device.
    almontgreen likes this.
  16. HansEman

    HansEman New Member Licensed User

    This is just a question - I want to make an Android device control a PC screen (send short text that my VB6 client will read and send to a running Windows program as keypress - like SendKeys).
    Could I use this WiFi connect or should I use TcpIP?
  17. Erel

    Erel Administrator Staff Member Licensed User

    Does your PC support wifi-direct? It most probably doesn't support this feature.

    If both the PC and the device are connected to the same router then you can use the Network library to connect.
    HansEman likes this.
  18. HansEman

    HansEman New Member Licensed User

    Thank you Erel, that's what I'll do.
  19. elck

    elck Member Licensed User

    This come close to what I am looking for.
    I managed to discover my Sony Actioncam (Enabled: true) (peered devices: 1)
    When connecting it always says 'Trying to connect'
    Which I think is to be expected, as the cam requires a password before it connects.
    I haven't got a clue on how to proceed from here.
    The documentation for the camera does not make much sense to me.
    I found it here:
    Can you please put me on my way?

  20. Erel

    Erel Administrator Staff Member Licensed User

    I assume that this camera supports wifi direct, is that correct?

    Do they provide any Android app that you can try to connect over wifi direct?
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice