我正在尝试为 Android 应用程序和桌面应用程序之间的通信实现 JmDNS 发现。我遵循了以下教程: http ://home.heeere.com/tech-androidjmdns.html
Android 应用注册一个服务,桌面应用为该服务添加一个监听器。我已经让它在四分之三的设备上运行得很好,但是第四个(运行 Android 3.2 的三星 Galaxy Tab 10.1 PT7500)我无法解决该服务。我的处理程序收到serviceAdded
事件,但没有serviceResolved
事件。我也尝试过调用jmdns.requestServiceInfo
and jmdns.getServiceInfo
,但前者什么都不做,后者超时并返回 null。
但是,jmdns-browser 能够很好地解析服务,所以它不是设备。这两台设备上都没有防火墙。该服务始终使用 IPv4 地址。
有谁知道什么可能导致这个问题?
启动服务的代码:
jmdns = JmDNS.create(wifiAddress);
ServiceInfo serviceInfo = ServiceInfo.create(Constants.SERVICE_TYPE,
Constants.SERVICE_NAME, Constants.ZEROCONF_PORT, "my service");
HashMap<String, String> deviceInfoMap = new HashMap<String, String>();
deviceInfoMap.put(Constants.KEY_DEVICE_NAME, getDeviceName());
deviceInfoMap.put(Constants.KEY_DEVICE_ID, getDeviceId());
// ...
serviceInfo.setText(deviceInfoMap);
jmdns.registerService(serviceInfo);
客户端/监听器的代码:
jmdns = JmDNS.create();
jmdns.addServiceListener(Constants.SERVICE_TYPE, serviceListener = new ServiceListener() {
@Override
public void serviceResolved(ServiceEvent event) {
System.out.println("Service resolved: " + event.getName() +
" of type " + event.getType());
}
@Override
public void serviceRemoved(ServiceEvent event) {
System.out.println("Service removed: " + event.getName() +
" of type " + event.getType());
}
@Override
public void serviceAdded(ServiceEvent event) {
System.out.println("Service added: " + event.getName() +
" of type " + event.getType());
ServiceInfo info = jmdns.getServiceInfo(event.getType(), event.getName());
System.out.println("Service info: " + info); // --> null
}
});
输出:
Service added: my service @ GT-P7500 of type _mytype._tcp.local.
Service info: null
Service added: my service @ Galaxy Nexus of type _mytype._tcp.local.
Service info: [ServiceInfoImpl@183779345
name: 'my service @ Galaxy Nexus ._mytype._tcp.local.'
address: '/192.168.1.154:4242 ' status: 'DNS: myhost.local.
state: probing 1 task: null', has data
deviceName: Galaxy Nexus
deviceId: <id>
displayDensity: XHDPI
]