|
@@ -8,7 +8,9 @@ import android.content.Intent;
|
|
|
import android.content.IntentFilter;
|
|
|
import android.net.ConnectivityManager;
|
|
|
import android.net.NetworkInfo;
|
|
|
+import android.net.wifi.WifiConfiguration;
|
|
|
import android.net.wifi.WifiManager;
|
|
|
+import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.support.design.widget.CoordinatorLayout;
|
|
|
import android.support.design.widget.FloatingActionButton;
|
|
@@ -17,9 +19,13 @@ import android.support.v4.content.ContextCompat;
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
import android.view.KeyEvent;
|
|
|
import android.view.View;
|
|
|
+import android.widget.Button;
|
|
|
import android.widget.EditText;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
/**
|
|
|
* Created by Mikhael LOPEZ on 14/12/2015.
|
|
|
*/
|
|
@@ -39,6 +45,11 @@ public class MainActivity extends AppCompatActivity {
|
|
|
private View textViewMessage;
|
|
|
private TextView textViewIpAccess;
|
|
|
|
|
|
+ private WifiManager wifiManager;
|
|
|
+ private Button open;
|
|
|
+ boolean flag = false;
|
|
|
+ private String deviceId = getUniquePsuedoID();
|
|
|
+
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
@@ -72,10 +83,102 @@ public class MainActivity extends AppCompatActivity {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
+ //获取wifi管理服务
|
|
|
+ wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
+ open = (Button)findViewById(R.id.open_hotspot);
|
|
|
+ //通过按钮事件设置热点
|
|
|
+ open.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ //如果是打开状态就关闭,如果是关闭就打开
|
|
|
+ flag=!flag;
|
|
|
+ setWifiApEnabled(flag);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
// INIT BROADCAST RECEIVER TO LISTEN NETWORK STATE CHANGED
|
|
|
initBroadcastReceiverNetworkStateChanged();
|
|
|
}
|
|
|
|
|
|
+ public static String getUniquePsuedoID()
|
|
|
+ {
|
|
|
+ // If all else fails, if the user does have lower than API 9 (lower
|
|
|
+ // than Gingerbread), has reset their phone or 'Secure.ANDROID_ID'
|
|
|
+ // returns 'null', then simply the ID returned will be solely based
|
|
|
+ // off their Android device information. This is where the collisions
|
|
|
+ // can happen.
|
|
|
+ // Thanks http://www.pocketmagic.net/?p=1662!
|
|
|
+ // Try not to use DISPLAY, HOST or ID - these items could change.
|
|
|
+ // If there are collisions, there will be overlapping data
|
|
|
+ String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);
|
|
|
+
|
|
|
+ // Thanks to @Roman SL!
|
|
|
+ // http://stackoverflow.com/a/4789483/950427
|
|
|
+ // Only devices with API >= 9 have android.os.Build.SERIAL
|
|
|
+ // http://developer.android.com/reference/android/os/Build.html#SERIAL
|
|
|
+ // If a user upgrades software or roots their phone, there will be a duplicate entry
|
|
|
+ String serial = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ serial = android.os.Build.class.getField("SERIAL").get(null).toString();
|
|
|
+ // Go ahead and return the serial for api => 9
|
|
|
+ return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ // String needs to be initialized
|
|
|
+ serial = "serial"; // some value
|
|
|
+ }
|
|
|
+
|
|
|
+ // Thanks @Joe!
|
|
|
+ // http://stackoverflow.com/a/2853253/950427
|
|
|
+ // Finally, combine the values we have found by using the UUID class to create a unique identifier
|
|
|
+ return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setWifiApEnabled(boolean flag) {
|
|
|
+ if(flag) createWifiHotspot("", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建Wifi热点
|
|
|
+ */
|
|
|
+ private void createWifiHotspot(String WIFI_HOTSPOT_SSID, String key) {
|
|
|
+ if (wifiManager.isWifiEnabled()) {
|
|
|
+ //如果wifi处于打开状态,则关闭wifi,
|
|
|
+ wifiManager.setWifiEnabled(false);
|
|
|
+ }
|
|
|
+ WifiConfiguration config = new WifiConfiguration();
|
|
|
+ config.SSID = WIFI_HOTSPOT_SSID;
|
|
|
+ config.preSharedKey = key;
|
|
|
+ config.hiddenSSID = false;
|
|
|
+ config.allowedAuthAlgorithms
|
|
|
+ .set(WifiConfiguration.AuthAlgorithm.OPEN);//开放系统认证
|
|
|
+ config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
|
|
|
+ config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
|
|
|
+ config.allowedPairwiseCiphers
|
|
|
+ .set(WifiConfiguration.PairwiseCipher.TKIP);
|
|
|
+ config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
|
|
|
+ config.allowedPairwiseCiphers
|
|
|
+ .set(WifiConfiguration.PairwiseCipher.CCMP);
|
|
|
+ config.status = WifiConfiguration.Status.ENABLED;
|
|
|
+ //通过反射调用设置热点
|
|
|
+ try {
|
|
|
+ Method method = wifiManager.getClass().getMethod(
|
|
|
+ "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
|
|
|
+ boolean enable = (Boolean) method.invoke(wifiManager, config, true);
|
|
|
+ if (enable) {
|
|
|
+ Snackbar.make(coordinatorLayout, "WIFI 热点创建成功,SSID:"+WIFI_HOTSPOT_SSID + ",密码:"+key, Snackbar.LENGTH_LONG).show();
|
|
|
+ } else {
|
|
|
+ Snackbar.make(coordinatorLayout, "WIFI 热点创建失败",Snackbar.LENGTH_LONG).show();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ Snackbar.make(coordinatorLayout, "WIFI 热点创建失败"+ e.getMessage(),Snackbar.LENGTH_LONG).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
//region Start And Stop AndroidWebServer
|
|
|
private boolean startAndroidWebServer() {
|
|
|
if (!isStarted) {
|
|
@@ -86,11 +189,14 @@ public class MainActivity extends AppCompatActivity {
|
|
|
}
|
|
|
androidWebServer = new AndroidWebServer(port);
|
|
|
androidWebServer.start();
|
|
|
+ setWifiApEnabled(true);
|
|
|
return true;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
Snackbar.make(coordinatorLayout, "The PORT " + port + " doesn't work, please change it between 1000 and 9999.", Snackbar.LENGTH_LONG).show();
|
|
|
}
|
|
|
+ }else{
|
|
|
+ setWifiApEnabled(false);
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
@@ -123,7 +229,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
}
|
|
|
|
|
|
private String getIpAccess() {
|
|
|
- WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
|
|
|
+ WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
|
|
|
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
|
|
|
final String formatedIpAddress = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
|
|
|
return "http://" + formatedIpAddress + ":";
|
|
@@ -135,7 +241,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
}
|
|
|
|
|
|
public boolean isConnectedInWifi() {
|
|
|
- WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
|
|
|
+ WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
NetworkInfo networkInfo = ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
|
|
|
if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()
|
|
|
&& wifiManager.isWifiEnabled() && networkInfo.getTypeName().equals("WIFI")) {
|