diff --git a/android/build.gradle b/android/build.gradle index d6c21f4..f420ac6 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -8,7 +8,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.1.2' + classpath 'com.android.tools.build:gradle:3.3.2' } } diff --git a/android/src/main/java/com/ly/wifi/WifiDelegate.java b/android/src/main/java/com/ly/wifi/WifiDelegate.java index c3b6f6f..da836fd 100644 --- a/android/src/main/java/com/ly/wifi/WifiDelegate.java +++ b/android/src/main/java/com/ly/wifi/WifiDelegate.java @@ -8,6 +8,9 @@ import android.content.pm.PackageManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; +import android.net.NetworkRequest; +import android.net.NetworkCapabilities; +import android.net.Network; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; @@ -36,6 +39,7 @@ private PermissionManager permissionManager; private static final int REQUEST_ACCESS_FINE_LOCATION_PERMISSION = 1; private static final int REQUEST_CHANGE_WIFI_STATE_PERMISSION = 2; + private static final int REQUEST_CHANGE_INTERNET_PERMISSION = 3; NetworkChangeReceiver networkReceiver; interface PermissionManager { @@ -219,9 +223,22 @@ public void connection(MethodCall methodCall, MethodChannel.Result result) { permissionManager.askForPermission(Manifest.permission.CHANGE_WIFI_STATE, REQUEST_ACCESS_FINE_LOCATION_PERMISSION); return; } + if (!permissionManager.isPermissionGranted(Manifest.permission.INTERNET)) { + permissionManager.askForPermission(Manifest.permission.INTERNET, REQUEST_CHANGE_INTERNET_PERMISSION); + return; + } + connection(); } + public void disconnect(MethodCall methodCall, MethodChannel.Result result) { + if (!setPendingMethodCallAndResult(methodCall, result)) { + finishWithAlreadyActiveError(); + return; + } + disconnect(); + } + private void connection() { String ssid = methodCall.argument("ssid"); String password = methodCall.argument("password"); @@ -241,6 +258,59 @@ private void connection() { wifiManager.enableNetwork(netId, true); wifiManager.reconnect(); result.success(1); + // >> HELBER + // 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); + // } + // }); + + // final ConnectivityManager manager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); + // NetworkRequest.Builder builder; + // builder = new NetworkRequest.Builder(); + // builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); + // if (manager != null) { + // manager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() { + // @Override + // public void onAvailable(Network network) { + // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + // // manager.bindProcessToNetwork(network); + // boolean result = ConnectivityManager.setProcessDefaultNetwork(network); + // Log.d("HELBER", "RESULT: "+ result); + // manager.unregisterNetworkCallback(this); + // } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + // ConnectivityManager.setProcessDefaultNetwork(network); + // manager.unregisterNetworkCallback(this); + // } + // } + // }); + // } + // << HELBER + // >> Pileggi + ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); + Network etherNetwork = null; + for (Network network : connectivityManager.getAllNetworks()) { + NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); + if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) { + etherNetwork = network; + } + } + // Android 6 + Network boundNetwork = connectivityManager.getBoundNetworkForProcess(); + if (boundNetwork != null) { + NetworkInfo boundNetworkInfo = connectivityManager.getNetworkInfo(boundNetwork); + if (boundNetworkInfo.getType() != ConnectivityManager.TYPE_ETHERNET) { + if (etherNetwork != null) { + connectivityManager.bindProcessToNetwork(etherNetwork); + } + } + } + // << Pileggi clearMethodCallAndResult(); } else { networkReceiver.connect(netId); @@ -248,6 +318,22 @@ private void connection() { } } + private void disconnect() { + String ssid = methodCall.argument("ssid"); + WifiConfiguration wifiConfig = createWifiConfig(ssid, ""); + if (wifiConfig == null) { + result.success(1); + clearMethodCallAndResult(); + return; + } + wifiManager.disconnect(); + int netId = wifiManager.addNetwork(wifiConfig); + wifiManager.disableNetwork(netId); + wifiManager.removeNetwork(netId); + result.success(1); + clearMethodCallAndResult(); + } + private WifiConfiguration createWifiConfig(String ssid, String Password) { WifiConfiguration config = new WifiConfiguration(); config.SSID = "\"" + ssid + "\""; @@ -307,6 +393,11 @@ public boolean onRequestPermissionsResult(int requestCode, String[] permissions, connection(); } break; + case REQUEST_CHANGE_INTERNET_PERMISSION: + if (permissionGranted) { + connection(); + } + break; default: return false; } diff --git a/android/src/main/java/com/ly/wifi/WifiPlugin.java b/android/src/main/java/com/ly/wifi/WifiPlugin.java index 68945cb..a6e8fa2 100644 --- a/android/src/main/java/com/ly/wifi/WifiPlugin.java +++ b/android/src/main/java/com/ly/wifi/WifiPlugin.java @@ -60,6 +60,9 @@ public void onMethodCall(MethodCall call, Result result) { case "connection": delegate.connection(call, result); break; + case "disconnect": + delegate.disconnect(call, result); + break; default: result.notImplemented(); break; diff --git a/example/android/build.gradle b/example/android/build.gradle index 71be3d3..6e12e86 100644 --- a/example/android/build.gradle +++ b/example/android/build.gradle @@ -5,7 +5,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.3.1' + classpath 'com.android.tools.build:gradle:3.3.2' } } diff --git a/example/lib/main.dart b/example/lib/main.dart index 48a7875..754ea4b 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -30,6 +30,8 @@ class _MyHomePageState extends State { String _ip = 'click button to get ip.'; List ssidList = []; String ssid = '', password = ''; + TextEditingController ssidControler = TextEditingController(); + TextEditingController passwdControler = TextEditingController(); @override void initState() { @@ -83,6 +85,7 @@ class _MyHomePageState extends State { ], ), TextField( + controller: ssidControler, decoration: InputDecoration( border: UnderlineInputBorder(), filled: true, @@ -96,6 +99,7 @@ class _MyHomePageState extends State { }, ), TextField( + controller: passwdControler, decoration: InputDecoration( border: UnderlineInputBorder(), filled: true, @@ -112,6 +116,10 @@ class _MyHomePageState extends State { child: Text('connection'), onPressed: connection, ), + RaisedButton( + child: Text('disconnect'), + onPressed: disconnect, + ), ], ); } else { @@ -126,6 +134,14 @@ class _MyHomePageState extends State { ), ), dense: true, + onTap: () { + setState(() { + ssidControler.text = ssidList[index -1].ssid; + // passwdControler.text = ""; + ssid = ssidControler.text; + // password = passwdControler.text; + }); + }, ), Divider(), ]); @@ -161,4 +177,10 @@ class _MyHomePageState extends State { print(v); }); } + + Future disconnect() async { + Wifi.disconnect(ssid).then((v) { + print(v); + }); + } } diff --git a/lib/wifi.dart b/lib/wifi.dart index 835ae51..6c43576 100644 --- a/lib/wifi.dart +++ b/lib/wifi.dart @@ -48,6 +48,23 @@ class Wifi { return WifiState.error; } } + + static Future disconnect(String ssid) async { + final Map params = { + 'ssid': ssid, + }; + int state = await _channel.invokeMethod('disconnect', params); + switch (state) { + case 0: + return WifiState.error; + case 1: + return WifiState.success; + case 2: + return WifiState.already; + default: + return WifiState.error; + } + } } class WifiResult {