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.basic4ppc.com/forum/addi...ates/30410-wifidirect-library.html#post176527
 

Attachments

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

elck

Member
Licensed User
Longtime User
Yes, and Yes (playmemories mobile), Erel
Of course I would like to make my own app, though!
 

blooddiamond

Member
Licensed User
Longtime User
hello,
Im trying to connect between android 4,2 and android 4,1 JB.. I have wifi on and trying to discover but still "no peered devices". What I'm doing wrong ?
 

blooddiamond

Member
Licensed User
Longtime User
Unfortunately no.. What reasons can it be ??

I downloaded program to both phones and turned on wifi.. Should i do something else ??
 

Adrian Jansen

Member
Licensed User
Longtime User
Airdrop?

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

Cheers,

You could of course just use an app like DropBox or GoogleDrive to transfer files. Just install the same app on both devices, then drop a file into one, and it will appear on the other ( via the Cloud, of course ).
 

bluedude

Well-Known Member
Licensed User
Longtime User
You could of course just use an app like DropBox or GoogleDrive to transfer files. Just install the same app on both devices, then drop a file into one, and it will appear on the other ( via the Cloud, of course ).

Sure we can but not what I wanted :)
 

Nenad Trickovic

Member
Licensed User
Longtime User
How can I check if device supports Direct WiFi in the code, and if this is the case, enable Direct WiFi without user intervention?
 

walterf25

Expert
Licensed User
Longtime User
Hi Erel, i have an LG Smart TV which supports wifidirect, i would like to make my own app to transfer files and maybe even movies through my galaxy s4, i have an app now from lg but is not too reliable as the connection drops everytime i'm trying to stream a movie from the phone, i downloaded your example and indeed the app discovers my lg smart tv i can see the model number by inserting a log line on this sub
B4X:
Sub Manager_PeersDiscovered (Success As Boolean, Devices As List)
    Log("peers: " & Success)
    If Success Then
        discoveredDevices = Devices
        Log(discoveredDevices.Get(0))    'I inserted this line to look at the paired device details
        lblDevicesText = Devices.Size
    Else
        lblDevicesText = "0"
    End If
    UpdateUI
End Sub

as i mentioned i can see the details of the tv which are this
Device: [TV][LG]60PH6700-UB
deviceAddress: 86:9c:a6:45:11:3d
primary type: 7-0050F204-1
secondary type: null
wps: 392
grpcapab: 64
devcapab: 52
status: 1
wfdInfo: WFD enabled: trueWFD DeviceInfo: 273
WFD CtrlPort: 7236
WFD MaxThroughput: 0
groupownerAddress: null
GOdeviceName: null
interfaceAddress:
peers: true

however when i click on the connect button it doesn't connect.
I can't really see any errors in the filtered or unfiltered logs that are related to this issue.
Do you have any idea as to whether this can even be done, I mean there are apps out there that are used to stream movies from the mobiles to smart TVs.

Any thoughts?

Thanks,
Walter
 

LucaMs

Expert
Licensed User
Longtime User
Erel, in the first post, you wrote:

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

I think you mean that the problems are in the OS, not in the B4A library, right?

My phone has S.O. 4.0.4. I created a small app (Eclipse) following an example on developer.android.com site and it worked fine (simple file sharing)

You are referring to some specific problem?
 

LucaMs

Expert
Licensed User
Longtime User
Is the ConnectionChanged event raised after you call Connect?


Ehm... i found now the project.. it is dated March 3 2013, my first steps in this "world".

Thanks to B4A, I have abandoned Eclipse ;)

When I opened it, it was "supposed" to update itself.

Now I have a compile error and I do not know if I'll lose my head to make things alright. I prefer to use B4A :D:p:D

P.S. compile error? uhm i dubt.

Errors occurred during the build.
Errors running builder 'Android Resource Manager' on project 'WiFiDirectDemo'.
java.lang.NullPointerException

I do not even mean to, but it seems to me that the resource compiler does not work, that is something in the environment

Bye bye Eclipse
 
Last edited:

walterf25

Expert
Licensed User
Longtime User
Erel regarding my post #33, even though on your example it seems like there's no connection made, i can see on my TV a message comes up that says that it is connected, but the connectionchanged Event is not raised, can you help with this?

Thanks,
Walter
 

walterf25

Expert
Licensed User
Longtime User
There is nothing that can be done if ConnectionChanged event is not raised. It fails on a lower level. Have you checked the unfiltered logs?
Yes i checked both filtered and unfiltered logs, but as i said on my first post i don't see anything related to this issue.
 

anaylor01

Well-Known Member
Licensed User
Longtime User
Quick question. Could this be used to send commands to each other like say making moves on a chess game?
 
Top