苹果bonjour为Android

时间:2012-09-05 13:12:50

标签: android bonjour jmdns

我正在为Android寻找苹果bonjour。 这是否有推荐的罐子?

我搜索了一个苹果bonjour应用程序,我找到了Jmdns应用程序http://home.heeere.com/tech-androidjmdns.html

但是在运行 DEMO 应用程序时,应用程序的启动会出现错误。在Eclipse LogCat之后:

09-05 13:56:49.926: E/AndroidRuntime(13243): java.lang.NoClassDefFoundError: javax.jmdns.JmDNS
09-05 13:56:49.926: E/AndroidRuntime(13243):    at com.heeere.android.dnssdtuto.DnssdDiscovery.setUp(DnssdDiscovery.java:44)
09-05 13:56:49.926: E/AndroidRuntime(13243):    at com.heeere.android.dnssdtuto.DnssdDiscovery.access$0(DnssdDiscovery.java:38)
09-05 13:56:49.926: E/AndroidRuntime(13243):    at com.heeere.android.dnssdtuto.DnssdDiscovery$1.run(DnssdDiscovery.java:27)
09-05 13:56:49.926: E/AndroidRuntime(13243):    at android.os.Handler.handleCallback(Handler.java:587)
09-05 13:56:49.926: E/AndroidRuntime(13243):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-05 13:56:49.926: E/AndroidRuntime(13243):    at android.os.Looper.loop(Looper.java:123)
09-05 13:56:49.926: E/AndroidRuntime(13243):    at android.app.ActivityThread.main(ActivityThread.java:4363)

Java代码:

package com.heeere.android.dnssdtuto;

import java.io.IOException;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
import javax.jmdns.ServiceListener;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class DnssdDiscovery extends Activity {

    android.net.wifi.WifiManager.MulticastLock lock;
    android.os.Handler handler = new android.os.Handler();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        handler.postDelayed(new Runnable() {
            public void run() {
                setUp();
            }
            }, 1000);

    }    /** Called when the activity is first created. */


    private String type = "_workspace._tcp.local.";
    private JmDNS jmdns = null;
    private ServiceListener listener = null;
    private ServiceInfo serviceInfo;
    private void setUp() {
        android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
        lock = wifi.createMulticastLock("mylockthereturn");
        lock.setReferenceCounted(true);
        lock.acquire();
        try {
            jmdns = JmDNS.create();
            jmdns.addServiceListener(type, listener = new ServiceListener() {

                public void serviceResolved(ServiceEvent ev) {
                    notifyUser("Service resolved: " + ev.getInfo().getQualifiedName() + " port:" + ev.getInfo().getPort());
                }

                public void serviceRemoved(ServiceEvent ev) {
                    notifyUser("Service removed: " + ev.getName());
                }

                public void serviceAdded(ServiceEvent event) {
                    // Required to force serviceResolved to be called again (after the first search)
                    jmdns.requestServiceInfo(event.getType(), event.getName(), 1);
                }
            });
            serviceInfo = ServiceInfo.create("_test._tcp.local.", "AndroidTest", 0, "plain test service from android");
            jmdns.registerService(serviceInfo);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    }


    private void notifyUser(final String msg) {
        handler.postDelayed(new Runnable() {
            public void run() {

        TextView t = (TextView)findViewById(R.id.text);
        t.setText(msg+"\n=== "+t.getText());
            }
            }, 1);

    }

    @Override
        protected void onStart() {
        super.onStart();
        //new Thread(){public void run() {setUp();}}.start();
    }

    @Override
        protected void onStop() {
        if (jmdns != null) {
            if (listener != null) {
                jmdns.removeServiceListener(type, listener);
                listener = null;
            }
            jmdns.unregisterAllServices();
            try {
                jmdns.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            jmdns = null;
        }
        //repo.stop();
        //s.stop();
        lock.release();
        super.onStop();
    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.heeere.android.dnssdtuto"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".DnssdDiscovery"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

</manifest> 

4 个答案:

答案 0 :(得分:3)

我有同样的问题 然后我用maven jmdns 3.4.1.jar替换了jmdns.jar Click to download

如果你在启动时遇到dalvik错误,请尝试从构建路径和其他文件夹中删除jar文件 将此文件保存在libs文件夹中, 可能这对你有帮助

答案 1 :(得分:1)

创建一个名为“libs”的文件夹,将jar放在该文件夹中,然后尝试。

答案 2 :(得分:1)

Jmdns example site的作者发表了一个新的Jmdns示例,它没有问题。我已经使用Maven Central Repository中的Jmdns 3.4.1进行了测试,并且它正常工作

这个link是Jmdns的新例子

答案 3 :(得分:0)

将此jar添加到您的项目中,清理构建并再试一次:

http://repo1.maven.org/maven2/javax/jmdns/jmdns/3.2.2/jmdns-3.2.2.jar