Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6660468
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类: Android平台

2015-11-07 22:22:24



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:
 

Attached Files:

  • File size: 8.7 KB Views: 7,172
,
阅读(684) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~