对于Android 2.0.1(目前在DROID上),多播是否已损坏,或者我错过了什么?

Gub*_*ron 5 java android multicast datagram

此代码在Ubuntu,Windows和Mac OS X中运行良好.它也适用于运行Android 2.1.1的Nexus One.

我开始发送和收听多播数据报,所有计算机和Nexus One都能完美地看到对方.然后我在Droid(固件2.0.1)上运行相同的代码,每个人都会得到Droid发送的数据包,但机器人只会监听自己的数据包.

这是run()一个线程的方法,该线程不断地在多播组上侦听发送到该组的传入数据包.

我在本地网络上运行测试,我在路由器中启用了多播支持.我的目标是让设备通过向多播组广播包而在网上相遇.

public void run() {
    byte[] buffer = new byte[65535];
    DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

    try {
        MulticastSocket ms = new MulticastSocket(_port);
        ms.setNetworkInterface(_ni); //non loopback network interface passed
        ms.joinGroup(_ia); //the multicast address, currently 224.0.1.16
        Log.v(TAG,"Joined Group " + _ia);

        while (true) {
            ms.receive(dp);
            String s = new String(dp.getData(),0,dp.getLength());
            Log.v(TAG,"Received Package on "+ _ni.getName() +": " + s);
            Message m = new Message();
            Bundle b = new Bundle();
            b.putString("event", "Listener ("+_ni.getName()+"): \"" + s + "\"");
            m.setData(b);
            dispatchMessage(m); //send to ui thread
        }
    } catch (SocketException se) {
        System.err.println(se);
    } catch (IOException ie) {
        System.err.println(ie);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是从每个可用的有效网络接口(不是环回接口)发送多播数据报的代码.

public void sendPing() {
    MulticastSocket ms = null;
    try {
        ms = new MulticastSocket(_port);
        ms.setTimeToLive(TTL_GLOBAL);

        List<NetworkInterface> interfaces = getMulticastNonLoopbackNetworkInterfaces();
        for (NetworkInterface iface : interfaces) {
            //skip loopback
            if (iface.getName().equals("lo"))
                continue;
            ms.setNetworkInterface(iface);
            _buffer = ("FW-"+ _name +" PING ("+iface.getName()+":"+iface.getInetAddresses().nextElement()+")").getBytes();
            DatagramPacket dp = new DatagramPacket(_buffer, _buffer.length,_ia,_port);
            ms.send(dp);
            Log.v(TAG,"Announcer: Sent packet - " + new String(_buffer) + " from " + iface.getDisplayName());
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e2) {
        e2.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

更新(2010年4月2日) 我找到了一种让Droid的网络接口使用Multicast进行通信的方法:WifiManager.MulticastLock.

MulticastLock _wifiMulticastLock = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).createMulticastLock("multicastLockNameHere");
_wifiMulticastLock.acquire();
Run Code Online (Sandbox Code Playgroud)

那么当你完成了......

if (_wifiMulticastLock != null && _wifiMulticastLock.isHeld())
    _wifiMulticastLock.release();
Run Code Online (Sandbox Code Playgroud)

在我这样做之后,Droid开始在多播组上发送和接收UDP数据报.

2010年7月6日更新

根据请求,这是我当前的代码,下一个方法存在于可用于广播和多播接收器的抽象类上.

public void run() {
    onInit();
    try {
        byte[] data = new byte[65535];
        while (isProcessing()) {
            try {
                DatagramPacket receivedDatagram = new DatagramPacket(data, data.length);
                _socket.receive(receivedDatagram);
                onDatagramReceived(receivedDatagram);
                data = new byte[65535]; // This pattern is for saving memory allocation.
            } catch (InterruptedIOException e) {
                if (!isProcessing())
                    break;
            }
        } // while

    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    } finally {
        onStop();
        _socket.close();
        _socket.disconnect();
    }
}
Run Code Online (Sandbox Code Playgroud)

您的扩展类应该实现onInit()onDatagramReceived()

对于多播接收器,onInit() 看起来像这样:

_socket = new MulticastSocket(PORT_MULTICAST);
InetAddress groupAddress = InetAddress.getByAddress(MULTICAST_GROUP_ADDRESS); 
InetAddress groupInetAddress = FrostWireUtils.fastResolveAddress(groupAddress); //reflection hack to not resolve ips
try {
    _socket.setSoTimeout(500);
    _socket.setTimeToLive(MULTICAST_TTL_GLOBAL);
    _socket.setReuseAddress(true);
    _socket.setNetworkInterface(
        WifiUtils.getWifiNetworkInterface());
    _socket.joinGroup(groupInetAddress);
    WifiUtils.lockMulticast();
} catch (Exception e) {
    Log.e(TAG, e.getMessage(), e);
}
Run Code Online (Sandbox Code Playgroud)

Gub*_*ron 0

我实施了另一个测试,这次使用 UDP Broadcast。有用。

结论:据我所知,固件 2.0.1 的 Motorola Droid 手机不支持多播,但您始终可以在广播地址上使用常规 DatagramPackets。