Hello I'm trying to create an access point from my app. I can stand up the AP but not with the configuration that I set. I am using an htc Sensation XE
WifiManager wifiManager = (WifiManager) rc.getSystemService(Context.WIFI_SERVICE);
if(wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(false);
}
Method[] wmMethods = wifiManager.getClass().getDeclaredMethods();
boolean methodFound=false;
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = "MyWifiAP";
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
for(Method method: wmMethods){
if(method.getName().equals("setWifiApEnabled")){
methodFound=true;
try {
boolean apstatus=(Boolean) method.invoke(wifiManager, netConfig,true);
for (Method isWifiApEnabledmethod: wmMethods){
if(isWifiApEnabledmethod.getName().equals("isWifiApEnabled")){
while(!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){
};
for(Method method1: wmMethods){
if(method1.getName().equals("getWifiApState")){
int apstate;
apstate=(Integer)method1.invoke(wifiManager);
for(Method method2: wmMethods){
if(method2.getName().equals("getWifiApConfiguration")){
try {
netConfig=(WifiConfiguration)method2.invoke(wifiManager);
} 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();
}
Log.e("CLIENT", "\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");
if (apstate==0) {
Log.d("basura", "apstate es: "+apstate);
}
}
}
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
When I print:
Log.e("CLIENT", "\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");
I get:
nSSID:null
nPassword:null
I have the same problem with my HTC Desire and Android 2.2.2. It seems that many(or all) HTC devices can't be configured this way. HTC has probably rewrote some of the code and restrict some of the hidden features. You are using methods, which are not offically in the API (eg setWifiApEnabled) through reflection. In my case the hotspot will be created, but with the default configuration. Probably you have the same issue. It will work for some other devices thought
Related
I want to create a hidden WiFi hotspot programmatically with an android device,but I can't find an answer with [developer.android.google.cn][1]
[1]: http:///https://developer.android.google.cn/reference/android/net/wifi/WifiConfiguration. just now I saw a property named hiddenSSID, I tried to change it's value(true/false),but it did not work,could anybody help me?
private void stratWifiAp(String mSSID, String mPasswd) {
Method method1 = null;
try {
method1 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = mSSID;
netConfig.preSharedKey = mPasswd;
netConfig.hiddenSSID = true;
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(4);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
method1.invoke(mWifiManager, netConfig, true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
Try to modify your code to be something like this:
private void stratWifiAp(String mSSID, String mPasswd) {
Method method1 = null;
try {
//通过反射机制打开热点
method1 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
WifiConfiguration netConfig = new WifiConfiguration();
netConfig.SSID = mSSID;
netConfig.preSharedKey = mPasswd;
netConfig.hiddenSSID = true;
netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
netConfig.allowedKeyManagement.set(4);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
method1.invoke(mWifiManager, netConfig, true);
return netConfig;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
//return netConfig; or try to add here IF ONLY YOU ADDED THE ABOVE ONE!
}
i would like to get data from my host server by using a data connection in mobile.
when my app is open it should connect to host server by data connection automatically.
when in case of connection lost ... my service should automatically connect to the host sever by enabling the data connection from mobile not using with Bluetooth or WiFi.
please help me from this situation
in my code i am using to check whether data connection is
ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
//some task
return true;
}
else{
Toast.makeText(this, "No internet connection", Toast.LENGTH_SHORT).show();
}
return false;
Use this class as it is and call it methods when ever you want to Enalbe /Disable DataConnection
package com.AZone.eabc;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import android.net.ConnectivityManager;
import android.util.Log;
import android.content.Context;
public class InternetControl {
public static void EnableInternet(Context mycontext)
{
try {
Log.i("Reached Enable", "I am here");
setMobileDataEnabled(mycontext,true);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException 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 (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void DisableInternet(Context mycontext)
{
try {
Log.i("Reached Disable", "I am here");
setMobileDataEnabled(mycontext,false);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException 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 (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void setMobileDataEnabled(Context context , boolean enabled) throws NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}
}
Call methods of this class from AnyWhere like this
InternetControl.EnableInternet(getBaseContext());
ADD following permissions in You AndroidManifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
Looks like it doesn't work on some phones witn Android 4.1.X.
I've been using this approach for a long time but it crashes on Samsung 5282 Android 4.1.2:
"NoSuchFieldException: mService"
Looks like Samsung removed mService field by itself because it still can be found in the GrepCode:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/net/ConnectivityManager.java/
Use this method to check,Is your device connected to Internet or not.
public boolean checkInternetConnection() {
ConnectivityManager conMgr = (ConnectivityManager)activity.getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
Put this inside the click event:
boolean Connection;
Connection = checkInternetConnection();
if(Connection==false){
//No internet connection
}
else{
//Here do what ever you do next
}
currently I am building a very small native app for my android phone. I want to active the portable wi-fi hotspot when I click one button in the app. But I didn't know how to invoke Android API to active portable wi-fi hotspot. I know how to do it through the UI. Can anyone show me?
// code to enable portable wifi hotspot
public void EnableWifiHotspot(){
try{
WifiManager wifi_manager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifi_configuration = null;
Method wifiHotspotEnabledMethod=wifi_manager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
wifiHotspotEnabledMethod.invoke(wifi_manager, wifi_configuration, true);
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
//code for disabling portable wifi hotspot
public void DisableWifiHotspot(){
try{
WifiManager wifi_manager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifi_configuration = null;
Method wifiHotspotEnabledMethod=wifi_manager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
wifiHotspotEnabledMethod.invoke(wifi_manager, wifi_configuration, false);
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
i'm trying to use the method setMobileDataEnabled of the ConnectivityManager class, using SDK 2.2.
According to http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2.1_r1/android/net/ConnectivityManager.java/?v=source this method is declared public but with #hide is not available in the SDK and in Eclipse.
In order to bypass the hiding I wrote the following function to toggle the mobile data connection on/off.
public void setMobileData(boolean toBeEnabled){
Object myObj= getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) myObj;
Class c = null;
try {
c = Class.forName(cm.getClass().getName());
} catch (ClassNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Method m = null;
try {
m = c.getDeclaredMethod("getMobileDataEnabled");
} catch (SecurityException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (NoSuchMethodException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Object mobileDataEnabled=null;
if (m!=null){
m.setAccessible(true);
Type res_of_m= m.getGenericReturnType();
Type[] pars_of_m= m.getGenericParameterTypes();
try {
mobileDataEnabled = (m.invoke(cm));
if (mobileDataEnabled!=null)
if (mobileDataEnabled.equals(!toBeEnabled)){
Method m2 = null;
try {
int index=0;
boolean method_found=false;
Method[] available_methods= c.getDeclaredMethods();
for (Method method : available_methods) {
// following line doesn't work
// method.getName()=="setMobileDataEnabled"
if (method.getName().contains("setMobileDataEnabled"))
{
method_found=true;
}
if (method_found==false)
index++;
}
// following line doesn't work
//m2 = c.getDeclaredMethod("setMobileDataEnabled");
m2 = (c.getDeclaredMethods())[index];
if (m2!=null){
m2.setAccessible(true);
m2.invoke(cm,toBeEnabled);
}
} catch (SecurityException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (InvocationTargetException e2) {
// TODO Auto-generated catch block
e2.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();
}
}
}
To make it working I also added the android.permission.WRITE_SECURE_SETTINGS" in the manifest and installed in /system/app according to Android: Add app to firmware, use WRITE_SECURE_SETTINGS.
Does anyone know a better way?
try {
final ConnectivityManager cm = (ConnectivityManager) this.context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class connectivityManager = Class.forName(cm.getClass()
.getName());
final Method[] methods = connectivityManager.getDeclaredMethods();
for (final Method method : methods) {
if (MLauncherConnectivityManager.SET_MOBILE_DATA_ENABLED
.equals(method.getName())) {
method.invoke(cm, true);
}
}
} catch (final ClassNotFoundException e) {
Log.e("Class", e.getMessage());
} catch (final IllegalArgumentException e) {
Log.e("Class", e.getMessage());
} catch (final IllegalAccessException e) {
Log.e("Class", e.getMessage());
} catch (final InvocationTargetException e) {
Log.e("Class", e.getMessage());
}
Android 2.2 i.e API Level 8 has tabStripEnabled="true" for TabWidget
how to achieve the same in Older versions of Android?
private void SetupTabs(TabHost tabHost) {
LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
TabWidget tw = (TabWidget) ll.getChildAt(0);
Field mBottomLeftStrip;
Field mBottomRightStrip;
try {
mBottomLeftStrip = tw.getClass().getDeclaredField("mBottomLeftStrip");
mBottomRightStrip = tw.getClass().getDeclaredField("mBottomRightStrip");
if (!mBottomLeftStrip.isAccessible()) {
mBottomLeftStrip.setAccessible(true);
}
if (!mBottomRightStrip.isAccessible()) {
mBottomRightStrip.setAccessible(true);
}
mBottomLeftStrip.set(tw, getResources().getDrawable(R.drawable.blank));
mBottomRightStrip.set(tw, getResources().getDrawable(R.drawable.blank));// blank is the name of the image in drawable folder
}
catch (java.lang.NoSuchFieldException e) {
// possibly 2.2
try {
Method stripEnabled = tw.getClass().getDeclaredMethod("setStripEnabled", boolean.class);
stripEnabled.invoke(tw, false);
}
catch (Exception e1) {
e1.printStackTrace();
}
}
catch (Exception e) {}
}
I made it so:
try {
Method setStripEnabled = tabWidget.getClass().getDeclaredMethod(
"setStripEnabled", boolean.class);
setStripEnabled.invoke(tabWidget, true);
Method setLeftStripDrawable = tabWidget.getClass()
.getDeclaredMethod("setLeftStripDrawable", int.class);
setLeftStripDrawable.invoke(tabWidget, R.drawable.tab_line);
Method setRightStripDrawable = tabWidget.getClass()
.getDeclaredMethod("setRightStripDrawable", int.class);
setRightStripDrawable.invoke(tabWidget, R.drawable.tab_line);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}