7

我正在尝试为 Android 应用程序和桌面应用程序之间的通信实现 JmDNS 发现。我遵循了以下教程: http ://home.heeere.com/tech-androidjmdns.html

Android 应用注册一个服务,桌面应用为该服务添加一个监听器。我已经让它在四分之三的设备上运行得很好,但是第四个(运行 Android 3.2 的三星 Galaxy Tab 10.1 PT7500)我无法解决该服务。我的处理程序收到serviceAdded事件,但没有serviceResolved事件。我也尝试过调用jmdns.requestServiceInfoand 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
] 
4

2 回答 2

8

Checking out the latest source code version from the github repository did the trick. Discovering the service now works just fine on all devices. (Note: I was using JmDNS version 3.4.1 which I had downloaded from sourceforge before)

Having skimmed through the SVN history, there seem to be a couple of commits related to problems with resolving services since the 3.4.1 release.

Edit: Replaced svn with github repository since jmdns has moved there.

于 2013-01-18T15:49:51.863 回答
1

使用JmDNS.create(InetAddress addr, String name)而不是JmDNS.create(). 在最新版本的 Android 中,JmDNS 需要更多地了解本地设备和它正在侦听的网络接口。

JmDNS jmdns; // somewhere global

// In a background thread 
public void buildJmDNS() {
    InetAddress addr = InetAddress.getLocalHost();
    String hostname = InetAddress.getByName(addr.getHostName()).toString();
    try {
        jmdns = JmDNS.create(addr, hostname); // throws IOException
    } catch (IOException e) {
       e.printStackTrace(); // handle this if you want
    }
}

我遇到了同样的问题,这为我解决了。我试图找到我发现的例子来说明为什么这是必要的,但我不能。

于 2013-01-18T15:27:16.587 回答