Android Tutorial Android Wifi-Direct Tutorial

http://www.b4x.com/android/forum/threads/android-wifi-direct-tutorial.30409/

Erel Administrator Staff Member Licensed User

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.

[IMG]

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
 

Attached Files:

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(35) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
posted @ 2016-02-01 00:00  张同光  阅读(67)  评论(0编辑  收藏  举报