注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好。

原文链接:http://developer.android.com/training/connect-devices-wirelessly/nsd-wifi-direct.html


在这系列课程的第一节课中(博客链接:http://www.cnblogs.com/jdneo/p/3579936.html),向你展示了如何发现连接到本地网络上的服务。然而,使用Wi-Fi Peer-to-Peer(P2P)服务搜索会允许你直接发现附近设备的服务,而不需要连接到网络中。你也可以对外广播目前正在你的设备上运行的服务。这些功能帮助你和其它应用进行通信,甚至是当无法获取本地网络或者热点的情况下。

虽然此API所要达到的目的,和之前课程中所说的NSD API非常类似,但是实现它们的代码却是不同的。这节课将向你展示如何使用Wi-Fi P2P从其他设备发现可获得的服务。这节课假设你已经对Wi-Fi P2P的API有了一定的了解。


一). 配置清单文件

为了使用Wi-Fi P2P,需要在你的清单文件中添加CHANGE_WIFI_STATEACCESS_WIFI_STATEINTERNET权限。虽然Wi-Fi P2P不需要网络连接,但是它使用的是标准的Java套接字,而在Android中使用它们则需要声明对应的权限许可:

<manifest xmlns: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"/>
...

二). 添加一个本地服务

如果你正在提供一个本地服务,你需要为服务搜索注册它。一旦你的本地服务注册了,框架将会自动响应Peer所发起的服务搜索请求。

要创建一个本地服务:

  1. 创建一个WifiP2pServiceInfo对象;
  2. 用你的服务的信息来填充它;
  3. 调用addLocalService()来为服务搜索注册本地服务
     private void startRegistration() {
// Create a string map containing information about your service.
Map record = new HashMap();
record.put("listenport", String.valueOf(SERVER_PORT));
record.put("buddyname", "John Doe" + (int) (Math.random() * 1000));
record.put("available", "visible"); // Service information. Pass it an instance name, service type
// _protocol._transportlayer , and the map containing
// information other devices will want once they connect to this one.
WifiP2pDnsSdServiceInfo serviceInfo =
WifiP2pDnsSdServiceInfo.newInstance("_test", "_presence._tcp", record); // Add the local service, sending the service info, network channel,
// and listener that will be used to indicate success or failure of
// the request.
mManager.addLocalService(channel, serviceInfo, new ActionListener() {
@Override
public void onSuccess() {
// Command successful! Code isn't necessarily needed here,
// Unless you want to update the UI or add logging statements.
} @Override
public void onFailure(int arg0) {
// Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY
}
});
}

二). 发现附近的服务

Android会使用回调函数来通知你的应用可以获取的服务,所以要做的第一件事情就是配置它们。创建一个WifiP2pManager.DnsSdTxtRecordListener来监听进入的记录。这个记录作为一个可选项被广播至其它设备。当有一个记录进来了,可以将设备地址以及其它你想要的其它信息拷贝到一个当前方法之外的一个数据结构中,这样你可以在之后的某一个阶段去访问它。下面的例子假设收到的记录包含有一个“buddyname”字段,用以用户身份识别。

final HashMap<String, String> buddies = new HashMap<String, String>();
...
private void discoverService() {
DnsSdTxtRecordListener txtListener = new DnsSdTxtRecordListener() {
@Override
/* Callback includes:
* fullDomain: full domain name: e.g "printer._ipp._tcp.local."
* record: TXT record dta as a map of key/value pairs.
* device: The device running the advertised service.
*/ public void onDnsSdTxtRecordAvailable(
String fullDomain, Map record, WifiP2pDevice device) {
Log.d(TAG, "DnsSdTxtRecord available -" + record.toString());
buddies.put(device.deviceAddress, record.get("buddyname"));
}
};
...
}

要获取服务信息,创建一个WifiP2pManager.DnsSdServiceResponseListener。它接收了实际的描述信息和连接信息。上述代码实现了一个Map对象来将“buddy name”和设备地址配对在一起。服务响应监听器会使用它将DNS记录和对应的服务信息连接到一起。一旦这两个监听器都实现了,使用setDnsSdResponseListeners()方法将它们添加到WifiP2pManager中。

private void discoverService() {
... DnsSdServiceResponseListener servListener = new DnsSdServiceResponseListener() {
@Override
public void onDnsSdServiceAvailable(String instanceName, String registrationType,
WifiP2pDevice resourceType) { // Update the device name with the human-friendly version from
// the DnsTxtRecord, assuming one arrived.
resourceType.deviceName = buddies
.containsKey(resourceType.deviceAddress) ? buddies
.get(resourceType.deviceAddress) : resourceType.deviceName; // Add to the custom adapter defined specifically for showing
// wifi devices.
WiFiDirectServicesList fragment = (WiFiDirectServicesList) getFragmentManager()
.findFragmentById(R.id.frag_peerlist);
WiFiDevicesAdapter adapter = ((WiFiDevicesAdapter) fragment
.getListAdapter()); adapter.add(resourceType);
adapter.notifyDataSetChanged();
Log.d(TAG, "onBonjourServiceAvailable " + instanceName);
}
}; mManager.setDnsSdResponseListeners(channel, servListener, txtListener);
...
}

现在创建一个服务请求并且调用addServiceRequest()。该方法必须接受一个监听器参数来报告结果是成功的还是失败的。

        serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
mManager.addServiceRequest(channel,
serviceRequest,
new ActionListener() {
@Override
public void onSuccess() {
// Success!
} @Override
public void onFailure(int code) {
// Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY
}
});

最后,调用discoverServices()

        mManager.discoverServices(channel, new ActionListener() {

            @Override
public void onSuccess() {
// Success!
} @Override
public void onFailure(int code) {
// Command failed. Check for P2P_UNSUPPORTED, ERROR, or BUSY
if (code == WifiP2pManager.P2P_UNSUPPORTED) {
Log.d(TAG, "P2P isn't supported on this device.");
else if(...)
...
}
});

如果所有代码都运行正常,太好了,你已经做到了!如果你遇到了问题,记住你执行的异步调用会接受一个WifiP2pManager.ActionListener作为参数,然后它会提供给你回调函数来指明成果或失败。若要诊断问题所在,在onFailure()中添加调试代码。该方法提供的错误代码会提示问题产生的原因。下面是一些可能的错误值及它们的含义:

P2P_UNSUPPORTED

在运行程序的设备上不支持Wi-Fi P2P。

BUSY

系统太忙以致无法处理请求。

ERROR

由于一个内部错误导致操作失败。

【Android Developers Training】 77. 使用Wi-Fi P2P进行服务搜索的更多相关文章

  1. 【Android Developers Training】 76. 用Wi-Fi创建P2P连接

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  2. 【Android Developers Training】 74. 序言:通过无线连接设备

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. 【Android Developers Training】 75. 使用NSD

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  4. 【Android Developers Training】 9. 覆盖于布局之上的Action Bar

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  5. 【Android Developers Training】 8. 定义Action Bar风格

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  6. 【Android Developers Training】 7. 添加Action Buttons

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. 【Android Developers Training】 6. 配置Action Bar

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  8. 【Android Developers Training】 5. 序言:添加Action Bar

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  9. 【Android Developers Training】 4. 启动另一个Activity

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

随机推荐

  1. (jquery+ajax)省市区三级联动(封装和不封装两种方式)-----2017-05-14

    首先,要实现如下图效果, 1.要理清思路: 先做出三个下拉菜单----根据第一个下拉菜单的value值获取第二个下拉列表的内容,第三个同理. 2.用到的数据库表:Chinastates表 规律:根据国 ...

  2. Oracle 12C 新特性之非分区表转分区表online clause(不停业务+索引有效)

    12c以前非分区表需要转换为分区, 如果不停业务的话可以使用在线重定义,只有在表进行切换的时候会有短暂的锁表. 12c 中alter table online clause 实现了表上现有的索引有效, ...

  3. j2ee分布式架构 dubbo + springmvc + mybatis + ehcache + redis 分布式架构

    介绍 <modules>        <!-- jeesz 工具jar -->        <module>jeesz-utils</module> ...

  4. python3.5 安装lxml

    通过xpath 爬虫时,使用到了lxml,通过pip 安装lxml 报错"building 'lxml.etree' extension  building 'lxml.etree' ext ...

  5. 计蒜客蓝桥杯模拟赛五J. 程序设计:放置守卫

    在一张 n 行 m 列的方格地图上放置一些守卫,每个守卫能守护上.左.右三个方向上相邻的方格和自己所在的方格.如下图,红色的方格放置守卫,绿色的方格为该守卫守护的区域. 现在要求在地图上放置若干个守卫 ...

  6. Opencv在linux下安装

    Opencv in Linux These steps have been tested for Ubuntu 10.04 but should work with other distros as ...

  7. javaWeb学习总结(4)- HTML 关于head中的<meta>标签

    关于<meta> 标签 <meta>标签出现在网页的标题部分,这些信息并不会出现在浏览器页面的显示之中,只会显示在源代码中.也就是在...当中. 主要用途是设置网页语言的编码方 ...

  8. Lua学习(1)——table

    table类型实现了“关联数组”.“关联数组”是一种具有特殊索引方式的数组.不仅可以通过证书来索引它,还可以使用字符串或其他类型(除了nil)来索引它.table是Lua中主要的数据结构机制(事实也是 ...

  9. A function to help graphical model checks of lm and ANOVA(转)

    As always a more colourful version of this post is available on rpubs. Even if LM are very simple mo ...

  10. 移动端使用rem同时适应安卓ios手机原理解析,移动端响应式开发

    rem单位大家可能已经很熟悉,rem是随着html的字体大小来显示代表宽度的方法,我们怎样进行移动端响应式开发呢 浏览器默认的字体大小为16px 及1rem 等于 16px 如果我们想要使1rem等于 ...