Skip to content

Commit

Permalink
Prefer wifi network when connecting over udp
Browse files Browse the repository at this point in the history
This works for android 6 and above
  • Loading branch information
Nisse43 committed Aug 22, 2019
1 parent 09c948a commit 1b89bf4
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
Expand Down Expand Up @@ -243,8 +247,22 @@ public void handleMessage(Message msg) {
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
break;
case R.id.menuUDPConnect:
ConnectivityManager cm = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE );
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {
for ( Network net : cm.getAllNetworks() ) {
NetworkInfo networkInfo = cm.getNetworkInfo( net );
if ( networkInfo.getType() == ConnectivityManager.TYPE_WIFI ) {
cm.bindProcessToNetwork( net );
break;
}
}
}

udp.connect(getGatewayIP(), 0);
useUDP();
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {
cm.bindProcessToNetwork( null );
}
break;
case R.id.menuUSBConnect:
checkUSBPermissionsAndConnectIfAllowed();
Expand Down

0 comments on commit 1b89bf4

Please sign in to comment.