Android Tutorial Android Wifi-Direct Tutorial

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.

SS-2013-06-20_15.39.46.jpg


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 "127.0.0.1" 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: http://www.b4x.com/forum/additional...ates/30410-wifidirect-library.html#post176527
 

Attachments

  • WifiDirectExample.zip
    8.7 KB · Views: 16,959

camolas

Member
Licensed User
Longtime User
Hi,

After whe get the devices peered on first connection and then exit the app and leave one app in one device runing can i connect the sockets by only press on connect button of one device only?
In my case i whave to press on the connect buttons !?
Thanks
 

camolas

Member
Licensed User
Longtime User
You need to first click on the discover button.

On both devices?! dont work for me
I need to connect both devicess automatically.
Example: Tablet and phone

1 - discover on both devices
2 - connect on both devices and "send hello...." every thing works fine
3 - disconnect phone (tablet still runing app)
4 - Now the problem is there way to connect phone to tablet without have to press any button on tablet side?

Thanks
 

camolas

Member
Licensed User
Longtime User
Thanks Erel,

Im using the hotspot solution but some times my wifi gets crazy and cant connect to any wifi dont now if is from my app or phone.... and every time i connect to tablet hotspot it creats a new connection name in wifi avaible list... a lot with same name dont now if his this that put wifi crazy....
 

almontgreen

Active Member
Licensed User
Longtime 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.
Where can I find more information about this with perhaps some sample code? I've just started messing about with AsyncStreamsObject and it works great between two devices... but I have 6 that need to communicate. Yikes! I'm in over my head right now...
 

tunde

Member
Licensed User
Longtime User
t
SS-2013-06-20_15.39.46.jpg


The WifiDirect library is not working for me,i used the code in the wifidirect example.zip
my phone keeps displaying searching for devices for hours without discovering anything,my phone wifi was on but no good result
pls could you help with this heart breaking problem.my phone is samsung DUOS,Android 4.1.2
 

tunde

Member
Licensed User
Longtime User
Are you running the app on two devices? Have you clicked on Discover button on both of them?

Thanks Erel,
Yes I did ,i used two Phones and they are both Samsung and i also tried to peer with a Wifi Module still no result.I was able to scan for Wifi Devices using
ABwifi library but i dont know how to use the Library in connecting with a particular Wifi SSID.This would have been my second option because it scans and displays the available Wifi SSIDS.pls your help is needed and am also running out of Time with my project
 
Last edited:

Beja

Expert
Licensed User
Longtime User
Worked in less than 5 minutes..
Samsong DUOS / LG G Stylus ---> worked fine then...
(some time) one of the devices stuck in "Trying to connect...." .. but astonishingly it can receive the hello msg from the other
device, but the "Send hello Message..." is grayed out and I can't send.

I disabled the WiFi and used tethering.. I can browse the net on the other device but the WiFiDirect doesn't work at all.. it can not discover the other device.

WiFiDirect.png
 
Last edited:

fokkate

New Member
I have downloaded the zip file and found out that there is no java file . and i just saw .b4a and .b4s files. How can I get access to the project?
 

Attachments

  • 2016-02-29 (2).png
    2016-02-29 (2).png
    13.1 KB · Views: 497

clydebamford

Member
Licensed User
Longtime User
I have wifi direct working quite well but i get a timed request to "accept" or "cancel", is this android or b4a, im trying to connect without user intervention and this breaks it for me.
 
Top