gpt4 book ai didi

安卓 NSD : registration with null service type

转载 作者:行者123 更新时间:2023-11-30 00:36:10 26 4
gpt4 key购买 nike

注册的结果不能为空,这是我从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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com