Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disconnect #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.android.tools.build:gradle:3.3.2'
}
}

Expand Down
91 changes: 91 additions & 0 deletions android/src/main/java/com/ly/wifi/WifiDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand All @@ -241,13 +258,82 @@ 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);
}
}
}

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 + "\"";
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions android/src/main/java/com/ly/wifi/WifiPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.android.tools.build:gradle:3.3.2'
}
}

Expand Down
22 changes: 22 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class _MyHomePageState extends State<MyHomePage> {
String _ip = 'click button to get ip.';
List<WifiResult> ssidList = [];
String ssid = '', password = '';
TextEditingController ssidControler = TextEditingController();
TextEditingController passwdControler = TextEditingController();

@override
void initState() {
Expand Down Expand Up @@ -83,6 +85,7 @@ class _MyHomePageState extends State<MyHomePage> {
],
),
TextField(
controller: ssidControler,
decoration: InputDecoration(
border: UnderlineInputBorder(),
filled: true,
Expand All @@ -96,6 +99,7 @@ class _MyHomePageState extends State<MyHomePage> {
},
),
TextField(
controller: passwdControler,
decoration: InputDecoration(
border: UnderlineInputBorder(),
filled: true,
Expand All @@ -112,6 +116,10 @@ class _MyHomePageState extends State<MyHomePage> {
child: Text('connection'),
onPressed: connection,
),
RaisedButton(
child: Text('disconnect'),
onPressed: disconnect,
),
],
);
} else {
Expand All @@ -126,6 +134,14 @@ class _MyHomePageState extends State<MyHomePage> {
),
),
dense: true,
onTap: () {
setState(() {
ssidControler.text = ssidList[index -1].ssid;
// passwdControler.text = "";
ssid = ssidControler.text;
// password = passwdControler.text;
});
},
),
Divider(),
]);
Expand Down Expand Up @@ -161,4 +177,10 @@ class _MyHomePageState extends State<MyHomePage> {
print(v);
});
}

Future<Null> disconnect() async {
Wifi.disconnect(ssid).then((v) {
print(v);
});
}
}
17 changes: 17 additions & 0 deletions lib/wifi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ class Wifi {
return WifiState.error;
}
}

static Future<WifiState> disconnect(String ssid) async {
final Map<String, dynamic> 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 {
Expand Down