package com.application.wifitoggle;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity {
//WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//WifiConfiguration wifiConfiguration = new WifiConfiguration();
//
public static String TAG
= "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hotspotenable
= (Button) findViewById
(R.
id.
button);
try {
Toast.makeText(getApplicationContext(),getCurrentHotspotSSID() + getCurrentHotspotPassword(),Toast.LENGTH_SHORT).show();
e.printStackTrace();
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.
System.
canWrite(this.
getApplicationContext())) {
} else {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + this.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
hotspotenable.
setOnClickListener(new View.
OnClickListener() { @Override
public void onClick
(View v
) { try {
CreateNewWifiApNetwork(getCurrentHotspotSSID(), getCurrentHotspotPassword());
e.printStackTrace();
e.printStackTrace();
}
}
});
}
apManager ap = new apManager(this.getApplicationContext());
if (ap.isApOn() == true)
hotspotenable.setText("Turn On");
else
hotspotenable.setText("Turn Off");
WifiManager wifiManager
= (WifiManager
) getApplicationContext
().
getSystemService(Context.
WIFI_SERVICE); Method[] methods
= wifiManager.
getClass().
getDeclaredMethods(); if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);
ssid = config.SSID;
password = config.preSharedKey;
//or other info about the current hotspot, accessible through config
}
}
ap.createNewNetwork(ssid, password);
}
//String ssid = null;
//String password = null;
apManager ap = new apManager(this.getApplicationContext());
if (ap.isApOn() == true)
hotspotenable.setText("Turn On");
else
hotspotenable.setText("Turn Off");
/*
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method[] methods = wifiManager.getClass().getDeclaredMethods();
for (Method m: methods) {
if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);
ssid = config.SSID;
password = config.preSharedKey;
//or other info about the current hotspot, accessible through config
}
}
*/
ap.createNewNetwork(ssid, password);
}
WifiManager wifiManager
= (WifiManager
) getApplicationContext
().
getSystemService(Context.
WIFI_SERVICE); Method[] methods
= wifiManager.
getClass().
getDeclaredMethods(); if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);
strSSID = config.SSID;
}
}
return strSSID;
}
WifiManager wifiManager
= (WifiManager
) getApplicationContext
().
getSystemService(Context.
WIFI_SERVICE); Method[] methods
= wifiManager.
getClass().
getDeclaredMethods(); if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = (WifiConfiguration) m.invoke(wifiManager);
strPassword=config.preSharedKey;
}
}
return strPassword;
}
// private void changeStateWifiAp(boolean activated) {
// Method method;
// try {
// method = wifiManager.getClass().getDeclaredMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
// method.invoke(wifiManager, wifiConfiguration, activated);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
}