android的组播真是够麻烦的 java
1. 打开权限 android
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" /> ui
<uses-permission android:name="android.permission.INTERNET"/> this
2. 打开组播锁,默认时,移动设备为了省电是关闭的 spa
WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
if(wifi != null)
{
MulticastLock mcLock = wifi.createMulticastLock("mylock");
mcLock.acquire();
... code
3. 模拟器玩wifi仍是很痛苦的,莫名其妙的错误和失败 get
4. 真机玩wifi组播也很要命,根据stackoverflow,android2.3.7如下的系统只能接收本身发送的组播消息,没法接收其余设备,包括移动设备,PC等发送的组播消息 it
如下是原文http://stackoverflow.com/questions/3623143/multicast-on-android-2-2 io
I've been doing a bit of research into this, and I believe it is a problem with Android in general, and fixed in or around Android 2.3.7. ast
It's a bit too anecdotal, but here's what I tested:
HTC Desire, 2.2, stock: fail
HTC Desire, 2.3.7, CyanogenMod: success
Motorola Milestone, 2.1-update1: fail
HTC Desire S, 2.3.3: fail
Acer Iconia A501, 3.2.1: success
Samsung something, 2.3.3: fail
HTC Legend, 2.2 I think: fail
When I say 'fail', in fact they were all able to send multicast messages to the 'success' devices, but never receive anything back except their own messages - as jldupont describes.
手头只有一台老旧的索爱X8,android2.1, 接收组播异常
一台小米1s,android4.0,能够正常接收PC的组播