为什么多播接收在某些Android设备上不起作用? [英] Why does multicast reception not work on some Android devices?

查看:129
本文介绍了为什么多播接收在某些Android设备上不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎多播接收在某些Android设备上不起作用.我无法使用13个测试设备中的4个接收多播.在这4台设备上,该应用似乎没有发送IGMP请求以加入多播组.

用于接收多播的代码如下:

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiManager.WifiLock wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, TAG);
WifiManager.MulticastLock multicastLock = wifiManager.createMulticastLock(TAG);
multicastLock.setReferenceCounted(true);

wifiLock.acquire();
multicastLock.acquire();

try {
    MulticastSocket socket = new MulticastSocket(32123);

    InetAddress group = InetAddress.getByName("224.1.2.3");
    socket.joinGroup(group);

    DatagramPacket packet;
    byte[] buf = new byte[256];
    packet = new DatagramPacket(buf, buf.length);

    socket.receive(packet);

    socket.leaveGroup(group);
    socket.close();
} catch (IOException e) {
    e.printStackTrace();
}

multicastLock.release();
wifiLock.release();

该应用具有以下权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

为了演示该问题,我在GitHub上使用上述代码创建了一个小测试项目: MulticastTest . /p>

我的代码有问题吗?我会错过权限吗?

编辑1 :该问题似乎与特定的Android版本无关.我可以在Android 4.x,5.x和6.x上重现该行为.

编辑2 :有一个相关的问题.

解决方案

坏消息:这似乎与受影响的设备有关.在不能接收多播流量的那些设备上,没有完全可用的/proc/net/igmp.如预期的那样,这很可能导致缺少联接组请求(IP_ADD_MEMBERSHIP).

我们尝试使用Android Java API,BSD套接字和Boost.Asio.这三个选项的结果相同.

我们使用名为 Multicast Tester 的应用验证了该问题.此应用程序在与我们应用程序相同的设备上具有相同的问题.设备不会发送IGMP请求,当然也不会接收到多播流量.

有一些打开和关闭(状态为过时"和错误的论坛")

似乎受影响的设备上的内核是用

构建的

CONFIG_IP_MULTICAST=n

内核配置文件中.这就是为什么/proc/net/igmp在受影响的设备上不可用的原因.很明显,只有在设置CONFIG_IP_MULTICAST时才创建它,如

The app has the following permissions:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

To demonstrate the problem I created a little test project using the above code on GitHub: MulticastTest.

Is there a problem with my code? Do I miss a permission?

EDIT 1: This problem does not seem to relate to a specific Android version. I can reproduce the behaviour on Android 4.x, 5.x, and 6.x.

EDIT 2: There is a related question.

解决方案

Bad news: This seems to be related to the affected devices. There is no /proc/net/igmp available exactly on those devices that can not receive the multicast traffic. As already expected this very likely leads to the missing join group request (IP_ADD_MEMBERSHIP).

We tried with the Android Java API, BSD sockets, and Boost.Asio. Same result with all three options.

We verified the problem with an app called Multicast Tester. This app has the same problem on the same devices as our app. No IGMP request is send by the device and of course no multicast traffic is received.

There are some open and closed (with statuses Obsolete and WrongForum) issues in the Android issue tracker. I think the closed issues are marked as Obsolete/WrongForum because it is not a problem in Android but specific to the affected devices (setup).

It seems the kernel on the affected devices was built with

CONFIG_IP_MULTICAST=n

in the kernel configuration file. That's also why /proc/net/igmp is not available on the affected devices. It obviously is only created when CONFIG_IP_MULTICAST is set as can be seen in the Linux kernel code.

这篇关于为什么多播接收在某些Android设备上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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