---
title: "Android 4.0 亮点之WI-FI Direct"
author: "地球小野花"
date: "2011-11-15T20:01:19Z"
tags: []
categories: ["移动开发"]
slug: "android-4-0-wi-fi-direct"
draft: false
lastmod: "2011-11-15T20:01:19Z"
id: 1260662
url: "https://android-sz.iteye.com/blog/1260662"
---

### Wi-Fi Direct

来自官网：

Android now supports Wi-Fi Direct for peer-to-peer (P2P) connections between Android-powered devices and other device types without a hotspot or Internet connection. The Android framework provides a set of Wi-Fi P2P APIs that allow you to discover and connect to other devices when each device supports Wi-Fi Direct, then communicate over a speedy connection across distances much longer than a Bluetooth connection.

 

 

英文不大好，大意是android 4.0 提供了点对点共享上网的api，换而言之。程序员可以通过该api实现支持 Wifi Driect的设备进行网络共享。（注： 现在市场上部分手机支持移动AP功能，即无限路由器与p2p 有区别）

 

看看怎样提供这些功能：

 

来自官网：

 

A new package, `android.net.wifi.p2p`, contains all the APIs for performing peer-to-peer connections with Wi-Fi. The primary class you need to work with is`WifiP2pManager`, which you can acquire by calling `getSystemService(WIFI_P2P_SERVICE)`. The `WifiP2pManager` includes APIs that allow you to:

- Initialize your application for P2P connections by calling `initialize()`
- Discover nearby devices by calling `discoverPeers()`
- Start a P2P connection by calling `connect()`
- And more

通过WifiP2pManager，实现P2P的初始化 initialize()，使用discoverPeers() 发现可链接的设备，最后调用connect()进行握手。（这个过程应该和蓝牙链接差不多）

链接后，就可以做很多想做的事情咯。（动动脑筋开发一些有用的应用，甩信是个很好的例子）

功欲善其事，必先利其器

来自官网：

 

Several other interfaces and classes are necessary as well, such as:

- The `WifiP2pManager.ActionListener` interface allows you to receive callbacks when an operation such as discovering peers or connecting to them succeeds or fails.
- `WifiP2pManager.PeerListListener` interface allows you to receive information about discovered peers. The callback provides a `WifiP2pDeviceList`, from which you can retrieve a `WifiP2pDevice` object for each device within range and get information such as the device name, address, device type, the WPS configurations the device supports, and more.
- The `WifiP2pManager.GroupInfoListener` interface allows you to receive information about a P2P group. The callback provides a `WifiP2pGroup` object, which provides group information such as the owner, the network name, and passphrase.
- `WifiP2pManager.ConnectionInfoListener` interface allows you to receive information about the current connection. The callback provides a `WifiP2pInfo` object, which has information such as whether a group has been formed and who is the group owner.

不做翻译了，就是其使用过程中必须用到的一些接口和方法

当然，”权利“不要忘记

来自官网：

 

In order to use the Wi-Fi P2P APIs, your app must request the following user permissions:

- `ACCESS_WIFI_STATE`
- `CHANGE_WIFI_STATE`
- `INTERNET` (although your app doesn’t technically connect to the Internet, communicating to Wi-Fi Direct peers with standard java sockets requires Internet permission).

 

最后一些广播和事件必须通知到系统的

来自官网：

 

- `WIFI_P2P_CONNECTION_CHANGED_ACTION`: The P2P connection state has changed. This carries `EXTRA_WIFI_P2P_INFO` with a `WifiP2pInfo` object and`EXTRA_NETWORK_INFO` with a `NetworkInfo` object.
- `WIFI_P2P_STATE_CHANGED_ACTION`: The P2P state has changed between enabled and disabled. It carries `EXTRA_WIFI_STATE` with either `WIFI_P2P_STATE_DISABLED`or `WIFI_P2P_STATE_ENABLED`
- `WIFI_P2P_PEERS_CHANGED_ACTION`: The list of peer devices has changed.
- `WIFI_P2P_THIS_DEVICE_CHANGED_ACTION`: The details for this device have changed.

 

详细指导怎么用： 

可以参考文档和demo。

See the `WifiP2pManager` documentation for more information. Also look at the [Wi-Fi Direct Demo](http://developer.android.com/resources/samples/WiFiDirectDemo/index.html) sample application.

 

最后附一些大能如何使用WIFI P2P：

andoird wifi 点对点连接笔记本的ad-hoc：[http://www.tourgz.com/bbs/thread-1183-1-1.html](http://www.tourgz.com/bbs/thread-1183-1-1.html)

 

android WiFi Direct：[http://v.youku.com/v\_show/id\_XMjQ3MzI3Mzg4.html](http://v.youku.com/v_show/id_XMjQ3MzI3Mzg4.html)

 

转载请注明出处,谢谢！