android.net.wifi.p2p package API

2024-04-19 22:32
文章标签 android wifi api net package p2p

本文主要是介绍android.net.wifi.p2p package API,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ref links:

api document: http://developer.android.com/reference/android/net/wifi/p2p/package-summary.html

demo: http://developer.android.com/resources/samples/WiFiDirectDemo/index.html


Overview

API package是用于create connection with wifi direct协议(http://en.wikipedia.org/wiki/Wi-Fi_Direct)

 

使用该API,必须是android 4.0 (level 14) or later,而且支持wifi directSamsung Nexus是第一部符合条件的手机。

 


package最重要的classWifiP2pManager,用它主要做下列事情:

1.     Initialize your application for P2P connectionsby calling initialize()

2.     Discover nearby devices by calling discoverPeers() and requestPeers()

3.     Start a P2P connection by calling connect()

4.     还有其他功能:如disconnect P2P connection,获取connection info,创建groupget group info(见下面)

 

Listeners

package几乎全部都是使用异步消息机制,因此你需要implement下面listener interfaces

1.     WifiP2pManager.ActionListener: WifiP2pManager5个方法(cancelConnect, connect, createGroup, discoverPeers,removeGroup)都要用该listener作为参数 for receiving callback

 

listener2callback方法onSuccess” and “onFailure(int)”,分别作为success and failurecallback

 

onFailure(int)的参数值有3个:ERROR,P2P_UNSUPPORTED or BUSY

 

2.     WifiP2pManager.ChannelListener: WifiP2pManagerinitialize方法用该listener作为参数 for receivingcallback.

 

listener1个方法onChannelDisconnected(),作为wifi directchannel disconnect时的callback

 

3.     WifiP2pManager.PeerListListener: WifiP2pManagerrequestPeers方法用该listener作为参数 for receivingcallback.

 

listener1个方法onPeersAvailable(WifiP2pDeviceList peers)作为当调用requestPeers来获取peer list成功时的callback.

 

4.     WifiP2pManager.ConnectionInfoListener: WifiP2pManagerrequestConnectionInfo方法用该listener作为参数 for receivingcallback.

 

listener1个方法onConnectionInfoAvailable(WifiP2pInfo info),作为当调用requestConnectionInfo来获取requestedconnection info成功时的callback.

 

5.     WifiP2pManager.GroupInfoListener: WifiP2pManagerrequestGroupInfo方法用该listener作为参数 for receivingcallback.

 

listener1个方法onGroupInfoAvailable(WifiP2pGroupgroup),作为当调用requestGroupInfo来获取requested group info成功时的callback.

 

 

WifiP2pManagerclass

关键方法:

l initialize(Context srcContext, Looper srcLooper,WifiP2pManager.ChannelListener listener)

 

用来initialize wifi,把本android app注册到wifi framwork里,并返回wifi frameworkchannel。在调用其他方法之前必须首先调用它

 

该方法的返回值是一个WifiP2pManager.Channelobject,它表示wifi framework channelWifiManager的其他方法在调用时都会用该channel作为参数!

 

l discoverPeers(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)

 

Initiate peerdiscovery.调用该方法会scan for available Wi-Fi peers

 

The functioncall immediately returns after sending a discovery request to the framework,因为是异步消息机制. The application is notified of a success or failure to initiatediscovery through listener callbacks onSuccess() or onFailure(int).

 

The discoveryremains active until a connection is initiated or a p2p group is formed. Register for WIFI_P2P_PEERS_CHANGED_ACTION intent to determinewhen the framework notifies of a change as peers are discovered.

 

在收到WIFI_P2P_PEERS_CHANGED_ACTIONintent后,你就可以调用requestPeers(WifiP2pManager.Channel,WifiP2pManager.PeerListListener)来获取peer list!!

 

l requestPeers(WifiP2pManager.Channel c, WifiP2pManager.PeerListListener listener)

 

sendrequest to获取current list of peers。返回的peer list要在参数PeerListListener里的onPeersAvailable(WifiP2pDeviceList peers)方法里获取

 

l connect (WifiP2pManager.Channel c, WifiP2pConfig config,WifiP2pManager.ActionListener listener)

 

建立p2pconnection to a device with the specified configuration

 

Theapplication is notified of a success or failure to initiate connect throughlistener callbacks onSuccess() or onFailure(int).

 

Register for WIFI_P2P_CONNECTION_CHANGED_ACTION intent todetermine when the framework notifies of a change in connectivity.

 

If the currentdevice is not part of a p2p group, a connect request initiates a groupnegotiation with the peer.

 

If thecurrent device is part of an existing p2p group or has created a p2p group withcreateGroup(WifiP2pManager.Channel, WifiP2pManager.ActionListener), aninvitation to join the group is sent to the peer device.

 

l requestConnectionInfo(WifiP2pManager.Channel c, WifiP2pManager.ConnectionInfoListenerlistener)

 

sendrequest to获取currentconnection info。返回的connection info要在参数ConnectionInfoListener里的onConnectionInfoAvailable(WifiP2pInfo info)方法里获取。

 

l cancelConnect(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)

 

disconnect current connection

 

l createGroup(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)

 

Create a p2p groupwith the current device as the group owner.This essentially creates an access point that can acceptconnections from legacy clients as well as other p2p devices.

 

Note: This function would normally not be used unless thecurrent device needs to form a p2p connection with a legacy client

 

Forcommunication with legacy Wi-Fi devices that do not support p2p, an app cancreate a group usingcreateGroup(WifiP2pManager.Channel, WifiP2pManager.ActionListener)which creates an access point whose details can be fetched withrequestGroupInfo(WifiP2pManager.Channel,WifiP2pManager.GroupInfoListener).

 

l requestGroupInfo(WifiP2pManager.Channel c, WifiP2pManager.GroupInfoListenerlistener)

 

l removeGroup(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)

 

 

Wifi Direct BroadcastReceiver

APP要自己定义一个wifi direct broadcastReceiver (extends android.content.BroadcastReceiver),并register it。该receiver用来接收来自wifi framework的消息/意图通知 (event/intent notification)

 

对于wifi direct p2p,有下面4event/intentnotification我们需要在自定义的wifi direct broadcastReceiver里接收注意,这4event/intent是定义在WifiP2pManager class

l WIFI_P2P_PEERS_CHANGED_ACTION:接收到的消息/意图是available peerlist发生了变化。

 

l WIFI_P2P_STATE_CHANGED_ACTION:接收到的消息/意图是Wi-Fi p2pstate发生变化。State2种:enable and disable

 

l WIFI_P2P_CONNECTION_CHANGED_ACTION:接收到的消息/意图是Wi-Fi p2pconnectivitystate发生改变. State应该是connected and disconnected

 

l WIFI_P2P_THIS_DEVICE_CHANGED_ACTION:接收到的消息/意图是正连接的device发生变化。

 

下面是define and register custom wifi direct broadcastreceiver的最简单例子:


//定义custom wifi direct broadcast receiver
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {private WifiP2pManager manager;private Channel channel;public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel) {super();this.manager = manager;this.channel = channel;}//当wifi framework broadcast 相关的intent时,就会callback 该onReceive方法!!@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();  //获取意图if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { //意图为WIFI P2P State发生变化int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); //get WIFI P2P Stateif (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { //WIFI P2P State为enable...} else { //WIFI P2P State为disable...}} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) { //意图为WIFI P2P current peer list发生变化if (manager != null) {manager.requestPeers(channel, peerListListener);  //send request to get current peers list}} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) { //意图为WIFI P2P connection state发生变化if (manager == null) {return;}NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);  //获取wifi p2p connection stateif (networkInfo.isConnected()) { //WIFI P2P connection state is "connected"manager.requestConnectionInfo(channel, connectionInfoListener);} else { //WIFI P2P connection state is "disconnected"...}} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) { //意图为正连接的device发生变化发生变化WifiP2pDevice device=((WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));  //获取正连接的device....}}
}

//Register custom wifi direct broadcast receiver in your app activity
IntentFilter intentFilter = new IntentFilter();
//只filter和wifi p2p相关的4个intent,即表示只会收到这4个intent的broadcast message
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);WiFiDirectBroadcastReceiver receiver = new WiFiDirectBroadcastReceiver(manager, channel);
//register broadcast receiver        
registerReceiver(receiver, intentFilter);


 

Simple Flow

 

 

 

 

 

这篇关于android.net.wifi.p2p package API的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/918615

相关文章

使用SpringBoot创建一个RESTful API的详细步骤

《使用SpringBoot创建一个RESTfulAPI的详细步骤》使用Java的SpringBoot创建RESTfulAPI可以满足多种开发场景,它提供了快速开发、易于配置、可扩展、可维护的优点,尤... 目录一、创建 Spring Boot 项目二、创建控制器类(Controller Class)三、运行

SSID究竟是什么? WiFi网络名称及工作方式解析

《SSID究竟是什么?WiFi网络名称及工作方式解析》SID可以看作是无线网络的名称,类似于有线网络中的网络名称或者路由器的名称,在无线网络中,设备通过SSID来识别和连接到特定的无线网络... 当提到 Wi-Fi 网络时,就避不开「SSID」这个术语。简单来说,SSID 就是 Wi-Fi 网络的名称。比如

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

.NET利用C#字节流动态操作Excel文件

《.NET利用C#字节流动态操作Excel文件》在.NET开发中,通过字节流动态操作Excel文件提供了一种高效且灵活的方式处理数据,本文将演示如何在.NET平台使用C#通过字节流创建,读取,编辑及保... 目录用C#创建并保存Excel工作簿为字节流用C#通过字节流直接读取Excel文件数据用C#通过字节

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

如何在Visual Studio中调试.NET源码

今天偶然在看别人代码时,发现在他的代码里使用了Any判断List<T>是否为空。 我一般的做法是先判断是否为null,再判断Count。 看了一下Count的源码如下: 1 [__DynamicallyInvokable]2 public int Count3 {4 [__DynamicallyInvokable]5 get

2、PF-Net点云补全

2、PF-Net 点云补全 PF-Net论文链接:PF-Net PF-Net (Point Fractal Network for 3D Point Cloud Completion)是一种专门为三维点云补全设计的深度学习模型。点云补全实际上和图片补全是一个逻辑,都是采用GAN模型的思想来进行补全,在图片补全中,将部分像素点删除并且标记,然后卷积特征提取预测、判别器判别,来训练模型,生成的像