Android上的Bonjour实现

mud*_*dit 29 android bonjour zeroconf jmdns

我想在我的Android应用程序上实现bonjour/zero conf.我正在使用jmDns库来搜索所有可用的设备.以下是我用于搜索同一网络中的设备的代码:

public class ListDevices extends ListActivity {
    JmDNS jmdns;
    JmDNSImpl impl;
    MulticastLock lock;
    protected ServiceListener listener;
    protected ServiceInfo info;
    public ListView lv;
    public ArrayList<String> deviceList;
    public int cancel = 0;
    public final static String TAG = "ListDevices";

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

        deviceList = new ArrayList<String>();
        showAllPrinters();

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, deviceList));

        lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                       ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
        this.listener = new ServiceListener() {
            public void serviceAdded(ServiceEvent event) {
                deviceList.add("Service added   : " + event.getName() + "."
                        + event.getType());
                Log.v(TAG, "Service added   : " + event.getName() + "."
                        + event.getType());
            }

            public void serviceRemoved(ServiceEvent event) {
                deviceList.add("Service removed : " + event.getName() + "."
                        + event.getType());
                Log.v(TAG, "Service removed : " + event.getName() + "."
                        + event.getType());
            }

            public void serviceResolved(ServiceEvent event) {
                deviceList.add("Service resolved: " + event.getInfo());
                Log.v(TAG, "Service resolved: " + event.getInfo());
            }
        };
    }

    public void showAllPrinters() {
        Log.d("ListDevices", "in showAllPrinters");
        try {

            WifiManager wifi = (WifiManager)
                               getSystemService(Context.WIFI_SERVICE);
            lock = wifi.createMulticastLock("fliing_lock");
            lock.setReferenceCounted(true);
            lock.acquire();

            InetAddress inetAddress = getLocalIpAddress();
            jmdns = JmDNS.create(inetAddress, "TEST");

            ServiceInfo[] infos = jmdns.list("_http._tcp.local.");

            if (infos != null && infos.length > 0) {
                for (int i = 0; i < infos.length; i++) {
                    deviceList.add(infos[i].getName());
                }
            } else {
                deviceList.add("No device found.");
            }
            impl = (JmDNSImpl) jmdns;

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

    public InetAddress getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = (NetworkInterface) en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = (InetAddress) enumIpAddr
                            .nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        return inetAddress;
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("ListDevices", ex.toString());
        }
        return null;
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isFinishing()) {
            lock.release();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,我将它们添加到列表中,以便显示所有可用设备的列表.现在,当我运行此代码时,我没有异常/没有错误.但另一方面,我的列表中没有添加任何内容[PS:网络中至少有5-6台PC和Mac.

我也尝试从这段代码中获取列表:

jmdns.addServiceListener("_http._tcp.local.", listener);
Run Code Online (Sandbox Code Playgroud)

listeneronCreate在活动中定义.但这也没有返回任何设备.

请帮忙,在这里建议我做错了什么.任何帮助表示赞赏!

nat*_*evw 18

Android 4.1增加了网络服务发现,似乎只是以不同的方式包装了Bonjour堆栈.我还看到一个名为android.net.wifi.p2p.WifiP2pManager的低级API 直接暴露DNS-SD(以及UPnP?).

请注意,根据我现在所知,底层mDNSResponder守护程序似乎并不是一直在运行,并且不用于系统范围的DNS查找(例如,从浏览器).

  • 请不要使用此直到android解决网络服务发现相关的问题.这些问题的提及如下:1).http://code.google.com/p/android/issues/detail?id = 39583 2).https://code.google.com/p/android/issues/detail ?id = 35585 3).http://code.google.com/p/android/issues/detail?id = 39750 (5认同)

小智 5

我不能给你任何具体的代码帮助,但我很确定Android和mDNS至少有一些手机和(我相信)模拟器存在问题.

更多信息:

http://rgladwell.wordpress.com/2010/04/18/gotchas-android-and-multicast-dns/