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

相关文章

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的

基于.NET编写工具类解决JSON乱码问题

《基于.NET编写工具类解决JSON乱码问题》在开发过程中,我们经常会遇到JSON数据处理的问题,尤其是在数据传输和解析过程中,很容易出现编码错误导致的乱码问题,下面我们就来编写一个.NET工具类来解... 目录问题背景核心原理工具类实现使用示例总结在开发过程中,我们经常会遇到jsON数据处理的问题,尤其是

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服