安卓 NSD : registration with null service type

标签 android zeroconf dns-sd nsd

注册的结果不能为空,这是我从logcat得到的,注册成功的回调。

registerService 46518
onServiceRegistered name: mytest, type: null, host: null, port: 0, txtRecord:    

一切都是空的,系统生成的端口 46518,类型,txtrecord。

以下代码来自official guide

private String mServiceName = "mytest";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        // Initialize a server socket on the next available port.
        ServerSocket mServerSocket = new ServerSocket(0);
        int mLocalPort = mServerSocket.getLocalPort();
        registerService(mLocalPort);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void registerService(int port) {
    Log.i(tag, "registerService " + 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(mServiceName);
    serviceInfo.setServiceType("_mytest._tcp");
    serviceInfo.setPort(port);
    serviceInfo.setAttribute("info", android.os.Build.MODEL);

    NsdManager mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
    mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}

NsdManager.RegistrationListener mRegistrationListener = 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.
        mServiceName = nsdServiceInfo.getServiceName();
        Log.i(tag, "onServiceRegistered " + nsdServiceInfo);
    }

    @Override
    public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
        // Registration failed!  Put debugging code here to determine why.
        Log.i(tag, "onRegistrationFailed code " + errorCode + "\n" + serviceInfo);
    }

    @Override
    public void onServiceUnregistered(NsdServiceInfo arg0) {
        // Service has been unregistered.  This only happens when you call
        // NsdManager.unregisterService() and pass in this listener.
        Log.i(tag, "onServiceUnregistered " + arg0);
    }

    @Override
    public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
        // Unregistration failed.  Put debugging code here to determine why.
        Log.i(tag, "onRegistrationFailed code " + errorCode + "\n" + serviceInfo);
    }
};

最佳答案

在我的 LG G5 (Android 7.0) 上也一样

检查了 MDNS 数据包是否出现在 Wireshark 中,他们确实出现了! 似乎服务是用正确的 ip 和端口宣布的, 即使 onServiceRegistered() 中的 NsdServiceInfo 表示不同的内容。

关于安卓 NSD : registration with null service type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43387668/

相关文章:

java - 在android中跟踪FTP上传数据?

android - 当在扩展对话框 Fragment 的 Fragment 中应用 onBackPressed 时

networking - 如何在 Windows 或 Linux 上安装 dns-sd 命令行测试工具?

c++ - Windows 上的 Bonjour/DNS-SD

android - 当我尝试设置 fontFamily 属性时,为标签 TextView 找到意外的命名空间前缀 "app"

android - 移动 LinearLayout 有困难

android - 在 jmdns 中使用子类型发现服务

linux - Avahi 主机名解析 : Is it caching somewhere?

windows - Bonjour DNS-SD 回调未被调用,我的代码中存在错误吗?