Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

android - Moto G dosent receive UDP packets over WIFI Network

My receiver don't receive any UDP packet in Moto G but it works for other devices well.

Over WiFi network I have send UDP packets successfully from other devices. But in Moto E and Moto G it is not working.

Can anyone help figure out why it's not working for Moto G/E?

My problem was that i was not Receiving any UDP Packets over WiFi Network.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I ran into the exact same problem! UDP packets would work on every phone except the Moto E. Then I found some very interesting information on the interwebz.

The problem was that the Moto E (and presumably the Moto G) requires the app to acquire WifiManager.MulticastLock. From the android documentation -

Allows an application to receive Wifi Multicast packets. Normally the Wifi stack filters out packets not explicitly addressed to this device. Acquring a MulticastLock will cause the stack to receive packets addressed to multicast addresses. Processing these extra packets can cause a noticable battery drain and should be disabled when not needed.

You need to add the following permission to your app -

<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />

And then in your code acquire a lock like so -

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...