Android M - Sending request over WiFi (without connection) when Mobile data is ON (with connection)

0 votes

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:

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)

    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" /> my AndroidManifest.xml and I ensured that Settings.System.canWrite(this) returns true but something still seems to be wrong.

Help! TIA!

Oct 11, 2018 in IoT (Internet of Things) by Bharani
• 4,550 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Binding the network using ConnectivityManager.setProcessDefaultNetwork() will help to prevent roaming and allows for full TCP access. Thus, within the onAvailable() callback you could bind the application process to that network rather than opening a connection to a particular URL.

ConnectivityManager connection_manager = 
(ConnectivityManager) activity.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkRequest.Builder request = new NetworkRequest.Builder();

connection_manager.registerNetworkCallback(, new NetworkCallback() {

    public void onAvailable(Network network) {
answered Oct 11, 2018 by nirvana
• 3,060 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Detecting when a sensor is not sending data to Orion CB in FiWARE

Orion in itself has no such mechanism ...READ MORE

answered Nov 21, 2018 in IoT (Internet of Things) by Shubham
• 12,890 points
0 votes
1 answer

Restrict Android Internet usage to WiFi without Internet Access

You can check if wifi is connected ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
0 votes
1 answer

Android AllJoyn: Connection with second machine gives error of BusAttachement

Why dont you create two Interfaces, one ...READ MORE

answered Jul 25, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
0 votes
1 answer

Discovering IoT Devices on the network

AllJoyn offers about announcement service through which ...READ MORE

answered Sep 27, 2018 in IoT (Internet of Things) by Annie97
• 2,190 points
0 votes
1 answer

Use of .driver-metadata file in Android Things?

If I'm not wrong, it could be ...READ MORE

answered Oct 12, 2018 in IoT (Internet of Things) by nirvana
• 3,060 points
0 votes
1 answer

Oreo WiFi Connectivity

I am currently facing the same issue ...READ MORE

answered Oct 16, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
0 votes
1 answer
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.