package qas_tool;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import com.android.dx.stock.ProxyBuilder;
import java.io.File;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class WIFI_AP_tool {
private Context context;
private WifiManager wifiManager;
public WIFI_AP_tool(Context context){
this.context=context;
}
public boolean is_connect(){
ConnectivityManager connMgr =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isWifiConn = false;
boolean isMobileConn = false;
for (Network network : connMgr.getAllNetworks()) {
NetworkInfo networkInfo = connMgr.getNetworkInfo(network);
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
isWifiConn |= networkInfo.isConnected();
}
if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
isMobileConn |= networkInfo.isConnected();
}
}
return isWifiConn| isMobileConn;
}
public boolean is_AP_open(){
try
{
WifiManager wifiManager;
wifiManager = (WifiManager)context.getSystemService(context.WIFI_SERVICE);
final Method method = wifiManager.getClass().getDeclaredMethod("isWifiApEnabled");
method.setAccessible(true);
return (Boolean) method.invoke(wifiManager);
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
//開啟Ap
public void startTethering() {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
File outputDir = context.getCodeCacheDir();
Class classOnStartTetheringCallback = Class.forName("android.net.ConnectivityManager$OnStartTetheringCallback");
Method startTethering = connectivityManager.getClass().getDeclaredMethod("startTethering", int.class, boolean.class, classOnStartTetheringCallback);
Object proxy = ProxyBuilder.forClass(classOnStartTetheringCallback).dexCache(outputDir).handler(new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
return null;
}
}).build();
startTethering.invoke(connectivityManager, 0, false, proxy);
} catch (Exception e) {
e.printStackTrace();
}
}
//停止AP
public void stopTethering() {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
Method stopTethering = connectivityManager.getClass().getDeclaredMethod("stopTethering", int.class);
stopTethering.invoke(connectivityManager,0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
沒有留言:
張貼留言