局域网中的设备发现 [英] Device discovery in local network

查看:160
本文介绍了局域网中的设备发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用SDK> = 16开发一个android应用程序,该应用程序应该能够使用WiFi无线电在局域网中发现不同的android设备(也包括iOS设备).

I'm currently developing an android app using SDK >= 16 which should be able to discover different android devices (later also iOS devices) in a local area network using the WiFi radio.

我的第一个猜测是使用多播,该多播在我的Samsung Galaxy S2上不起作用:仅在从同一设备发送数据包时才接收数据包.

My first guess was to use multicast which turned out to be non functional on my Samsung Galaxy S2: packets are only received when sent from the same device.

我的第二个猜测是使用有限的IP地址范围主动扫描网络并等待适当的响应.不幸的是,这意味着网络使用DHCP来寻址IP地址.

My second guess is to actively scan the network using a limited IP address range and wait for a proper response. Unfortunately, this implies that the network uses DHCP to address the IP addresses.

以上解决方案似乎都不是完美的解决方案.

None of the above solutions seem to be the perfect solution.

我对第一个猜测的当前解决方案:

My current solution for my first guess:

public class MulticastReceiver extends AsyncTask<Activity, Integer, String> {

    private static final String host = "224.1.1.1";
    private static final int port = 5007;
    private static final String TAG = "MulticastReceiver";

    protected String doInBackground(Activity... activities) {
        WifiManager wm = (WifiManager)activities[0].getSystemService(Context.WIFI_SERVICE);
        WifiManager.MulticastLock multicastLock = wm.createMulticastLock("mydebuginfo");
        multicastLock.acquire();
        String message = "Nothing";

        if (multicastLock.isHeld()) {
            Log.i(TAG, "held multicast lock");
        }

        try {
            InetAddress addr = InetAddress.getByName(host);
            MulticastSocket socket = new MulticastSocket(port);
            socket.setTimeToLive(4);
            socket.setReuseAddress(true);
            socket.joinGroup(addr);

            byte[] buf = new byte[5];
            DatagramPacket recv = new DatagramPacket(buf, buf.length, addr, port);
            socket.receive(recv);
            message = new String(recv.getData());
            socket.leaveGroup(addr);
            socket.close();
        } catch (Exception e) {
            message = "ERROR " + e.toString();
        }

        multicastLock.release();

        return message;
    }
}

此代码导致在套接字socket.receive(recv);上阻塞如果我指定了超时,则会收到超时异常.

This code results in blocking on line socket.receive(recv); If I specify a timeout, I get a timeout exception.

推荐答案

在非常相似的问题中检查我的答案

Check my answer in very similar question Android Network Discovery Service (ish) before API 14

我不相信多播不能在Galaxy S2上运行,前一段时间,当我在编写某些网络应用程序时,我在许多设备上进行了一些测试,其中一些设备较旧,例如G1,但也测试了S2,S3和Galaxy Tab 10.

I do not belive that multicast is not working on Galaxy S2, some time ago when I was coding some network application, I made several test on many devices, some older like G1 but also on S2, S3 and Galaxy Tab 10.

但是要使用多播,必须以编程方式启用它.

But to be able to use multicast you must enable it programatically.

您使用过这段代码吗?

WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
if(wifi != null){
WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");
lock.acquire();
}

这篇关于局域网中的设备发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆