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

myriaddev

Active Member
Licensed User
Longtime 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\service1.java:169: package android.net.wifi.p2p does not exist
_dev.setObject((android.net.wifi.p2p.WifiP2pDevice)(_discovereddevices.Get((int)(0))));
^
1 error

Please help. Jerry

** Updated reference to android.jar to level 14 and it worked! Thanks Erel
 
Last edited:

bluedude

Well-Known Member
Licensed User
Longtime User
Airdrop?

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

Cheers,
 

bluedude

Well-Known Member
Licensed User
Longtime User
WOuld you happen to have a file sample? Not sure how to write Async streams into a file like a bitmap.

Cheers,
 

Mahares

Expert
Licensed User
Longtime 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.
 

awama

Active Member
Licensed User
Longtime 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 (127.0.0.1)

The Button "Send Hello Message" has no effect.

Is the reason the mentioned OS 4.0.4?
 

Erel

B4X founder
Staff member
Licensed User
Longtime 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.:
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.)

Troubleshooting:
- Turn off and on the Wifi.
- Check the unfiltered logs for any messages.
 

luke2012

Well-Known Member
Licensed User
Longtime 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 ?
 

HansEman

Member
Licensed User
Longtime User
Guys,
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?
 

elck

Member
Licensed User
Longtime 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: https://camera.developer.sony.com/pages/documents/view/?id=camera_api
Can you please put me on my way?

Thanks
Kees
 
Top