Skip to content

Commit

Permalink
Merge branch 'dev' into android10
Browse files Browse the repository at this point in the history
  • Loading branch information
voroshkov authored May 4, 2020
2 parents e023569 + d8abb07 commit 44af585
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +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 @@ -245,8 +247,7 @@ public void handleMessage(Message msg) {
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
break;
case R.id.menuUDPConnect:
udp.connect(getGatewayIP(), 0);
useUDP();
preferWifiAndConnectUDP();
break;
case R.id.menuUSBConnect:
checkUSBPermissionsAndConnectIfAllowed();
Expand All @@ -260,6 +261,25 @@ public void handleMessage(Message msg) {
return super.onOptionsItemSelected(item);
}

private void preferWifiAndConnectUDP() {
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);
}
}

private boolean checkIsWifiOnAndConnected() {
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
if (!wifiMgr.isWifiEnabled()) return false; // Wi-Fi adapter is OFF
Expand Down

0 comments on commit 44af585

Please sign in to comment.