252 lines
9.7 KiB
Java
252 lines
9.7 KiB
Java
package com.ifish.activity;
|
||
|
||
import android.app.Activity;
|
||
import android.app.Dialog;
|
||
import android.content.Intent;
|
||
import android.net.wifi.ScanResult;
|
||
import android.net.wifi.WifiInfo;
|
||
import android.net.wifi.WifiManager;
|
||
import android.os.Bundle;
|
||
import android.os.Handler;
|
||
import android.os.Message;
|
||
import android.text.TextUtils;
|
||
import android.view.View;
|
||
import android.view.Window;
|
||
import android.widget.Button;
|
||
import android.widget.EditText;
|
||
import android.widget.ImageView;
|
||
|
||
import androidx.annotation.Nullable;
|
||
|
||
import com.ifish.basebean.ConnectMessageEvent;
|
||
import com.ifish.baseclass.BaseActivity;
|
||
import com.ifish.utils.AnimationUtil;
|
||
import com.ifish.utils.SPUtil;
|
||
import com.ifish.utils.ToastUtil;
|
||
import com.ifish.utils.WifiAdmin;
|
||
import com.ifish.utils.WifiUtil;
|
||
import com.ifish.view.WifiDialog;
|
||
import com.ifish.wheelview.adapter.ArrayWheelAdapter;
|
||
import com.ifish.wheelview.view.WheelView;
|
||
|
||
import java.io.BufferedReader;
|
||
import java.io.InputStreamReader;
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
|
||
import de.greenrobot.event.EventBus;
|
||
|
||
public class HotSpotConnentActivity extends BaseActivity {
|
||
private EditText et_wifiname, et_wifipwd;
|
||
ImageView iv_wifiset;
|
||
private String device_mac;
|
||
private SPUtil sp;
|
||
private Activity act;
|
||
|
||
@Override
|
||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
act = this;
|
||
setContentView(R.layout.activity_hotconnect_device);
|
||
EventBus.getDefault().register(this);
|
||
initTitle("连接网络");
|
||
sp = SPUtil.getInstance(HotSpotConnentActivity.this);
|
||
et_wifiname = findMyViewById(R.id.et_wifiname);
|
||
et_wifipwd = findMyViewById(R.id.et_wifipwd);
|
||
iv_wifiset = findMyViewById(R.id.iv_wifiset);
|
||
iv_wifiset.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
showHeatWenduDialog();
|
||
}
|
||
});
|
||
findViewById(R.id.bt_setconnect).setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
// device_mac="5ccf7f99e929";
|
||
// doPostBindDevice();
|
||
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
|
||
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
||
if (!wifiManager.isWifiEnabled()) {
|
||
ToastUtil.show(getApplicationContext(), "请检查WiFi是否开启");
|
||
return;
|
||
}
|
||
String wifissid = wifiInfo.getSSID();
|
||
wifissid = wifissid.replaceAll("\"", "");
|
||
if (TextUtils.isEmpty(wifissid)) {
|
||
ToastUtil.show(getApplicationContext(), "您没有连接WiFi");
|
||
return;
|
||
}
|
||
String wifiname = et_wifiname.getText().toString();
|
||
String wifipwd = et_wifipwd.getText().toString();
|
||
if (TextUtils.isEmpty(wifiname)) {
|
||
ToastUtil.show(getApplicationContext(), "请输入路由器名称");
|
||
return;
|
||
}
|
||
List<ScanResult> scanResults = wifiManager.getScanResults();
|
||
// 是否是 2.4G
|
||
boolean is24G = false;
|
||
// 是否是 5G
|
||
boolean is5G = false;
|
||
for (ScanResult scanResult : scanResults) {
|
||
if (wifiname.equals(scanResult.SSID)) {
|
||
int frequency = scanResult.frequency;
|
||
WifiUtil wifiUtil = new WifiUtil(HotSpotConnentActivity.this);
|
||
if(wifiUtil.is24GHzWifi(frequency)){
|
||
is24G = true;
|
||
}
|
||
if (wifiUtil.is5GHzWifi(frequency)) {
|
||
is5G = true;
|
||
}
|
||
}
|
||
}
|
||
|
||
if(!is24G && is5G){ //之所以这样处理,是因为现在有的路由器支持2.4+5G双频同时存在
|
||
WifiDialog wd = new WifiDialog(HotSpotConnentActivity.this);
|
||
wd.show("仅支持2.4G Wi-Fi网络,请重新选择", "去更改", et_wifiname);
|
||
dismissProgressDialog();
|
||
return;
|
||
}
|
||
|
||
if (TextUtils.isEmpty(wifipwd)) {
|
||
ToastUtil.show(getApplicationContext(), "请输入路由器密码");
|
||
return;
|
||
}
|
||
/*if (wifipwd.toString().contains("’") || wifipwd.toString().contains("”")) {
|
||
ToastUtil.show(getApplicationContext(), "您输入密码带特殊符号,请更改路由器密码。");
|
||
return;
|
||
}*/
|
||
Intent intent = new Intent(act, HotSpotConnentSetWIFIActivity.class);
|
||
String devicetype = getIntent().getStringExtra("devicetype");
|
||
if ("camera".equals(devicetype)) {
|
||
intent.putExtra("devicetype", "camera");
|
||
} else if ("pet".equals(devicetype)) {
|
||
intent.putExtra("devicetype", "pet");
|
||
}
|
||
intent.putExtra("wifiname", wifiname);
|
||
intent.putExtra("wifipwd", wifipwd);
|
||
startActivity(intent);
|
||
AnimationUtil.startAnimation(act);
|
||
}
|
||
});
|
||
WifiAdmin wifiAdmin = new WifiAdmin(this);
|
||
sp = SPUtil.getInstance(HotSpotConnentActivity.this);
|
||
String wifissid = wifiAdmin.getSSID();
|
||
wifissid = wifiAdmin.updateSsid(wifissid);
|
||
String wifiname = wifissid;
|
||
if (TextUtils.isEmpty(wifissid)) {
|
||
wifiname = "";
|
||
} else {
|
||
if ("0x".equals(wifissid)) {//上次连接过wifi,但是中途断开,则会出现0x
|
||
wifiname = "";
|
||
}
|
||
}
|
||
et_wifiname.setText(wifiname);
|
||
}
|
||
|
||
|
||
@Override
|
||
protected void onDestroy() {
|
||
super.onDestroy();
|
||
EventBus.getDefault().unregister(this);
|
||
}
|
||
|
||
public static void isNetWorkAvailable(final String address, final Comparable<Boolean> callback) {
|
||
final Handler handler = new Handler() {
|
||
|
||
@Override
|
||
public void handleMessage(Message msg) {
|
||
super.handleMessage(msg);
|
||
if (callback != null) {
|
||
callback.compareTo(msg.arg1 == 0);
|
||
}
|
||
}
|
||
|
||
};
|
||
new Thread(new Runnable() {
|
||
|
||
@Override
|
||
public void run() {
|
||
Runtime runtime = Runtime.getRuntime();
|
||
Message msg = new Message();
|
||
try {
|
||
Process pingProcess = runtime.exec("/system/bin/ping -c 1 " + address);
|
||
InputStreamReader isr = new InputStreamReader(pingProcess.getInputStream());
|
||
BufferedReader buf = new BufferedReader(isr);
|
||
if (buf.readLine() == null) {
|
||
msg.arg1 = -1;
|
||
} else {
|
||
msg.arg1 = 0;
|
||
}
|
||
buf.close();
|
||
isr.close();
|
||
} catch (Exception e) {
|
||
msg.arg1 = -1;
|
||
e.printStackTrace();
|
||
} finally {
|
||
runtime.gc();
|
||
handler.sendMessage(msg);
|
||
}
|
||
}
|
||
|
||
}).start();
|
||
}
|
||
|
||
public List<ScanResult> getWifiList() {
|
||
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
|
||
List<ScanResult> scanWifiList = wifiManager.getScanResults();
|
||
List<ScanResult> wifiList = new ArrayList<>();
|
||
if (scanWifiList != null && scanWifiList.size() > 0) {
|
||
HashMap<String, Integer> signalStrength = new HashMap<String, Integer>();
|
||
for (int i = 0; i < scanWifiList.size(); i++) {
|
||
ScanResult scanResult = scanWifiList.get(i);
|
||
if (!scanResult.SSID.isEmpty()) {
|
||
String key = scanResult.SSID + " " + scanResult.capabilities;
|
||
if (!signalStrength.containsKey(key)) {
|
||
signalStrength.put(key, i);
|
||
wifiList.add(scanResult);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return wifiList;
|
||
}
|
||
|
||
private Dialog wifiDialog;
|
||
|
||
private void showHeatWenduDialog() {
|
||
List<ScanResult> wifiList = getWifiList();
|
||
if (wifiList == null || wifiList.size() == 0) {
|
||
ToastUtil.show(HotSpotConnentActivity.this, "未查找到周边WIFI");
|
||
return;
|
||
}
|
||
final String[] wifis = new String[wifiList.size()];
|
||
for (int i = 0; i < wifis.length; i++) {
|
||
wifis[i] = wifiList.get(i).SSID;
|
||
}
|
||
final WheelView wv_water;
|
||
Button bt_submit;
|
||
wifiDialog = new Dialog(HotSpotConnentActivity.this, R.style.HOLOMyDialogs);
|
||
wifiDialog.setCancelable(true);
|
||
Window window = wifiDialog.getWindow();
|
||
window.setContentView(R.layout.setwifi_dialog);
|
||
wv_water = (WheelView) window.findViewById(R.id.wv_water);
|
||
bt_submit = (Button) window.findViewById(R.id.bt_submit);
|
||
wv_water.setViewAdapter(new ArrayWheelAdapter<String>(HotSpotConnentActivity.this, wifis));
|
||
wifiDialog.show();
|
||
wv_water.setCurrentItem(0);
|
||
bt_submit.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
wifiDialog.dismiss();
|
||
et_wifiname.setText(wifis[wv_water.getCurrentItem()]);
|
||
}
|
||
});
|
||
}
|
||
|
||
public void onEventMainThread(ConnectMessageEvent event) {
|
||
finish();
|
||
}
|
||
}
|