|
@@ -6,6 +6,8 @@ import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.content.IntentFilter;
|
|
|
import android.content.pm.PackageManager;
|
|
|
+import android.net.ConnectivityManager;
|
|
|
+import android.net.NetworkInfo;
|
|
|
import android.net.wifi.ScanResult;
|
|
|
import android.net.wifi.WifiManager;
|
|
|
import android.os.Build;
|
|
@@ -36,17 +38,22 @@ public class MainActivity extends FlutterActivity {
|
|
|
|
|
|
|
|
|
public static WifiManager wifiManager;
|
|
|
+ public static ConnectivityManager connectivityManager;
|
|
|
|
|
|
private static final int PERMISSIONS_REQUEST_CODE = 1;
|
|
|
private BroadcastReceiver wifiScanReceiver;
|
|
|
|
|
|
+ private BroadcastReceiver netWorkChangeReceiver;
|
|
|
+
|
|
|
@Override
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
+ connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
// 检查并请求定位权限
|
|
|
checkAndRequestPermissions();
|
|
|
// 注册Wi-Fi扫描广播接收器
|
|
|
registerWifiScanReceiver();
|
|
|
+ registerNetWorkChangeReceiver();
|
|
|
// 检查并启用Wi-Fi
|
|
|
if (!wifiManager.isWifiEnabled()) {
|
|
|
wifiManager.setWifiEnabled(true);
|
|
@@ -54,18 +61,12 @@ public class MainActivity extends FlutterActivity {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
}
|
|
|
|
|
|
- private BroadcastReceiver mReceiver =new BroadcastReceiver() {
|
|
|
- @Override
|
|
|
- public void onReceive(Context context, Intent intent) {
|
|
|
- if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
|
|
|
- List<ScanResult> results = wifiManager.getScanResults();
|
|
|
- if (results !=null) {
|
|
|
- System.out.println("results size");
|
|
|
- System.out.println(results.size());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
+ @Override
|
|
|
+ protected void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ unregisterReceiver(wifiScanReceiver);
|
|
|
+ unregisterReceiver(netWorkChangeReceiver);
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
|
|
@@ -86,9 +87,9 @@ public class MainActivity extends FlutterActivity {
|
|
|
// List<Map<String,String>> wifiList = new ArrayList<>();
|
|
|
JSONObject response = new JSONObject();
|
|
|
for (ScanResult scanResult : scanResults) {
|
|
|
- if(!scanResult.SSID.equals("endo-lighting")){
|
|
|
- continue;
|
|
|
- }
|
|
|
+ if(!scanResult.SSID.equals("endo-lighting")){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
try {
|
|
|
response.put("cmd","scanResult");
|
|
|
response.put("ssid",scanResult.SSID);
|
|
@@ -130,4 +131,41 @@ public class MainActivity extends FlutterActivity {
|
|
|
registerReceiver(wifiScanReceiver, intentFilter);
|
|
|
}
|
|
|
|
|
|
+ private void registerNetWorkChangeReceiver(){
|
|
|
+ netWorkChangeReceiver = new BroadcastReceiver() {
|
|
|
+ @Override
|
|
|
+ public void onReceive(Context context, Intent intent) {
|
|
|
+ if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
|
|
|
+ NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
|
|
+ if (networkInfo != null && networkInfo.isConnected()) {
|
|
|
+ if(networkInfo.isConnected()){
|
|
|
+ System.out.println("wifi成功连接");
|
|
|
+ // Wi-Fi连接成功
|
|
|
+ JSONObject response = new JSONObject();
|
|
|
+ try {
|
|
|
+ response.put("cmd","connected");
|
|
|
+ } catch (JSONException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ EventChannelPlugin.getInstance().sendEventData(response.toString());
|
|
|
+ } else if (!networkInfo.isConnected()) {
|
|
|
+ System.out.println("wifi断开");
|
|
|
+ // Wi-Fi连接成功
|
|
|
+ JSONObject response = new JSONObject();
|
|
|
+ try {
|
|
|
+ response.put("cmd","disconnect");
|
|
|
+ } catch (JSONException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ EventChannelPlugin.getInstance().sendEventData(response.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ IntentFilter intentFilter = new IntentFilter();
|
|
|
+ intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
|
|
+ registerReceiver(netWorkChangeReceiver, intentFilter);
|
|
|
+ }
|
|
|
+
|
|
|
}
|