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的組播