Moto G tolerance receives UDP packets over WIFI network
My receiver doesn't receive any UDP packet in Moto G, but it works well for other devices.
Over the wifi network, I am successfully sending UDP packets from other devices. But in Moto E and Moto G, it doesn't work.
Can anyone help figure out why it doesn't work for Moto G / E?
My problem was that I was not receiving any UDP packets over the WiFi network.
source to share
Try this code. This works for me.
public void run() {
Looper.prepare();
try {
WifiManager.MulticastLock lock;
WifiManager wifi;
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
if (lock == null)
lock = wifi.createMulticastLock("WiFi_Lock");
lock.setReferenceCounted(true);
lock.acquire();
}
}
catch(Exception e)
{
Log.d("Wifi Exception",""+e.getMessage().toString());
}
}
source to share
I faced the same problem! UDP packets will work on every phone except Moto E. Then I found very interesting information about interwebz .
The problem was that Moto E (and presumably Moto G) requires the app to purchase WifiManager.MulticastLock
. From android documentation -
Allows the application to receive Multicast Wifi packets. Typically, the Wifi Stack will filter out packets not explicitly addressed to this device. Acquiring MulticastLock will cause the stack to receive packets addressed to multicast addresses. Handling these extra packets can cause noticeable battery drain and should be disabled when not needed.
You need to add the following permissions to your application -
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
And then in your code, get the lock, like:
WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
if(wifi != null){
WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");
lock.acquire();
}
source to share