1
NsdManagerを使用してアンドロイドにnsdserviceを登録しようとしていますが、内部エラーが原因で失敗しています。これは単なるテストであり、将来私は複数のデバイスを1つの "ホストデバイス"に接続できるようにしたいと考えています。クライアントはデータをホストに送信できる必要があります。ネットワーク内のデバイスipにホストのinetadressを設定しようとしましたが、同じ結果が出ました。NsdManagerがアンドロイドでサービスを登録できません
私mainactivity:
public class Main2Activity extends AppCompatActivity {
ServerSocket serverSocket;
int localPort;
NsdManager.RegistrationListener registrationListener;
String serviceName = "ef";
NsdManager nsdManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
initializeServerSocket();
initializeRegistrationListener();
registerService(localPort);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@SuppressWarnings("deprecation")
public void registerService (int port) throws UnknownHostException {
NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setServiceName("testchat");
serviceInfo.setServiceType("nsdchat._tcp");
serviceInfo.setPort(port);
/*WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
int ipAdress = wm.getConnectionInfo().getIpAddress();
serviceInfo.setHost(InetAddress.getByName(Formatter.formatIpAddress(ipAdress)));*/
nsdManager = (NsdManager) this.getSystemService(Context.NSD_SERVICE);
Log.d("nsdmanager", "info: " + nsdManager.toString());
nsdManager.registerService(
serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener);
}
public void initializeServerSocket() throws IOException {
// Initialize a server socket on the next available port.
serverSocket = new ServerSocket(0);
// Store the chosen port.
localPort = serverSocket.getLocalPort();
Log.d("test", "initialized server socket: " + localPort);
}
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.
Log.d("test", "service registered");
serviceName = NsdServiceInfo.getServiceName();
}
@Override
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Registration failed! Put debugging code here to determine why.
Log.d("test", "registration failed");
Log.d("serviceinfo", serviceInfo.toString());
switch (errorCode) {
case NsdManager.FAILURE_ALREADY_ACTIVE:
Log.d("test", "FAILURE_ALREADY_ACTIVE");
break;
case NsdManager.FAILURE_INTERNAL_ERROR:
Log.d("test", "FAILURE_INTERNAL_ERROR");
break;
case NsdManager.FAILURE_MAX_LIMIT:
Log.d("test", "FAILURE_MAX_LIMIT");
break;
}
}
@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.
}
};
Log.d("test", "initialized registration listener");
}
私のログは次のようになります。
06-06 14:23:57.016 16319-16319/xxx D/test: initialized server socket: 49575
06-06 14:23:57.016 16319-16319/xxx D/test: initialized registration listener
06-06 14:23:57.020 16319-16319/xxx D/nsdmanager: info: [email protected]
06-06 14:23:57.030 16319-16457/xxx D/test: registration failed
06-06 14:23:57.031 16319-16457/xxx D/serviceinfo: name: testchat, type: nsdchat._tcp, host: null, port: 49575, txtRecord:
06-06 14:23:57.031 16319-16457/xxx D/test: FAILURE_INTERNAL_ERROR
。楽しい事実:公式のチュートリアルでは、それは正確にその方法で書かれていた、その欠陥が... – Tazua