在NSD中使用网络服务信息时显示null

时间:2017-05-05 06:20:23

标签: android nsd

要使用NSDManager类发现服务列表,我已经使用google引用实现了一些代码,但是当我尝试发现服务时,我无法获取主机地址。 Discovery启动时显示为null。我已提及 this link ,以便发现连接到同一网络中的设备列表 this blog ,以便在连接到同一网络时获取所有服务。

我的方法是否正确,以获得连接到同一网络的服务/设备列表?

我尝试使用代码来发现列表

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv=(TextView)findViewById(R.id.mytext);
    mylist=new List<NsdManager.DiscoveryListener>() {
        @Override
        public int size() {
            return s;
        }

        @Override
        public boolean add(NsdManager.DiscoveryListener discoveryListener) {
            return true;
        }

        @Override
        public NsdManager.DiscoveryListener get(int i) {
            return mDiscoveryListener;
        }

        @Override
        public NsdManager.DiscoveryListener set(int i, NsdManager.DiscoveryListener discoveryListener) {
            return mDiscoveryListener;
        }


        @Override
        public NsdManager.DiscoveryListener remove(int i) {
            return mDiscoveryListener;
        }

    };
    nsdList=new ArrayList<>();
    nsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
    //broadcaster = LocalBroadcastManager.getInstance(mContext);
    registerService(port);

    initializeResolveListener();
    discoverServices();

}



 public void initializeDiscoveryListener() {
    mDiscoveryListener = new NsdManager.DiscoveryListener() {
        @Override
        public void onDiscoveryStarted(String regType) {
            Log.d(TAG, "Service discovery started");
        }

        @Override
        public void onServiceFound(NsdServiceInfo service) {
            Log.d(TAG, "Service discovery success : " + service);
            String serviceType = service.getServiceType();
            mylist.add(mDiscoveryListener);
            nsdList.add(service.getServiceName());

            s++;

            Log.d(TAG, "Service discovery success: " + service.getServiceName());


            boolean isOurService = serviceType.equals(SERVICE_TYPE) || serviceType.equals(SERVICE_TYPE);

            if (!isOurService) {
                Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
            } else if (service.getServiceName().equals(SERVICE_NAME)) {
                Log.d(TAG, "Same machine: " + SERVICE_NAME);
            } else if (service.getServiceName().contains(SERVICE_NAME)) {
                Log.d(TAG, "different machines. (" + service.getServiceName() + "-" +
                        SERVICE_NAME+ ")");
                nsdManager.resolveService(service, mResolveListener);

            }
        }

注册服务
public void registerService(int port) {
    NsdServiceInfo serviceInfo = new NsdServiceInfo();
    serviceInfo.setServiceName(SERVICE_NAME);
    serviceInfo.setServiceType(SERVICE_TYPE);
    serviceInfo.setPort(port);

    nsdManager.registerService(serviceInfo,
            NsdManager.PROTOCOL_DNS_SD,
            registrationListener);
}

我的决心听众

public void initializeResolveListener() {
    mResolveListener = new NsdManager.ResolveListener() {
        @Override
        public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
            Log.e(TAG, "Resolve failed" + errorCode);
        }

        @Override
        public void onServiceResolved(NsdServiceInfo serviceInfo) {
            Log.v(TAG, "Resolve Succeeded. " + serviceInfo);
            if (serviceInfo.getServiceName().equals(mService)) {
                Log.d(TAG, "Same IP.");
                return;
            }
            mService = serviceInfo;

            int port = mService.getPort();
            InetAddress host = mService.getHost();            }
    };
}

当我尝试执行代码时,它显示为null shows null

1 个答案:

答案 0 :(得分:1)

当您发现服务时,它只是一个PTR记录:

_http._tcp.local. type PTR, class IN, myService._http._tcp.local.

如果您需要主机/端口信息,您必须自行解决该记录。