NSD 发现服务 android

NSD discovering services android

任务说明: 我打算构建一个应用程序,它可以使用 NSD android 在我的网络中发现 ESP32 提供的服务,所以我实现了下面的代码 代码:

import android.net.DhcpInfo;
import  android.net.nsd.NsdManager;
import android.net.nsd.NsdManager.DiscoveryListener;
import android.util.Log;
import android.net.nsd.NsdServiceInfo;
import android.text.format.Formatter;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.net.InetAddress;
import  java.io.IOException;
import  java.net.UnknownHostException;
//import android.app.Activity;


import java.lang.reflect.Method;

 public  class MusicList extends org.qtproject.qt5.android.bindings.QtActivity {


     public static String getWifiName(Context context) throws IOException {
             WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
             final DhcpInfo dhcp = manager.getDhcpInfo();
             InetAddress localhost = null;
             //byte[] localHostIp = InetAddress.getLocalHost().getAddress();
             //String string = new String(localHostIp);
             //InetAddress in= null;
             //in = InetAddress.getByName(Formatter.formatIpAddress(dhcp.gateway));

             if (manager.isWifiEnabled()) {
                WifiInfo wifiInfo = manager.getConnectionInfo();
                if (wifiInfo != null) {
                   DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                   if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) {



                                              System.out.println(Formatter.formatIpAddress(dhcp.gateway));
                                              System.out.println(Formatter.formatIpAddress(dhcp.netmask));
                                              
                                              



                       return wifiInfo.getSSID()+"/"+Formatter.formatIpAddress(dhcp.gateway)+"/"+Formatter.formatIpAddress(dhcp.netmask);

                   }
                }
             }
            else {
                manager.setWifiEnabled(true);

                }
          return null;
         }

     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);
             }
         };
     }

 public void discoverServices(){
     nsdManager = Context.getSystemService(Context.NSD_SERVICE);
     nsdManager.discoverServices("_http._tcp.", NsdManager.PROTOCOL_DNS_SD, discoveryListener);



     }


}
 

但是我得到了以下错误:

D:\ProgQt\build-app-Android_for_armeabi_v7a_arm64_v8a_x86_x86_64_Clang_Qt_5_15_0_for_Android-Debug\android-build\src\org\qtproject\example\musiclist\MusicList.java:127: 错误: 找不到符号 discoveryListener = new NsdManager.DiscoveryListener() { ^

                 ^

你能帮忙吗?

你忘了输入变量的类型。如果应该是NsdManager.DiscoveryListener discoveryListener = new NsdManager.DiscoveryListener() {

您也会遇到与 nsdManager 相同的问题。在你的最后一个函数中,因为你使用了 discoveryListener 而没有定义它。当您从某个地方复制粘贴此代码时,您是否忘记复制粘贴 class 的实例变量?

总是在同一条路径上,我遇到了这个问题

:\ProgQt\build-app-Android_for_armeabi_v7a_arm64_v8a_x86_x86_64_Clang_Qt_5_15_0_for_Android-Debug\android-build\src\org\qtproject\example\musiclist\MusicList.java:188: 错误:无法从静态上下文中引用非静态变量 discoveryListener nsdManager.discoverServices("_http._tcp.", NsdManager.PROTOCOL_DNS_SD, discoveryListener);

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);
             }
         };
     }

 public static void discover(){
     System.out.println("discoverServices");

     nsdManager.discoverServices("_http._tcp.", NsdManager.PROTOCOL_DNS_SD, discoveryListener);


 }
     //nsdManager = (NsdManager)c.getSystemService(Context.NSD_SERVICE);






}