apple bonjour for android
Asked Answered
B

4

3

I m looking for an apple bonjour for android. Are there a recommanded jar for this?

I googled for an apple bonjour application and I found the Jmdns application http://home.heeere.com/tech-androidjmdns.html.

but when running the DEMO application, a bug appear in the launch of the application. here after the 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 code:

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> 
Banking answered 5/9, 2012 at 13:12 Comment(1)
pls post your code so we can help you.Caralie
B
1

The author of the Jmdns example site, published a new example of Jmdns that works without problem. I have tested with the Jmdns 3.4.1 from Maven Central Repository and It works

this link is the new example of Jmdns

Banking answered 7/9, 2012 at 8:38 Comment(0)
M
2

I had same problem then i replaced jmdns.jar with maven jmdns 3.4.1.jar Click to download

if you will get dalvik error on start up try to remove jar file from build path and other folder keep this file in libs folder, may be this will helpful to you

Mix answered 22/12, 2012 at 4:36 Comment(0)
D
1

Make a folder called "libs", put the jar in that folder, and try.

Duren answered 6/9, 2012 at 7:37 Comment(2)
I can see that eclipse see the jar file. and there is no problem in the built. the problem is an exception error when starting the applicationBanking
I can see the problem is a NoDefClassFoundError. Something similar happened to me with other .jar. That exception happens when the class is not reachable, so despite you might think, Eclipse is not finding that class. See my question: #10071288 . Making the "libs" folder fixed it for me. In my case, Eclipse didn't show any error in compilation, but failed in execution.Duren
B
1

The author of the Jmdns example site, published a new example of Jmdns that works without problem. I have tested with the Jmdns 3.4.1 from Maven Central Repository and It works

this link is the new example of Jmdns

Banking answered 7/9, 2012 at 8:38 Comment(0)
M
0

Add this jar to your project, clean build and try again :

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

Mountfort answered 5/9, 2012 at 13:32 Comment(2)
Did you add it to your build path ?Mountfort
the build is OK, but when running an exception error appear at the start-up of the applicationBanking

© 2022 - 2024 — McMap. All rights reserved.