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

LucaMs

Expert
Licensed User
Longtime User
I apologize in advance for the question asked without reading everything.

it is possible that the app accept connections automatically, without user intervention?

I was thinking of an app in which the user activates a function of listening and the app analyzes the first data received and then decides whether to continue the connection.
 

scrat

Active Member
Licensed User
Longtime User
The library works well, thanks.

Just 2 problems:
How to disconnect from a group to connect to another.
I have to manually delete the connection in the wifi parameters to be able to reconnect

When I use manager.GetCurrentStatus I have this error
B4X:
java.lang.Exception: Sub manager_connectionchanged signature does not match expected signature.
and the subis
B4X:
Sub Manager_ConnectionChanged(Connected As Boolean, GroupOwnerIp As String)


thank you
 

scrat

Active Member
Licensed User
Longtime User
About the first problem, what happens when you call DiscoverPeers again?
the devices list is refreshed noting more.

the scenario
Three devices "A"," B"," C" in the same room
After a Discover "A" see "B" and "C", "B" see "A" and "C" etc ...
"A" connects to "B" with "B" Mac adress and exchange something.
"A" wants to disconnect from "B" and connect to "C" in a new "AC" group (without B inside)
Howto disconnect and remove "AB" group ?

In the api doc i see removeGroup and createGroup methods.
these methods may be the solution?
 

scrat

Active Member
Licensed User
Longtime User
Nothing.
Device A is always connected to B

before connection with C I try to disconnect

B4X:
Public Sub Disconnect
If client.IsInitialized Then client.Close
If server.IsInitialized Then server.Close
manager.CancelConnections
End Sub

Other problem
I close and kill the app on A and B
I restart app on A et B
I try to connect to C from A and the connection is alway on A and B
 

scrat

Active Member
Licensed User
Longtime User
Yes it works afer close server, client and redim wifimanager

B4X:
Public Sub Init
    If server.IsInitialized Then server.Close
    If client.IsInitialized Then client.Close
    Dim server As ServerSocket
    Dim client As Socket
    Dim manager As WifiManager
    server.Initialize(port, "server")
    manager.Initialize("manager")
    UpdateUI
End Sub

Yes it works.
But I must wait a long time before reconnecting (~5")
If I connect just after WifiManager_ConnectionChanged nothing works and I must cut wifi or reboot.
 

antonomase

Active Member
Licensed User
Longtime User
Hi,

I have 2 phones A and B and 2 tablets X and Y, all in the same room. The phones are used as remote controls of the tablets.
I want to connect the phone A on the tablet X and the phone B on tablet Y.

In my App on the phones, I want to display the Mac adress of the discovered devices.

Question 1: In the device list set by the method Manager_PeersDiscovered (Success As Boolean, Devices As List), how to get the deviceAddress of each discovered device ? What type of object is in the ArrayList named Devices ?
Found : Devices is an Array of WifiDevices (need to read file WifiDirect.xml to find it)
But : Why having an object which seems to have a lot of properties and just two getters (name et MacAdddress) ?

Question 2 : On each tablet, how to display the mac Address of the wifi so the user can see which phone must be connected to the tablet ?
Found : using Library ABWifi
But : on each the device, the mac address with ABWifi begins witch 5C and with Wifi direct begins with 5E (the other digits are OK). Why ?

Thanks
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why having an object which seems to have a lot of properties and just two getters
Not sure that I understand the question. Which property do you need to access?

About the mac address. I don't know how ABWifi is implemented however the values in the PeersDiscovered event come directly from the underlying layer.
 
Top