Android网络服务发现(NSD)协议的使用
Android的网络服务发现协议(NSD)能够用于在小范围的网络中发现邻近设备上的某个应用。这对于一些社交网络、多人游戏类的应用会很有帮助。
Android的NSD的用法大致上分为四种操作:
1. 注冊网络服务
2. 发现网络服务
3. 连接网络服务
4. 注销网络服务
使用NSD时一定要注意:
记得在Manifest中增加android.permission.INTERNET 权限,不然程序会崩溃。
一. 注冊网络服务
注冊网络服务须要两样东西: 网络服务的信息(NsdServiceInfo)和注冊事件监听器(NsdManager.RegistrationListener)
这两样东西齐全后就能够通过:NsdManager.registerService发放来注冊网络服务了。
实例代码例如以下:
public void registerService(View view) {// 注意:注冊网络服务时不要对端口进行硬编码,通过例如以下这样的方式为你的网络服务获取// 一个可用的端口号.int port = 0;try {ServerSocket sock = new ServerSocket(0);port = sock.getLocalPort();sock.close();} catch (Exception e) {Toast.makeText(getApplicationContext(), "can not set port", Toast.LENGTH_SHORT);}// 注冊网络服务的名称、类型、端口NsdServiceInfo nsdServiceInfo = new NsdServiceInfo();nsdServiceInfo.setServiceName("NSD_Test_Program");nsdServiceInfo.setServiceType("_http._tcp.");nsdServiceInfo.setPort(port);// 实现一个网络服务的注冊事件监听器。监听器的对象应该保存起来以便之后进行注销nsRegListener = new NsdManager.RegistrationListener() {@Overridepublic void onUnregistrationFailed(NsdServiceInfo arg0, int arg1) {Toast.makeText(getApplicationContext(), "Unregistration Failed", Toast.LENGTH_SHORT).show();}@Overridepublic void onServiceUnregistered(NsdServiceInfo arg0) {Toast.makeText(getApplicationContext(), "Service Unregistered", Toast.LENGTH_SHORT).show();}@Overridepublic void onServiceRegistered(NsdServiceInfo arg0) {Toast.makeText(getApplicationContext(), "Service Registered", Toast.LENGTH_SHORT).show();}@Overridepublic void onRegistrationFailed(NsdServiceInfo arg0, int arg1) {Toast.makeText(getApplicationContext(), "Registration Failed", Toast.LENGTH_SHORT).show();}};// 获取系统网络服务管理器,准备之后进行注冊NsdManager nsdManager = (NsdManager) getApplicationContext().getSystemService(Context.NSD_SERVICE);nsdManager.registerService(nsdServiceInfo, NsdManager.PROTOCOL_DNS_SD, nsRegListener); }
注意:registerService()方法是异步运行的,假设有一定要在服务注冊完成后才干运行的操作,请在onServiceResgistered事件中执这些操作。
二. 发现网络服务
要发现附近的网络服务须要定义一个网络服务发现时间监听器。代码例如以下:
public void discoverService(View view) {nsDicListener = new NsdManager.DiscoveryListener() {@Overridepublic void onStopDiscoveryFailed(String serviceType, int errorCode) {Toast.makeText(getApplicationContext(), "Stop Discovery Failed", Toast.LENGTH_SHORT).show();}@Overridepublic void onStartDiscoveryFailed(String serviceType, int errorCode) {Toast.makeText(getApplicationContext(),"Start Discovery Failed", Toast.LENGTH_SHORT).show();}@Overridepublic void onServiceLost(NsdServiceInfo serviceInfo) {Toast.makeText(getApplicationContext(), "Service Lost", Toast.LENGTH_SHORT).show();}@Overridepublic void onServiceFound(NsdServiceInfo serviceInfo) {// 发现网络服务时就会触发该事件// 能够通过switch或if获取那些你真正关心的服务Toast.makeText(getApplicationContext(), "Service Found", Toast.LENGTH_SHORT).show();}@Overridepublic void onDiscoveryStopped(String serviceType) {Toast.makeText(getApplicationContext(), "Discovery Stopped", Toast.LENGTH_SHORT).show();}@Overridepublic void onDiscoveryStarted(String serviceType) {Toast.makeText(getApplicationContext(), "Discovery Started", Toast.LENGTH_SHORT).show();}};NsdManager nsdManager = (NsdManager) getApplicationContext().getSystemService(Context.NSD_SERVICE);nsdManager.discoverServices("_http._tcp", NsdManager.PROTOCOL_DNS_SD,nsDicListener); }三. 连接网络服务
通过定义一个网络服务连接时间监听器来轮询解析到的网络服务。能够进一步获取该网络服务的地址和port然后决定是否进行连接,演示样例代码:
public void initResolveListener(View view) {nsResolveListener = new NsdManager.ResolveListener() {@Overridepublic void onServiceResolved(NsdServiceInfo arg0) {// 能够再这里获取对应网络服务的地址及port信息,然后决定是否要与之建立连接。// 之后就是一些socket操作了}@Overridepublic void onResolveFailed(NsdServiceInfo arg0, int arg1) {}}; }四. 注销网络服务
想要注销网络服务,应该事先保存和该网络服务有关的句柄。通过NsdManager.unregisterService和NsdManager.stopServiceDiscovery方法来注销网络服务。实例代码:
public void unregisterService(View view) {NsdManager nsdManager = (NsdManager) getApplicationContext().getSystemService(Context.NSD_SERVICE);nsdManager.stopServiceDiscovery(nsDicListener); // 关闭网络发现nsdManager.unregisterService(nsRegListener); // 注销网络服务 }假设转载请注明出处:http://blog.csdn.net/gophers
总结
以上是生活随笔为你收集整理的Android网络服务发现(NSD)协议的使用的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 2015-12-18 学习心得
- 下一篇: Android之ASD组件(一)