本文译自:http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html

WiFi对等API(P2P)不需要连接到网络或热点(Android的WiFi P2P框架符合WiFi编程指导规范),就允许应用程序连接到附近的设备。WiFiP2P允许应用程序快速的查找附近的设备,并与其交互。在连接范围上超过了蓝牙的能力。

本文介绍如何使用WiFi P2P来查找和连接附近的设备。

建立应用程序权限

为了使用WiFi P2P,要在应用程序的清单文件中添加CHANGE_WIFI_STATEACCESS_WIFI_STATEINTERNET权限。WiFi P2P不要求互联网连接,但是它使用标准的Java套接字,它要求INTERNET权限。因此使用WiFi P2P需要下例权限。

<manifestxmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.nsdchat"
    ...

<uses-permission
        android:required="true"
        android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission
        android:required="true"
        android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission
        android:required="true"
        android:name="android.permission.INTERNET"/>
    ...

建立广播接收器和对等管理器

要使用WiFi P2P,就要监听广播意图,在确定事件发生时,告诉你的应用程序。在你的应用程序中,要实例化一个IntentFilter对象,并设置它来监听以下动作:

WIFI_P2P_STATE_CHANGED_ACTION

指示是否启用WiFi P2P。

WIFI_P2P_PEERS_CHANGED_ACTION

指示可用的对等列表已经发生了改变。

WIFI_P2P_CONNECTION_CHANGED_ACTION

指示WiFi P2P连接的状态已经发生改变。

WIFI_P2P_THIS_DEVICE_CHANGED_ACTION

指示设备的配置明细已经发生改变。

privatefinalIntentFilterintentFilter =newIntentFilter();
...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

//  Indicates achange in the Wi-Fi P2P status.
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);

// Indicates a change inthe list of available peers.
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);

// Indicates the state ofWi-Fi P2P connectivity has changed.
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);

// Indicates this device'sdetails have changed.
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

...
}

在onCreate()方法中,获取一个WifiP2pManager对象的实例,并调用它的initialize()方法。这个方法会返回一个WifiP2pManager.Channel对象,它会用于后续对WiFi P2P框架的连接。

@Override

Channel mChannel;

public void onCreate(Bundle savedInstanceState) {
    ....
    mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);
}

现在创建一个新的BroadcastReceiver类,用于监听系统的WiFi P2P状态的改变。在onReceive()方法中,添加处理上述每个P2P状态改变的条件。

@Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            // Determine if Wifi P2P mode is enabled or not, alert
            // the Activity.
            int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
            if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
               activity.setIsWifiP2pEnabled(true);
            } else {
               activity.setIsWifiP2pEnabled(false);
            }
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

// The peer list has changed!  We should probably do somethingabout
            // that.

} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {

// Connection state changed!  We should probably do somethingabout
            // that.

} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            DeviceListFragment fragment = (DeviceListFragment)activity.getFragmentManager()
                   .findFragmentById(R.id.frag_list);
            fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
                   WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));

}
    }

最后,在你的主Activity激活时,添加注册Intent过滤器和广播接收器的代码,并在Activity被挂起时,解除注册。做这件事的最好的地方是onResume()和onPause()方法

/** register the BroadcastReceiverwith the intent values to be matched */
    @Override
    public void onResume() {
        super.onResume();
        receiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
        registerReceiver(receiver, intentFilter);
    }

@Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

启动对等点发现功能

要使用Wi-Fi P2P来搜索附近的设备,就要调用discoverPeers()方法,这个方法需要以下参数:

WifiP2pManager.Channel:这个参数是在初始化对等管理器时获取,用于把应用程序连接到Wi-Fi的P2P框架中;

WifiP2pManager.ActionListener接口实现:系统会在处理成功或失败时调用该接口中对应的方法。

mManager.discoverPeers(mChannel,newWifiP2pManager.ActionListener(){

@Override
        public void onSuccess() {
            // Code for when the discovery initiation is successful goes here.
            // No services have actually been discovered yet, so this method
            // can often be left blank.  Code for peer discovery goes in the
            // onReceive method, detailed below.
        }

@Override
        public void onFailure(int reasonCode) {
            // Code for when the discovery initiation fails goes here.
            // Alert the user that something went wrong.
        }
});

记住,这只是启动对等点发现功能。DiscoverPeers()方法启动发现处理,然后立即返回。系统会通过调用你传入的第二参数中的对应的接口方法,通知你对等点发现处理是否启动成功。此外,发现处理会一直保持到连接被启动或P2P组被组建。

获取对等点的列表

现在要编写获取和处理对等点列表的代码。首先实现WifiP2pManager.PeerListener接口,这个接口会提供有关已经发现的Wi-Fi的P2P对等点的信息。下列代码演示这个处理:

privateList peers =newArrayList();
    ...

private PeerListListenerpeerListListener = new PeerListListener() {
        @Override
        public void onPeersAvailable(WifiP2pDeviceListpeerList) {

// Out with the old, in with the new.
            peers.clear();
            peers.addAll(peerList.getDeviceList());

// If an AdapterView is backed by this data, notify it
            // of the change.  For instance, if you have a ListView ofavailable
            // peers, trigger an update.
            ((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
            if (peers.size() == 0) {
                Log.d(WiFiDirectActivity.TAG, "No devices found");
                return;
            }
        }
    }

现在,修改你的广播接收器的onReceive()方法,当接收到带有WIFI_P2P_PEERS_CHANGED_ACTION操作的Intent时,调用requestPeers()方法,并把上面实现的监听器传递给requestPeers()方法。你可以把这个监听器作为广播接收器的构造器的参数来传递。

publicvoid onReceive(Context context,Intent intent){
    ...
    else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

// Requestavailable peers from the wifi p2p manager. This is an
        //asynchronous call and the calling activity is notified with a
        // callbackon PeerListListener.onPeersAvailable()
        if (mManager != null) {
            mManager.requestPeers(mChannel,peerListListener);
        }
        Log.d(WiFiDirectActivity.TAG, "P2P peerschanged");
    }...
}

这样,带有WIFI-P2P_PEERS_CHANGED_ACTION的Intent操作就会触发更新对等点列表的请求。

连接到对等点

为了连接到对等点,要创建一个新的WifiP2pConfig对象,并从代表你要连接的设备的WifiP2pDevice对象中复制数据到这个WifiP2pConfig对象中。然后调用connect()方法。

@Override
    public void connect() {
        // Pickingthe first device found on the network.
        WifiP2pDevice device = peers.get(0);

WifiP2pConfig config = new WifiP2pConfig();
        config.deviceAddress = device.deviceAddress;
        config.wps.setup = WpsInfo.PBC;

mManager.connect(mChannel, config, new ActionListener() {

@Override
            public void onSuccess() {
                // WiFiDirectBroadcastReceiver will notify us. Ignore for now.
            }

@Override
            public void onFailure(int reason) {
                Toast.makeText(WiFiDirectActivity.this, "Connect failed. Retry.",
                       Toast.LENGTH_SHORT).show();
            }
        });
    }

上述实现的WifiP2pManager.ActionListener接口只会通知你连接成功还是失败。要监听连接状态的变化,就好实现WifiP2pManager.ConnectionInfoListener()接口,这个接口中的onConnectionInfoAvailable()回调方法在连接状态变化时通知你。当有多个设备连接一个设备时,其中一个设备将会被指定为“组管理者”。

@Override
    public voidonConnectionInfoAvailable(final WifiP2pInfo info) {

//InetAddress from WifiP2pInfo struct.
        InetAddress groupOwnerAddress =info.groupOwnerAddress.getHostAddress());

// After thegroup negotiation, we can determine the group owner.
        if (info.groupFormed && info.isGroupOwner) {
            // Do whatever tasks are specific to the group owner.
            // One common case is creating a server thread and accepting
            // incoming connections.
        } else if (info.groupFormed) {
            // The other device acts as the client. In this case,
            // you'll want to create a client thread that connects to the group
            // owner.
        }
    }

现在,返回到广播接收器的onReceive()方法,修改对WIFI_P2P_CONNECTION_CHANGED_ACTION的Intent操作的监听。当收到这个Intent时,调用requestConnectionInfo()方法。这是一个异步调用,因此,结果会被你提供的连接信息监听器接收到。

...
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {

if (mManager == null) {
                return;
            }

NetworkInfo networkInfo = (NetworkInfo) intent
                   .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);

if (networkInfo.isConnected()) {

// We are connected with the other device, request connection
                // info to find group owner IP

mManager.requestConnectionInfo(mChannel, connectionListener);
            }
            ...

Android---用Wi-Fi来建立对等连接的更多相关文章

  1. java.io.IOException: 您的主机中的软件中止了一个已建立的连接解决办法

    问题现象和http://hi.baidu.com/cara_cloud/item/193a3ee327546d395a2d64be描述的一样,就是在eclipse的console栏中一直显示java. ...

  2. 【转】android蓝牙开发 蓝牙设备的查找和连接

    1.  首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限 // 管理蓝牙设备的权限 <uses-permissionandroid:name="android. ...

  3. Android应用程序与SurfaceFlinger服务的连接过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7857163 前文在描述Android应用程序和 ...

  4. Android实现推送方式解决方案 - 长连接+心跳机制(MQTT协议)

    本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...

  5. 通过UDP建立TCP连接

    解释 通过UDP广播查询服务器的IP地址,然后再建立TCP点对点连接. 应用场景 在服务器IP未知时,并且已知服务器与客户端明确在一个局域网或者允许组播的子网下. 通过UDP发现服务器地址然后再进行T ...

  6. 使用wireshark抓包分析浏览器无法建立WebSocket连接的问题(server为Alchemy WebSockets组件)

    工作时使用了Websocket技术,在使用的过程中发现,浏览器(Chrome)升级后可能会导致Websocket不可用,更换浏览器后可以正常使用. 近日偶尔一次在本地调试,发现使用相同版本的Chrom ...

  7. telnet建立http连接获取网页HTML内容

    利用telnet可以与服务器建立http连接,获取网页,实现浏览器的功能.它对于需要对http header进行观察和测试到时候非常方便.因为浏览器看不到http header. 步骤如下: 1. 运 ...

  8. HTTP协议用的TCP但是只建立单向连接

    作者:IronTech链接:https://www.zhihu.com/question/20085992/answer/71742030来源:知乎著作权归作者所有,转载请联系作者获得授权. 下面的解 ...

  9. System.getProperty()引起的悲剧--您的主机中的软件中止了一个已建立的连接

    我已无法形容此刻我的心情.. 本来是已经写好的netty5的demo程序,server和client之间创建tcp长连接的..然后随便传点数据的简单demo..然后今天试了一下tcp粘包的例子,用到了 ...

随机推荐

  1. unity 基础之PhysicsManager

    原地址:http://www.cnblogs.com/alongu3d/p/3644725.html @by  广州小龙 1.Gravity(重力) 物理系统都是根据实际情况进行模拟的,由于Y值是往下 ...

  2. UL LI 布局 TAB 切换条

    web页面实现tab的功能有几种实现方式,下面是使用UL LI DIV方式实现的tab. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  3. [OJ] Single Number II

    LintCode 83. Single Number II (Medium) LeetCode 137. Single Number II (Medium) 以下算法的复杂度都是: 时间复杂度: O( ...

  4. Asynchronously with NSURLConnection

    NSString *urlAsString=@"http://www.apple.com"; NSURL *url=[NSURL URLWithString:urlAsString ...

  5. Oracle系列之视图

    涉及到表的处理请参看原表结构与数据  Oracle建表插数据等等 创建视图,把tb_Employee表sal<1000的雇员,映射到该视图( view) create or replace vi ...

  6. ADDED_TO_STAGE 多次被调用

    var mca:MovieClip=new MCA(); mca.addEventListener(Event.ADDED_TO_STAGE,addStageEvent); function addS ...

  7. MySQL sql_slave_skip_counter

    因为mysql的主从复制是逻辑复制,所以slave在apply relay log的过程中,经常会遇到错误,而参数sql_slave_skip_counter可以设置跳过多少个event,让slave ...

  8. PICK定理模板

    PICK定理: S=I+O/2-1 S为多边形面积,I多边形内部的格点,O是多边形边上的格点 其中边上格点求法: 假设两个点A(x1,y1),B(x2,y2) 线段AB间格点个数为gcd(abs(x1 ...

  9. Matlab工具箱安装体会

    总结有两点: 1.如需添加jar包等附加库,可在待安装工具箱下,新建一个java文件夹,并将jar包等文件存放在里面,然后执行以下操作: 1)Create or open your preferenc ...

  10. .net HTMLParser详细使用说明 强大的Filter类 解析HTML文档如此简单

    背景: HTMLParser原本是一个在sourceforge上的一个Java开源项目,使用这个Java类库可以用来线性地或嵌套地解析HTML文本.他的 功能强大和开源等特性吸引了大量Web信息提取的 ...