网络服务发现 (NSD) 可让您的应用访问其他设备在本地网络上提供的服务。支持 NSD 的设备包括打印机、摄像头、HTTPS 服务器和其他移动设备。
NSD 实现了基于 DNS 的服务发现 (DNS-SD) 机制,该机制允许您的应用通过指定服务类型和提供所需类型服务的设备实例的名称来请求服务。Android 和其他移动平台都支持 DNS-SD。
将 NSD 添加到应用后,用户就可以识别本地网络中支持应用请求的服务的其他设备。这对于各种点对点应用(例如文件共享或多人游戏)非常有用。Android 的 NSD API 简化了实现此类功能所需的工作。
本课将向您介绍如何构建一个应用,使其可向本地网络广播其名称和连接信息,并扫描来自执行相同操作的其他应用的信息。最后,本课将介绍如何连接到在其他设备上运行的同一应用。
在网络上注册您的服务
注意:此步骤是可选的。如果您不想通过本地网络广播应用的服务,则可以跳至下一部分,即发现网络上的服务。
如需在本地网络上注册服务,请先创建一个 NsdServiceInfo
对象。此对象会提供网络上的其他设备在决定是否连接到您的服务时使用的信息。
Kotlin
fun registerService(port: Int) { // Create the NsdServiceInfo object, and populate it. val serviceInfo = NsdServiceInfo().apply { // The name is subject to change based on conflicts // with other services advertised on the same network. serviceName = "NsdChat" serviceType = "_nsdchat._tcp" setPort(port) ... } }
Java
public void registerService(int port) { // Create the NsdServiceInfo object, and populate it. NsdServiceInfo serviceInfo = new NsdServiceInfo(); // The name is subject to change based on conflicts // with other services advertised on the same network. serviceInfo.setServiceName("NsdChat"); serviceInfo.setServiceType("_nsdchat._tcp"); serviceInfo.setPort(port); ... }
此代码段将服务名称设置为“NsdChat”。服务名称是实例名称:它是对网络上的其他设备可见的名称。网络上使用 NSD 查找本地服务的任何设备都可以看到该名称。请注意,网络上任何服务的名称都必须是唯一的,并且 Android 会自动处理冲突解决。如果网络上的两台设备都安装了 NsdChat 应用,其中一台设备会自动将服务名称更改为“NsdChat (1)”之类的内容。
第二个参数用于设置服务类型,指定应用使用的协议和传输层。语法为“_<protocol>._<transportlayer>”。在代码段中,该服务使用通过 TCP 运行的 HTTP 协议。提供打印机服务(例如网络打印机)的应用会将服务类型设置为“_ipp._tcp”。
注意 :国际编号分配机构 (IANA) 管理着服务发现协议(如 NSD 和 Bonjour)所使用的服务类型的集中式权威列表。您可以从 IANA 服务名称和端口号列表下载该列表。如果您打算使用新的服务类型,则应填写 IANA 端口和服务注册表单进行预留。
为服务设置端口时,请避免对其进行硬编码,因为这会与其他应用发生冲突。例如,假设您的应用始终使用端口 1337,这可能会与使用同一端口的其他已安装应用发生冲突。请改用设备的下一个可用端口。由于此信息通过服务广播提供给其他应用,因此在编译时无需让其他应用知道您的应用使用的端口。相反,应用可以在连接到您的服务之前通过您的服务广播获取此信息。
如果您使用套接字,下面说明了如何将套接字初始化为任何可用端口,只需将其设置为 0。
Kotlin
fun initializeServerSocket() { // Initialize a server socket on the next available port. serverSocket = ServerSocket(0).also { socket -> // Store the chosen port. mLocalPort = socket.localPort ... } }
Java
public void initializeServerSocket() { // Initialize a server socket on the next available port. serverSocket = new ServerSocket(0); // Store the chosen port. localPort = serverSocket.getLocalPort(); ... }
您已定义 NsdServiceInfo
对象,现在需要实现 RegistrationListener
接口。此接口包含 Android 用于提醒您的应用服务注册和取消注册成功或失败的回调。
Kotlin
private val registrationListener = object : NsdManager.RegistrationListener { override fun onServiceRegistered(NsdServiceInfo: NsdServiceInfo) { // Save the service name. Android may have changed it in order to // resolve a conflict, so update the name you initially requested // with the name Android actually used. mServiceName = NsdServiceInfo.serviceName } override fun onRegistrationFailed(serviceInfo: NsdServiceInfo, errorCode: Int) { // Registration failed! Put debugging code here to determine why. } override fun onServiceUnregistered(arg0: NsdServiceInfo) { // Service has been unregistered. This only happens when you call // NsdManager.unregisterService() and pass in this listener. } override fun onUnregistrationFailed(serviceInfo: NsdServiceInfo, errorCode: Int) { // Unregistration failed. Put debugging code here to determine why. } }
Java
public void initializeRegistrationListener() { registrationListener = new NsdManager.RegistrationListener() { @Override public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) { // Save the service name. Android may have changed it in order to // resolve a conflict, so update the name you initially requested // with the name Android actually used. serviceName = NsdServiceInfo.getServiceName(); } @Override public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { // Registration failed! Put debugging code here to determine why. } @Override public void onServiceUnregistered(NsdServiceInfo arg0) { // Service has been unregistered. This only happens when you call // NsdManager.unregisterService() and pass in this listener. } @Override public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) { // Unregistration failed. Put debugging code here to determine why. } }; }
现在,您已具备注册服务的所有条件。调用 registerService()
方法。
请注意,此方法是异步的,因此任何需要在服务注册后运行的代码都必须包含在 onServiceRegistered()
方法中。
Kotlin
fun registerService(port: Int) { // Create the NsdServiceInfo object, and populate it. val serviceInfo = NsdServiceInfo().apply { // The name is subject to change based on conflicts // with other services advertised on the same network. serviceName = "NsdChat" serviceType = "_nsdchat._tcp" setPort(port) } nsdManager = (getSystemService(Context.NSD_SERVICE) as NsdManager).apply { registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener) } }
Java
public void registerService(int port) { NsdServiceInfo serviceInfo = new NsdServiceInfo(); serviceInfo.setServiceName("NsdChat"); serviceInfo.setServiceType("_http._tcp."); serviceInfo.setPort(port); nsdManager = Context.getSystemService(Context.NSD_SERVICE); nsdManager.registerService( serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener); }
发现网络上的服务
网络充满生机,从凶猛的网络打印机到温顺的网络摄像头,再到附近井字棋游戏玩家残酷而激烈的战斗。让您的应用看到这一充满活力的功能生态系统的关键是服务发现。您的应用需要监听网络上的服务广播,以查看哪些服务可用,并过滤掉任何应用不支持的服务。
与服务注册一样,服务发现包括两个步骤:设置具有相关回调的发现监听器,以及对 discoverServices()
进行单个异步 API 调用。
首先,实例化一个实现 NsdManager.DiscoveryListener
的匿名类。以下代码段展示了一个简单示例:
Kotlin
// Instantiate a new DiscoveryListener private val discoveryListener = object : NsdManager.DiscoveryListener { // Called as soon as service discovery begins. override fun onDiscoveryStarted(regType: String) { Log.d(TAG, "Service discovery started") } override fun onServiceFound(service: NsdServiceInfo) { // A service was found! Do something with it. Log.d(TAG, "Service discovery success$service") when { service.serviceType != SERVICE_TYPE -> // Service type is the string containing the protocol and // transport layer for this service. Log.d(TAG, "Unknown Service Type: ${service.serviceType}") service.serviceName == mServiceName -> // The name of the service tells the user what they'd be // connecting to. It could be "Bob's Chat App". Log.d(TAG, "Same machine: $mServiceName") service.serviceName.contains("NsdChat") -> nsdManager.resolveService(service, resolveListener) } } override fun onServiceLost(service: NsdServiceInfo) { // When the network service is no longer available. // Internal bookkeeping code goes here. Log.e(TAG, "service lost: $service") } override fun onDiscoveryStopped(serviceType: String) { Log.i(TAG, "Discovery stopped: $serviceType") } override fun onStartDiscoveryFailed(serviceType: String, errorCode: Int) { Log.e(TAG, "Discovery failed: Error code:$errorCode") nsdManager.stopServiceDiscovery(this) } override fun onStopDiscoveryFailed(serviceType: String, errorCode: Int) { Log.e(TAG, "Discovery failed: Error code:$errorCode") nsdManager.stopServiceDiscovery(this) } }
Java
public void initializeDiscoveryListener() { // Instantiate a new DiscoveryListener discoveryListener = new NsdManager.DiscoveryListener() { // Called as soon as service discovery begins. @Override public void onDiscoveryStarted(String regType) { Log.d(TAG, "Service discovery started"); } @Override public void onServiceFound(NsdServiceInfo service) { // A service was found! Do something with it. Log.d(TAG, "Service discovery success" + service); if (!service.getServiceType().equals(SERVICE_TYPE)) { // Service type is the string containing the protocol and // transport layer for this service. Log.d(TAG, "Unknown Service Type: " + service.getServiceType()); } else if (service.getServiceName().equals(serviceName)) { // The name of the service tells the user what they'd be // connecting to. It could be "Bob's Chat App". Log.d(TAG, "Same machine: " + serviceName); } else if (service.getServiceName().contains("NsdChat")){ nsdManager.resolveService(service, resolveListener); } } @Override public void onServiceLost(NsdServiceInfo service) { // When the network service is no longer available. // Internal bookkeeping code goes here. Log.e(TAG, "service lost: " + service); } @Override public void onDiscoveryStopped(String serviceType) { Log.i(TAG, "Discovery stopped: " + serviceType); } @Override public void onStartDiscoveryFailed(String serviceType, int errorCode) { Log.e(TAG, "Discovery failed: Error code:" + errorCode); nsdManager.stopServiceDiscovery(this); } @Override public void onStopDiscoveryFailed(String serviceType, int errorCode) { Log.e(TAG, "Discovery failed: Error code:" + errorCode); nsdManager.stopServiceDiscovery(this); } }; }
NSD API 使用此接口中的方法在发现启动、失败以及发现和丢失服务(丢失表示“不再可用”)时通知您的应用。请注意,此代码段会在找到服务时执行多次检查。
- 系统会将所找到服务的服务名称与本地服务的服务名称进行比较,以确定设备是否刚刚接收了自己的广播(有效)。
- 检查服务类型,以验证服务类型是应用可以连接的服务类型。
- 系统会检查服务名称,以验证是否连接到了正确的应用。
检查服务名称并非总是必要操作,并且仅在您想要连接到特定应用时才需要检查。例如,应用可能只希望连接到在其他设备上运行的自身的实例。但是,如果应用想要连接到网络打印机,只要看到服务类型为“_ipp._tcp”就足够了。
设置监听器后,调用 discoverServices()
,传入应用应查找的服务类型、要使用的发现协议以及您刚刚创建的监听器。
Kotlin
nsdManager.discoverServices(SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, discoveryListener)
Java
nsdManager.discoverServices( SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, discoveryListener);
连接到网络上的服务
当应用在网络上找到要连接的服务时,它必须首先使用 resolveService()
方法确定该服务的连接信息。实现 NsdManager.ResolveListener
以传入此方法,并使用它获取包含连接信息的 NsdServiceInfo
。
Kotlin
private val resolveListener = object : NsdManager.ResolveListener { override fun onResolveFailed(serviceInfo: NsdServiceInfo, errorCode: Int) { // Called when the resolve fails. Use the error code to debug. Log.e(TAG, "Resolve failed: $errorCode") } override fun onServiceResolved(serviceInfo: NsdServiceInfo) { Log.e(TAG, "Resolve Succeeded. $serviceInfo") if (serviceInfo.serviceName == mServiceName) { Log.d(TAG, "Same IP.") return } mService = serviceInfo val port: Int = serviceInfo.port val host: InetAddress = serviceInfo.host } }
Java
public void initializeResolveListener() { resolveListener = new NsdManager.ResolveListener() { @Override public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { // Called when the resolve fails. Use the error code to debug. Log.e(TAG, "Resolve failed: " + errorCode); } @Override public void onServiceResolved(NsdServiceInfo serviceInfo) { Log.e(TAG, "Resolve Succeeded. " + serviceInfo); if (serviceInfo.getServiceName().equals(serviceName)) { Log.d(TAG, "Same IP."); return; } mService = serviceInfo; int port = mService.getPort(); InetAddress host = mService.getHost(); } }; }
解析服务后,您的应用会收到详细的服务信息,包括 IP 地址和端口号。这些就是您创建与服务自己的网络连接所需的一切。
在应用关闭时取消注册您的服务
在应用的生命周期中,酌情启用和停用 NSD 功能非常重要。在应用关闭时将其取消注册,有助于防止其他应用认为其仍处于活动状态并尝试与其连接。此外,服务发现是一项成本较高的操作,应在父级 Activity 暂停时停止,并在 Activity 恢复时重新启用。替换主 activity 的生命周期方法,并插入代码以酌情启动和停止服务广播和发现。
Kotlin
// In your application's Activity override fun onPause() { nsdHelper?.tearDown() super.onPause() } override fun onResume() { super.onResume() nsdHelper?.apply { registerService(connection.localPort) discoverServices() } } override fun onDestroy() { nsdHelper?.tearDown() connection.tearDown() super.onDestroy() } // NsdHelper's tearDown method fun tearDown() { nsdManager.apply { unregisterService(registrationListener) stopServiceDiscovery(discoveryListener) } }
Java
// In your application's Activity @Override protected void onPause() { if (nsdHelper != null) { nsdHelper.tearDown(); } super.onPause(); } @Override protected void onResume() { super.onResume(); if (nsdHelper != null) { nsdHelper.registerService(connection.getLocalPort()); nsdHelper.discoverServices(); } } @Override protected void onDestroy() { nsdHelper.tearDown(); connection.tearDown(); super.onDestroy(); } // NsdHelper's tearDown method public void tearDown() { nsdManager.unregisterService(registrationListener); nsdManager.stopServiceDiscovery(discoveryListener); }