本文主要是介绍初学Android Wi-Fi Peer-to-Peer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
后悔当年没有把英语学好,工作后再也没有认真学习英语了,曾经的那点四六级词汇也忘得差不多了,
现在要用点英语的时候,头就大了!这几天在弄有关wifi的东东,今天上Android开发者官网(http://developer.android.com)
上逛了下,突然发现了Wi-FiP2P( Peer-to-Peer),简单地学习了下,把官网上的代码给code了下来。
使用的大概步骤:
- sdk版本设置及添加网络权限:
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- 在Activity中注册对应的广播
- 在广播中进行WIFI直连(WI-FI P2P)处理
public class WifiP2pActivity extends Activity {//WIFI直连管理操作类WifiP2pManagerprivate WifiP2pManager mManager;private Channel mChannel;//监听WIFI状态的BroadcastReceiverprivate BroadcastReceiver mReceiver;private IntentFilter mIntentFilter;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);mChannel = mManager.initialize(this, getMainLooper(), null);mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel,this);//初始化及设置IntentFiltermIntentFilter = new IntentFilter();mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);//P2P功能是否打开mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);//peer改变mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);//P2P连接状态改变mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);//设备设置改变//搜索或发现配对PeersmManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {@Overridepublic void onSuccess() {//查找成功}@Overridepublic void onFailure(int reasonCode) {// 查找失败}});}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();//注册广播registerReceiver(mReceiver, mIntentFilter);}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();//解除广播unregisterReceiver(mReceiver);}
}
/*** A BroadcastReceiver that notifies of important Wi-Fi p2p events.*/
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {private WifiP2pManager mManager;private Channel mChannel;private WifiP2pActivity activity;private PeerListListener mPeerListListener;public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,WifiP2pActivity activity) {super();this.mManager = manager;this.mChannel = channel;this.activity = activity;}@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {// Wifi P2P is enabled} else {// Wi-Fi P2P is not enabled}} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {// Call WifiP2pManager.requestPeers() to get a list of current peers// discoverPeers查找peer成功可失败,会监听WIFI_P2P_PEERS_CHANGED_ACTIONif (mManager != null) {mManager.requestPeers(mChannel, mPeerListListener);}} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {// Respond to new connection or disconnections} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {// Respond to this device's wifi state changing}mPeerListListener = new PeerListListener() {@SuppressWarnings("unchecked")@Overridepublic void onPeersAvailable(WifiP2pDeviceList arg0) {// TODO Auto-generated method stubList<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();peers.addAll((Collection<? extends WifiP2pDevice>) arg0);if (peers.size() > 0) {// 发现有peerWifiP2pDevice device = peers.get(0);connectPeer(device);}}};}
//进行连接操作private void connectPeer(WifiP2pDevice device) {WifiP2pConfig config = new WifiP2pConfig();config.deviceAddress = device.deviceAddress;mManager.connect(mChannel, config, new ActionListener() {@Overridepublic void onSuccess() {// 连接成功}@Overridepublic void onFailure(int reason) {// 连接失败}});}
}
下面部分是来自http://www.cnblogs.com/rayray/p/3857722.html
Wi-Fi Direct与ad-hoc模式有哪些不同?
Wi-Fi Direct将提供一个强大的发现功能,使其更易于使用。Wi-Fi Direct结合了Wi-Fi技术的若干重要创新,如更高的数据速率、企业管理能力、WMM? Quality of Service模式,以及点到点连接的电源管理协议。
http://wifinetnews.com/archives/2009/10/wifi_direct_peering.html
两者在安全性上有所不同,Wi-Fi Direct采用WPA2(Wi-Fi Protection Access Version 2)加密,在Windows ad-hoc网络下,最高安全支持为WEP。不过Windows 7亦支持WPA2。
DIGITIMES中文网 原文网址: Wi-Fi Direct 10大问答http://gb-www.digitimes.com.tw/tw/dt/n/shwnws.asp?id=0000205310_A224IE390EPO7S191B4CM#ixzz1iDK5ewPn
两者在安全性上有所不同,Wi-Fi Direct采用WPA2(Wi-Fi Protection Access Version 2)加密,在Windows ad-hoc网络下,最高安全支持为WEP。不过Windows 7亦支持WPA2
这篇关于初学Android Wi-Fi Peer-to-Peer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!