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,990

antonomase

Active Member
Licensed User
Longtime User
If I look into the variable "device" with the debugger, it seems to be an object with many properties. But just name and mac address are available with getters.
 

luke2012

Well-Known Member
Licensed User
Longtime User
@Erel,
I'm trying your demo and I have only one question (until now) :)

The accept connection message is raised only the first time, like bluetooth paring ?
 

luke2012

Well-Known Member
Licensed User
Longtime User
@Erel,
I'm trying to understand your tutorial and it is quite clear for me, except the GroupOwnerIp parameter that is passed in the _ConnectionChanged event.

Tell me if I wrong :
1) I understand that it's the server IP. This IP is used by the client to create the socket connection.
2) GroupOwnerIp can assume loopback IP address (127.0.0.1) or other network address.

2) I don't know which is the rule that set the GroupOwnerIp IP address.
 

luke2012

Well-Known Member
Licensed User
Longtime User
1) Yes.
2) The "server" device GroupOwnerIp address will always be 127.0.0.1. The server should use a ServerSocket to accept incoming connections.

So the ServerSocket can accept multiple connections from other devices ?
 

Gaver Powers

Member
Licensed User
Longtime User
Has anyone connected to a wifi enabled arduino using this yet?
 

F.JANSEN

New Member
Licensed User
Longtime User
Dear,


how do I see the name of my ” wi-fi direct” when I establish a connection? I can only see the device name found but not mine.


Best Regards,
Jansen
 

F.JANSEN

New Member
Licensed User
Longtime User
This is the configuration of Wi-Fi Direct from my Smartphone, and enables me to rename "Jansenx".
I'm trying to get this name in my application. Is this possible?

Screenshot_2015-02-02-14-44-47[1].png


Screenshot_2015-02-02-14-44-56[1].png
 

F.JANSEN

New Member
Licensed User
Longtime User
Really, I did not find API that returns the name of my direct wi-fi device ... Thanks!
 

ppgirl

Member
Licensed User
Longtime User
Hi Erel,

Help , Could you develop more/detail functions for wifi direct library?

I want to create the code from follow java code . but I can not find those struct...



  1. config.wps = wps;
  2. config.deviceAddress = mConnectingDevice.deviceAddress;
  3. config.groupOwnerIntent = WifiP2pConfig.MIN_GROUP_OWNER_INTENT;
  4. ...
  5. mWifiP2pManager.connect(mWifiP2pChannel, config, new ActionListener() {
 

ppgirl

Member
Licensed User
Longtime User
Dear Erel,

I want make a wifi display app , I need set Wifi to WFD mode and wait connecting.

There is sample code , I would move to a java project for whole function if B4A can not finish it.

private WifiManager n;
private WifiP2pManager t;
private WifiP2pManager.Channel u;
private Timer g = null;
....

this.n = ((WifiManager)getSystemService("wifi"));
if (this.n != null)
Log.d("",Get WifiP2pManager instance successfully");
this.t = ((WifiP2pManager)getSystemService("wifip2p"));
if (this.t != null)
Log.d("", "Get WifiP2pManager instance successfully");
this.u = this.t.initialize(this, getMainLooper(), null);
Log.d("", "WifiP2pManager initialization started");

WifiP2pWfdInfo localWifiP2pWfdInfo = new WifiP2pWfdInfo();
localWifiP2pWfdInfo.setWfdEnabled(true);
localWifiP2pWfdInfo.setDeviceType(1);
localWifiP2pWfdInfo.setSessionAvailable(true);
localWifiP2pWfdInfo.setControlPort(554);
localWifiP2pWfdInfo.setMaxThroughput(50);
this.t.setWFDInfo(this.u, localWifiP2pWfdInfo, new g(this));

......
 

BarryW

Active Member
Licensed User
Longtime User
Can this library send a file?

Photo, mp3, video or txt file?
 
Top