Android 使用PhoneGap/Cordova 2.9.0中的插件实现mDNS/ZeroConf/Bonjour

Android 使用PhoneGap/Cordova 2.9.0中的插件实现mDNS/ZeroConf/Bonjour,android,cordova,phonegap-plugins,zeroconf,Android,Cordova,Phonegap Plugins,Zeroconf,我正在为Android制作我的第一个PhoneGap应用程序,它需要mDNS分辨率。由于Android(v4.1之前)上没有解析“.local”地址,因此我使用了ZeroConf库和JmDNS.jar文件。我已经参考了来自的插件,你可能想看看 ZeroConf.java public PluginResult execute(String action, JSONArray args, String callbackId) { this.callback = callbackId;

我正在为Android制作我的第一个PhoneGap应用程序,它需要mDNS分辨率。由于Android(v4.1之前)上没有解析“.local”地址,因此我使用了ZeroConf库和JmDNS.jar文件。我已经参考了来自的插件,你可能想看看

ZeroConf.java

public PluginResult execute(String action, JSONArray args, String callbackId) {
    this.callback = callbackId;

    if (action.equals("watch")) {
        String type = args.optString(0);
        if (type != null) {
            watch(type);
        } else {
            return new PluginResult(PluginResult.Status.ERROR, "Service type not specified");
        }
    } else if (action.equals("unwatch")) {
        String type = args.optString(0);
        if (type != null) {
            unwatch(type);
        } else {
            return new PluginResult(PluginResult.Status.ERROR, "Service type not specified");
        }
    } else if (action.equals("register")) {
        JSONObject obj = args.optJSONObject(0);
        if (obj != null) {
            String type = obj.optString("type");
            String name = obj.optString("name");
            int port = obj.optInt("port");
            String text = obj.optString("text");
            if(type == null) {
                return new PluginResult(PluginResult.Status.ERROR, "Missing required service info");
            }
            register(type, name, port, text);
        } else {
            return new PluginResult(PluginResult.Status.ERROR, "Missing required service info");
        }

    } else if (action.equals("close")) { 
        if(jmdns != null) {
            try {
                jmdns.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }  else if (action.equals("unregister")) {
        if(jmdns != null) {
            jmdns.unregisterAllServices();
        }

    } else {
        Log.e("ZeroConf", "Invalid action: " + action);
        return new PluginResult(PluginResult.Status.INVALID_ACTION);
    }
    PluginResult result = new PluginResult(Status.NO_RESULT);
    result.setKeepCallback(true);
    return result;
}

private void watch(String type) {
    if(jmdns == null) {
        setupWatcher();
    }
    Log.d("ZeroConf", "Watch " + type);
    Log.d("ZeroConf", "Name: " + jmdns.getName() + " host: " + jmdns.getHostName());
    jmdns.addServiceListener(type, listener);
}
private void unwatch(String type) {
    if(jmdns == null) {
        return;
    }
    jmdns.removeServiceListener(type, listener);
}

private void register (String type, String name, int port, String text) {
    if(name == null) {
        name = "";
    }

    if(text == null) {
        text = "";
    }

     try {
         ServiceInfo service = ServiceInfo.create(type, name, port, text);
        jmdns.registerService(service);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void setupWatcher() {
    Log.d("ZeroConf", "Setup watcher");
     WifiManager wifi = (WifiManager) this.cordova.getActivity().getSystemService(android.content.Context.WIFI_SERVICE);
    lock = wifi.createMulticastLock("ZeroConfPluginLock");
    lock.setReferenceCounted(true);
    lock.acquire();
    try {
        jmdns = JmDNS.create();
        listener = new ServiceListener() {

            public void serviceResolved(ServiceEvent ev) {
                Log.d("ZeroConf", "Resolved");

                sendCallback("added", ev.getInfo());
            }

            public void serviceRemoved(ServiceEvent ev) {
                Log.d("ZeroConf", "Removed");

                sendCallback("removed", ev.getInfo());
            }

            public void serviceAdded(ServiceEvent event) {
                Log.d("ZeroConf", "Added");

                // Force serviceResolved to be called again
                jmdns.requestServiceInfo(event.getType(), event.getName(), 1);
            }
        };

    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
}

public void sendCallback(String action, ServiceInfo info) {
    JSONObject status = new JSONObject();
    try {
        status.put("action", action);
        status.put("service", jsonifyService(info));
        Log.d("ZeroConf", "Sending result: " + status.toString());

        PluginResult result = new PluginResult(PluginResult.Status.OK, status);
        result.setKeepCallback(true);
        this.success(result, this.callback);

    } catch (JSONException e) {

        e.printStackTrace();
    }


}


public static JSONObject jsonifyService(ServiceInfo info) {
    JSONObject obj = new JSONObject();
    try {
        obj.put("application", info.getApplication());
        obj.put("domain", info.getDomain());
        obj.put("port", info.getPort());
        obj.put("name", info.getName());
        obj.put("server", info.getServer());
        obj.put("description", info.getNiceTextString());
        obj.put("protocol", info.getProtocol());
        obj.put("qualifiedname", info.getQualifiedName());
        obj.put("type", info.getType());

        JSONArray addresses = new JSONArray();
        String[] add = info.getHostAddresses();
        for(int i = 0; i < add.length; i++) {
            addresses.put(add[i]);
        }
        obj.put("addresses", addresses);
        JSONArray urls = new JSONArray();

        String[] url = info.getURLs();
        for(int i = 0; i < url.length; i++) {
            urls.put(url[i]);
        }
        obj.put("urls", urls);

    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }

    return obj;

}
config.xml

<plugins>
    <plugin name="ZeroConf" value="com.triggertrap.ZeroConf"/>
</plugins>

现在我的问题是:


我想调用一个固定的URL,例如,index.html页面中的,它应该解析为本地IP地址。我如何做到这一点?我知道这必须使用JavaScript来完成,但是如何去做呢?我搜索了很多文章,一直到这里。如果您能进一步指导我,我将不胜感激。

在您收到“设备就绪”活动后的某个地方,您可以注册观看通过“你好”发布的服务广告:


ZeroConf.watch(“_http._tcp.local.”,函数(服务){
//对服务做些什么
}


但是,使用ZeroConf插件是不够的,因为您还需要在foo.local上提供内容的服务器通过bonjour发布其HTTP服务。如果您使用node.js服务器,您可以使用它。

foo是本地LAN网络上设备的名称。我正试图通过此URL访问该设备。您使用什么服务器技术来访问它在foo上服务网页?如果您已经在应用程序中硬编码了foo.local,那么您正在寻找的功能是多播名称解析MDN(例如,将foo.local映射到IP地址).ZeroConf插件提供DNS服务发现,并不是MDN的直接替代品。您可能希望查看JmDNS提供的完整API,并仅使用MDN部分(如果存在)。Stribu:foo实际上是一个硬件设备arduino,此应用程序基本上是为了通过此固定URL尝试通过应用程序ping arduino。您的情况如何用这个在你的arduino上做广告:你能做到吗..我指的是这个问题-你能帮我吗?
<plugins>
    <plugin name="ZeroConf" value="com.triggertrap.ZeroConf"/>
</plugins>