How to set secured hotspot in android (with password) - android

I want to set the hotspot on with it's ssid and password configured because i want to share it with some devices.
The code i am using below works fine for me but i am unable to set the password of hotspot.
if (wm.isWifiEnabled()) {
wm.setWifiEnabled(false);
}
WifiConfiguration wifiCon = new WifiConfiguration();
wifiCon.SSID = "UsmanAp";
wifiCon.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiCon.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
try{
Method setWifiApMethod = wm.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
boolean apstatus=(Boolean) setWifiApMethod.invoke(wm, wifiCon,true);
Method isWifiApEnabledmethod = wm.getClass().getMethod("isWifiApEnabled");
while(!(Boolean)isWifiApEnabledmethod.invoke(wm)){};
Method getWifiApStateMethod = wm.getClass().getMethod("getWifiApState");
int apstate=(Integer)getWifiApStateMethod.invoke(wm);
Method getWifiApConfigurationMethod = wm.getClass().getMethod("getWifiApConfiguration");
wifiCon=(WifiConfiguration)getWifiApConfigurationMethod.invoke(wm);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
}
What do add in the above code to set the password of the hotspot.
-Usman

You are not setting the preSharedKey, which is the password for WPA. Also the allowedKeyManagement should be set to WifiConfiguration.KeyMgmt.WPA_PSK.
WifiConfiguration wifiCon = new WifiConfiguration();
wifiCon.SSID = "UsmanAp";
wifiCon.preSharedKey = "password";
wifiCon.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiCon.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
try
{
Method setWifiApMethod = wm.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
boolean apstatus=(Boolean) setWifiApMethod.invoke(wm, wifiCon,true);
}
catch (Exception e)
{
Log.e(this.getClass().toString(), "", e);
}

Related

Android 7.1 couldn't enable wifi hotspot programmatically

I am trying that opening Wifi hotspot in android,When I run below code it runs successfully for Api 28 I can open Wifi hotspot without getting errror but When I run it in Api 25 for Android 7.1 device it gets below errors, it says get WRITE_SETTINGS permission,but When I tested for Api 28 it doesn't necessary for running application,Why can I get this errros and How can I run it for android Api 25
Getting Errors
W/System.err: java.lang.NoSuchMethodException: setWifiApEnabled [class android.net.wifi.WifiConfiguration, boolean]
at java.lang.Class.getMethod(Class.java:1981)
at java.lang.Class.getMethod(Class.java:1637)
at com.kocsistem.pixageoneandroid.utils.Utils.configApState(Utils.java:1126)
at com.kocsistem.pixageoneandroid.utils.Utils.openHotspot(Utils.java:1095)
at com.kocsistem.pixageoneandroid.network.broadcast.NetworkChangeReceiver$2.run(NetworkChangeReceiver.java:120)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Open Hotspot code
public static void openHotspot(Context context,boolean enable){
WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
assert manager != null;
try {
manager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() {
#SuppressLint("SetTextI18n")
#Override
public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
super.onStarted(reservation);
Log.i("Wifi Hotspot is on now , reservation is : %s", reservation.toString());
mReservation = reservation;
WifiManager.LocalOnlyHotspotReservation mReservation = reservation;
String key = mReservation.getWifiConfiguration().preSharedKey;
Log.i("key:",key);
String ussid = mReservation.getWifiConfiguration().SSID;
Log.i("ussid:",ussid);
}
#Override
public void onStopped() {
super.onStopped();
Log.i("onStopped: ","");
}
#Override
public void onFailed(int reason) {
super.onFailed(reason);
Log.i("onFailed: ","");
}
}, new Handler());
}catch (Exception e){
e.printStackTrace();
}
}else{
//for Api<26
configApState(context);
}
}
check whether wifi hotspot on or off
public static boolean isApOn(Context context) {
WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
try {
Method method = wifimanager.getClass().getDeclaredMethod("isWifiAp Enabled");
method.setAccessible(true);
return (Boolean) method.invoke(wifimanager);
}
catch (Throwable ignored) {}
return false;
}
toggle wifi hotspot on or off
public static boolean configApState(Context context) {
WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
WifiConfiguration wificonfiguration = null;
try {
// if WiFi is on, turn it off
if(isApOn(context)) {
wifimanager.setWifiEnabled(false);
}
Method method = wifimanager.getClass().getMethod("setWifiApEnabled ", WifiConfiguration.class, boolean.class);
method.invoke(wifimanager, wificonfiguration, !isApOn(context));
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
this code works for Android Api 26 (Oreo) and above , its not gonna work with nougat (api 25)
there is another way to do that , you have to get method called "setWifiApEnabled" Check this peace of code
PS: you have to convert the code from c# to java and dont forget to grant the write settings permission ...
private bool ActivateTethering(string ssid, string password)
{
var myConfiguration = new WifiConfiguration();
WifiManager wifimanager = (WifiManager)Context.GetSystemService(Context.WifiService);
myConfiguration.Ssid = ssid;
myConfiguration.PreSharedKey = password;
myConfiguration.AllowedAuthAlgorithms.Set((int)AuthAlgorithmType.Shared);
myConfiguration.AllowedProtocols.Set((int)ProtocolType.Rsn);
myConfiguration.AllowedProtocols.Set((int)ProtocolType.Wpa);
myConfiguration.AllowedKeyManagement.Set((int)KeyManagementType.WpaPsk);
var enableWifi = wifimanager.Class.GetDeclaredMethods();
try
{
bool setWifiConfig = false;
foreach (var method in enableWifi)
{
if (method.Name.Equals("setWifiApEnabled"))
{
setWifiConfig = (bool)method.Invoke(wifimanager, myConfiguration, true);
break;
}
}
}
catch (InvocationTargetException e)
{
Console.WriteLine(e.Data.ToString());
}
return setWifiConfig ;
}

Android app to dynamically turn on/off a wifi hotspot

I want to control wifi hotspot dynamically in my Android app project. I have tired Reflection (which will not work in Android Oreo and later versions), startLocalOnyNetwork (but I want specific SSID and PASSWORD, which is not possible to configure it).
Then I rooted my phone, Is it possible if the device is rooted ?
Expecting an api to turn on/off wifi hotspot with specific SSID and PASSWORD or use the previous one.
Any possibilities or workarounds ?
Thanks in advance.
To turn on Wifi Hotspot, need some permissions
<uses-permission android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
and the permission should be dynamically granted by user
In apps advanced settings -> Modify system settings
/**
* This enables tethering using the ssid/password defined in Settings App>Hotspot & tethering
* Does not require app to have system/privileged access
* Credit: Vishal Sharma - https://stackoverflow.com/a/52219887
*/
public boolean startTethering() {
File outputDir = mContext.getCodeCacheDir();
Object proxy;
try {
proxy = ProxyBuilder.forClass(OnStartTetheringCallbackClass())
.dexCache(outputDir).handler(new InvocationHandler() {
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return null;
}
}).build();
} catch (Exception e) {
Log.e(TAG, "Error in enableTethering ProxyBuilder");
e.printStackTrace();
return false;
}
Method method = null;
try {
method = mConnectivityManager.getClass().getDeclaredMethod("startTethering", int.class, boolean.class, OnStartTetheringCallbackClass(), Handler.class);
if (method == null) {
Log.e(TAG, "startTetheringMethod is null");
} else {
method.invoke(mConnectivityManager, ConnectivityManager.TYPE_MOBILE, false, proxy, null);
Log.d(TAG, "startTethering invoked");
}
return true;
} catch (Exception e) {
Log.e(TAG, "Error in enableTethering");
e.printStackTrace();
}
return false;
}
public void stopTethering() {
try {
Method method = mConnectivityManager.getClass().getDeclaredMethod("stopTethering", int.class);
if (method == null) {
Log.e(TAG, "stopTetheringMethod is null");
} else {
method.invoke(mConnectivityManager, ConnectivityManager.TYPE_MOBILE);
Log.d(TAG, "stopTethering invoked");
}
} catch (Exception e) {
Log.e(TAG, "stopTethering error: " + e.toString());
e.printStackTrace();
}
}
Use above methods to turn on/off Wifi Hotspot with SSID and password defined in the settings.
private int AP_STATE_DISABLED = 11;
private int AP_STATE_ENABLING = 12;
private int AP_STATE_ENABLED = 13;
private int AP_STATE_ERROR = 14;
/**
* #return status hot spot enabled or not
*/
public boolean isHotSpotEnabled(Context context) {
Method method = null;
int actualState = 0;
try {
WifiManager mWifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
method = mWifiManager.getClass().getDeclaredMethod("getWifiApState");
method.setAccessible(true);
actualState = (Integer) method.invoke(mWifiManager, (Object[]) null);
if (actualState == AP_STATE_ENABLING ||actualState == AP_STATE_ENABLED) {
return true;
}
} catch (IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Above method can be used to get the current state of hotspot

hotspot set number of users programmatically

i've used a method to create a hotspot and it's working
but i need to set a specific number of maximum connections that will be in this hotspot
thanks in advance
My current code :
private boolean configHotspot(String name,String pass) {
WifiConfiguration wifiCon = new WifiConfiguration();
wifiCon.SSID = name;
wifiCon.preSharedKey = pass;
wifiCon.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiCon.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiCon.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
try
{
Method setWifiApMethod = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
boolean apstatus=(Boolean) setWifiApMethod.invoke(wifimanager, wifiCon,true);
return apstatus;
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}

Detect USB tethering on android

Is there any way to know (pro grammatically) in your Activity/Application that the user has enabled USB tethering on his phone?
you can also use reflection to access the hidden function for setting usb tethering.
Here is my code.
ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
Log.d(TAG,"test enable usb tethering");
String[] available = null;
int code=-1;
Method[] wmMethods = cm.getClass().getDeclaredMethods();
for(Method method: wmMethods){
if(method.getName().equals("getTetherableIfaces")){
try {
available = (String[]) method.invoke(cm);
break;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
}
for(Method method: wmMethods){
if(method.getName().equals("tether")){
try {
code = (Integer) method.invoke(cm, available[0]);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
break;
}
}
if (code==0)
Log.d(TAG,"Enable usb tethering successfully!");
else
Log.d(TAG,"Enable usb tethering failed!");
For disabling usb tethering, you just need to change the reflection method name "getTetherableIfaces" to "getTetheredIfaces", change "tether" to "untether".
Please check.
Looking through the Settings.System documentation points to the answer being no, its not possible to do this.
Link to said documentation
This should work on all phones, confirmed on some Android 7,6 and 5 devices;
Method: interface rndisX (typically rndis0) only shows up when usb tethering is enabled.
Code Example:
private static boolean isTetheringActive(Context context){
try{
for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){
InetAddress inetAddress=enumIpAddr.nextElement();
if(!intf.isLoopback()){
if(intf.getName().contains("rndis")){
return true;
}
}
}
}
}catch(Exception e){e.printStackTrace();}
return false;
}
Here is a solution to Listen for tethering state changes :
First you need to be familiar with BroadcastReceiver.
you can find a lot of tutorial (google : how to listen for connectivity changes ...)
In order to get the Tethering state update, you need to use a hidden filter action of Android (see ConnectivityManager)
and in your BroadcastReceiver class :
IntentFilter filter = new IntentFilter("android.net.conn.TETHER_STATE_CHANGED");
then register the filter to your BroadcastReceiver :
myApplicationContext.registerReceiver(this, filter);
on your onReceive(final Context context,final Intent intent) method, the Intent.extras information contains 3 arrays filled with the corresponding tethered network interface :
erroredArray / availableArray / activeArray
It's a little bit tricky but you can get the tethering status information.
In addition, you can do some reflexion on hidden function of Android code :
Search for getTetherableIfaces() in the Connectivity Manager.
Here is a link : https://github.com/android/platform_frameworks_base/blob/master/core/java/android/net/ConnectivityManager.java#L1604
You can get the Network Interfaces and check what is active like this:
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
NetworkInterface rndis = null;
NetworkInterface wlan = null;
while(interfaces.hasMoreElements()) {
NetworkInterface nif = interfaces.nextElement();
if(hasIP4Address(nif)) {
if(nif.getDisplayName().startsWith("rndis"))
rndis = nif;
else if (nif.getDisplayName().startsWith("wlan"))
wlan = nif;
}
}
// Let the user choose Wi-Fi or rndis connect
if (rndis != null) {
socket.setNetworkInterface(rndis);
Log.i(TAG, "Subscribe: with interface rndis");
} else if(wlan != null) {
socket.setNetworkInterface(wlan);
Log.i(TAG, "Subscribe: with interface wlan");
}
I have found that if I check for usb0 network interface
it only has an ip address once tethering has been set up.
public static String getIPAddressUsb(final boolean useIPv4) {
try {
final List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (final NetworkInterface intf : interfaces) {
if (intf.getDisplayName().startsWith("usb")) {
final List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
for (final InetAddress addr : addrs) {
final String sAddr = addr.getHostAddress().toUpperCase();
final boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
if (useIPv4) {
if (isIPv4) { return sAddr; }
} else {
if (!isIPv4) {
final int delim = sAddr.indexOf('%');
return delim < 0 ? sAddr : sAddr.substring(0, delim);
}
}
}
}
}
} catch (final Exception ex) {
// for now eat exceptions
}
return "";
}
boolean isUsbTethered(){
String ipAddr = MipnAndroidApplication.getIPAddressUsb(true);
if (ipAddr.length() == 0) {
Log.i(LOG_TAG, "tethering not enabled");
return false;
} else {
Log.i(LOG_TAG, "tethering enabled :)");
return true;
}
}

Start / stop built-in Wi-Fi / USB tethering from code?

How can I start or stop the built-in tethering in Android 2.2 from my application?
There is a non-public Tethering API in the ConnectivityManager. As shown above you can use reflection to access it. I tried this on a number of Android 2.2 phones, and it works on all of them (my HTC turns on tethering but does NOT show this in the status bar..., so check from the other end). Below is some rough code which emits debugging stuff and turns on tethering on usb0.
ConnectivityManager cman = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Method[] methods = cman.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("getTetherableIfaces")) {
try {
String[] ifaces = (String[]) method.invoke(cman);
for (String iface : ifaces) {
Log.d("TETHER", "Tether available on " + iface);
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (method.getName().equals("isTetheringSupported")) {
try {
boolean supported = (Boolean) method.invoke(cman);
Log.d("TETHER", "Tether is supported: " + (supported ? "yes" : "no"));
} catch (Exception e) {
e.printStackTrace();
}
}
if (method.getName().equals("tether")) {
Log.d("TETHER", "Starting tether usb0");
try {
int result = (Integer) method.invoke(cman, "usb0");
Log.d("TETHER", "Tether usb0 result: " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Please note: this code requires the following permissions to work:
android.permission.ACCESS_NETWORK_STATE
android.permission.CHANGE_NETWORK_STATE
I answered this question here. In short, it is possible, here is the code:
private void setWifiTetheringEnabled(boolean enable) {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
Method[] methods = wifiManager.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("setWifiApEnabled")) {
try {
method.invoke(wifiManager, null, enable);
} catch (Exception ex) {
}
break;
}
}
}
Your app should have the following permission:
android.permission.CHANGE_WIFI_STATE
There are no public APIs in the Android SDK for managing the tethering -- sorry!
I used the code from Android How to turn on hotspot in Android Programmatically! and I enable the portable hotspot for android 4.2. Here's the code.
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// TODO Auto-generated method stub
WifiConfiguration wifi_configuration = null;
wifiManager.setWifiEnabled(false);
try
{
//USE REFLECTION TO GET METHOD "SetWifiAPEnabled"
Method method=wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
method.invoke(wifiManager, wifi_configuration, true);
}
catch (NoSuchMethodException e){
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Categories

Resources