I need to send UDP packets to a WiFi module (provided with my own AP) with no internet connection but when I connect the mobile with the AP, it redirects my packets on the mobile data interface because it finds an internet connection.
This is my code for it, but it's not working on Android M:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setWifiInterfaceAsDefault() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
NetworkRequest networkRequest= builder.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build();
connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback());
}
I also added:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
..to my AndroidManifest.xml and I ensured that Settings.System.canWrite(this) returns true but something still seems to be wrong.
Help! TIA!