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:

@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!

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

1 answer to this question.

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();
request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);

connection_manager.registerNetworkCallback(request.build(), new NetworkCallback() {

    @Override
    public void onAvailable(Network network) {
        ConnectivityManager.setProcessDefaultNetwork(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
• 13,110 points
25 views
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
81 views
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
24 views
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
22 views
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
33 views
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
144 views
0 votes
1 answer
0 votes
1 answer