android AP 热点介绍 和接口使用

以下基于android ics系统

 

Android AP接口属性为 @hide,不对外开放,但通过revoke机制调用到。

 

Ap的几个重要接口

getWifiApState

setWifiApEnabled

getWifiApConfiguration

isWifiApEnabled

 

 

使用方法:

 

Java代码   收藏代码
  1. package com.lenovo.channel.method.ap.hotspot;  
  2.   
  3. import java.lang.reflect.Field;  
  4. import java.lang.reflect.Method;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. import android.net.wifi.WifiConfiguration;  
  9. import android.net.wifi.WifiManager;  
  10. import android.os.Build;  
  11.   
  12. import com.lenovo.channel.method.ap.hotspot.Hotspot.WifiApState;  
  13. import com.lenovo.common.BeanUtils;  
  14. import com.lenovo.common.Logger;  
  15.   
  16. class WifiApManager {  
  17.     private static final String tag = "WifiApManager";  
  18.   
  19.     private static final String METHOD_GET_WIFI_AP_STATE = "getWifiApState";  
  20.     private static final String METHOD_SET_WIFI_AP_ENABLED = "setWifiApEnabled";  
  21.     private static final String METHOD_GET_WIFI_AP_CONFIG = "getWifiApConfiguration";  
  22.     private static final String METHOD_IS_WIFI_AP_ENABLED = "isWifiApEnabled";  
  23.   
  24.     private static final Map<String, Method> methodMap = new HashMap<String, Method>();  
  25.     private static Boolean mIsSupport;  
  26.     private static boolean mIsHtc;  
  27.   
  28.     public synchronized static final boolean isSupport() {  
  29.         if (mIsSupport != null) {  
  30.             return mIsSupport;  
  31.         }  
  32.   
  33.         boolean result = Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO;  
  34.         if (result) {  
  35.             try {  
  36.                 Field field = WifiConfiguration.class.getDeclaredField("mWifiApProfile");  
  37.                 mIsHtc = field != null;  
  38.             } catch (Exception e) {  
  39.             }  
  40.         }  
  41.   
  42.         if (result) {  
  43.             try {  
  44.                 String name = METHOD_GET_WIFI_AP_STATE;  
  45.                 Method method = WifiManager.class.getMethod(name);  
  46.                 methodMap.put(name, method);  
  47.                 result = method != null;  
  48.             } catch (SecurityException e) {  
  49.                 Logger.e(tag, "SecurityException", e);  
  50.             } catch (NoSuchMethodException e) {  
  51.                 Logger.e(tag, "NoSuchMethodException", e);  
  52.             }  
  53.         }  
  54.           
  55.         if (result) {  
  56.             try {  
  57.                 String name = METHOD_SET_WIFI_AP_ENABLED;  
  58.                 Method method = WifiManager.class.getMethod(name, WifiConfiguration.classboolean.class);  
  59.                 methodMap.put(name, method);  
  60.                 result = method != null;  
  61.             } catch (SecurityException e) {  
  62.                 Logger.e(tag, "SecurityException", e);  
  63.             } catch (NoSuchMethodException e) {  
  64.                 Logger.e(tag, "NoSuchMethodException", e);  
  65.             }  
  66.         }  
  67.   
  68.         if (result) {  
  69.             try {  
  70.                 String name = METHOD_GET_WIFI_AP_CONFIG;  
  71.                 Method method = WifiManager.class.getMethod(name);  
  72.                 methodMap.put(name, method);  
  73.                 result = method != null;  
  74.             } catch (SecurityException e) {  
  75.                 Logger.e(tag, "SecurityException", e);  
  76.             } catch (NoSuchMethodException e) {  
  77.                 Logger.e(tag, "NoSuchMethodException", e);  
  78.             }  
  79.         }  
  80.   
  81.         if (result) {  
  82.             try {  
  83.                 String name = getSetWifiApConfigName();  
  84.                 Method method = WifiManager.class.getMethod(name, WifiConfiguration.class);  
  85.                 methodMap.put(name, method);  
  86.                 result = method != null;  
  87.             } catch (SecurityException e) {  
  88.                 Logger.e(tag, "SecurityException", e);  
  89.             } catch (NoSuchMethodException e) {  
  90.                 Logger.e(tag, "NoSuchMethodException", e);  
  91.             }  
  92.         }  
  93.   
  94.         if (result) {  
  95.             try {  
  96.                 String name = METHOD_IS_WIFI_AP_ENABLED;  
  97.                 Method method = WifiManager.class.getMethod(name);  
  98.                 methodMap.put(name, method);  
  99.                 result = method != null;  
  100.             } catch (SecurityException e) {  
  101.                 Logger.e(tag, "SecurityException", e);  
  102.             } catch (NoSuchMethodException e) {  
  103.                 Logger.e(tag, "NoSuchMethodException", e);  
  104.             }  
  105.         }  
  106.   
  107.         mIsSupport = result;  
  108.         return isSupport();  
  109.     }  
  110.   
  111.     private final WifiManager mWifiManager;  
  112.     WifiApManager(WifiManager manager) {  
  113.         if (!isSupport()) {  
  114.             throw new RuntimeException("Unsupport Ap!");  
  115.         }  
  116.         Logger.i(tag, "Build.BRAND -----------> " + Build.BRAND);  
  117.           
  118.         mWifiManager = manager;  
  119.     }  
  120.     public WifiManager getWifiManager() {  
  121.         return mWifiManager;  
  122.     }  
  123.   
  124.     public int getWifiApState() {  
  125.         try {  
  126.             Method method = methodMap.get(METHOD_GET_WIFI_AP_STATE);  
  127.             return (Integer) method.invoke(mWifiManager);  
  128.         } catch (Exception e) {  
  129.             Logger.e(tag, e.getMessage(), e);  
  130.         }  
  131.         return WifiApState.WIFI_AP_STATE_UNKWON;  
  132.     }  
  133.       
  134.     private WifiConfiguration getHtcWifiApConfiguration(WifiConfiguration standard){  
  135.         WifiConfiguration htcWifiConfig = standard;  
  136.         try {  
  137.             Object mWifiApProfileValue = BeanUtils.getFieldValue(standard, "mWifiApProfile");  
  138.   
  139.             if (mWifiApProfileValue != null) {  
  140.                 htcWifiConfig.SSID = (String)BeanUtils.getFieldValue(mWifiApProfileValue, "SSID");  
  141.             }  
  142.         } catch (Exception e) {  
  143.             Logger.e(tag, "" + e.getMessage(), e);  
  144.         }  
  145.         return htcWifiConfig;  
  146.     }  
  147.   
  148.     public WifiConfiguration getWifiApConfiguration() {  
  149.         WifiConfiguration configuration = null;  
  150.         try {  
  151.             Method method = methodMap.get(METHOD_GET_WIFI_AP_CONFIG);  
  152.             configuration = (WifiConfiguration) method.invoke(mWifiManager);  
  153.             if(isHtc()){  
  154.                 configuration = getHtcWifiApConfiguration(configuration);  
  155.             }  
  156.         } catch (Exception e) {  
  157.             Logger.e(tag, e.getMessage(), e);  
  158.         }  
  159.         return configuration;  
  160.     }  
  161.   
  162.     public boolean setWifiApConfiguration(WifiConfiguration netConfig) {  
  163.         boolean result = false;  
  164.         try {  
  165.             if (isHtc()) {  
  166.                 setupHtcWifiConfiguration(netConfig);  
  167.             }  
  168.   
  169.             Method method = methodMap.get(getSetWifiApConfigName());  
  170.             Class<?>[] params = method.getParameterTypes();  
  171.             for (Class<?> clazz : params) {  
  172.                 Logger.i(tag, "param -> " + clazz.getSimpleName());  
  173.             }  
  174.   
  175.             if (isHtc()) {  
  176.                 int rValue = (Integer) method.invoke(mWifiManager, netConfig);  
  177.                 Logger.i(tag, "rValue -> " + rValue);  
  178.                 result = rValue > 0;  
  179.             } else {  
  180.                 result = (Boolean) method.invoke(mWifiManager, netConfig);  
  181.             }  
  182.         } catch (Exception e) {  
  183.             Logger.e(tag, "", e);  
  184.         }  
  185.         return result;  
  186.     }  
  187.       
  188.     public boolean setWifiApEnabled(WifiConfiguration configuration, boolean enabled) {  
  189.         boolean result = false;  
  190.         try {  
  191.             Method method = methodMap.get(METHOD_SET_WIFI_AP_ENABLED);  
  192.             result = (Boolean)method.invoke(mWifiManager, configuration, enabled);  
  193.         } catch (Exception e) {  
  194.             Logger.e(tag, e.getMessage(), e);  
  195.         }  
  196.         return result;  
  197.     }  
  198.   
  199.     public boolean isWifiApEnabled() {  
  200.         boolean result = false;  
  201.         try {  
  202.             Method method = methodMap.get(METHOD_IS_WIFI_AP_ENABLED);  
  203.             result = (Boolean)method.invoke(mWifiManager);  
  204.         } catch (Exception e) {  
  205.             Logger.e(tag, e.getMessage(), e);  
  206.         }  
  207.         return result;  
  208.     }  
  209.   
  210.     private void setupHtcWifiConfiguration(WifiConfiguration config) {  
  211.         try {  
  212.             Logger.d(tag, "config=  " + config);  
  213.             Object mWifiApProfileValue = BeanUtils.getFieldValue(config, "mWifiApProfile");  
  214.   
  215.             if (mWifiApProfileValue != null) {  
  216.                 BeanUtils.setFieldValue(mWifiApProfileValue, "SSID", config.SSID);  
  217.                 BeanUtils.setFieldValue(mWifiApProfileValue, "BSSID", config.BSSID);  
  218.                 BeanUtils.setFieldValue(mWifiApProfileValue, "secureType""open");  
  219.                 BeanUtils.setFieldValue(mWifiApProfileValue, "dhcpEnable"1);  
  220.             }  
  221.         } catch (Exception e) {  
  222.             Logger.e(tag, "" + e.getMessage(), e);  
  223.         }  
  224.     }  
  225.       
  226.     public static boolean isHtc() {  
  227.         return mIsHtc;  
  228.     }  
  229.       
  230.     private static String getSetWifiApConfigName() {  
  231.         return mIsHtc? "setWifiApConfig""setWifiApConfiguration";  
  232.     }  
  233. }  

下面是一个英文页面发现的文档,集合了很多wifi连接的技术方案:

Android Wifi Hotspot Manager Class
08 2012 May 


Nick 
Android has the great option to let you Tether your connection via wifi, but as developer you got little to none control over this mechanism.


Therefore i wrote a class to correct this: WifiApManager.


With this class you can enable the check the current Status of the Hotspot, enable/disable it, get/set the current AP configuration and also get the list of currently connected clients.


 


I also made an example to demonstrate it’s capabilities:








Download example Sourcecode + binaries


 


And here the source-code of the class WifiApManager.java:


package com.whitebyte.wifihotspotutils;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.util.ArrayList;
 
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;
 
public class WifiApManager {
private final WifiManager mWifiManager;
 
public WifiApManager(Context context) {
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
 
/**
     * Start AccessPoint mode with the specified
     * configuration. If the radio is already running in
     * AP mode, update the new configuration
     * Note that starting in access point mode disables station
     * mode operation
     * @param wifiConfig SSID, security and channel details as part of WifiConfiguration
     * @return {@code true} if the operation succeeds, {@code false} otherwise
     */
public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
try {
if (enabled) { // disable WiFi in any case
mWifiManager.setWifiEnabled(false);
}
 
Method method = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
return (Boolean) method.invoke(mWifiManager, wifiConfig, enabled);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return false;
}
}
 
/**
     * Gets the Wi-Fi enabled state.
     * @return {@link WIFI_AP_STATE}
     * @see #isWifiApEnabled()
     */
public WIFI_AP_STATE getWifiApState() {
try {
Method method = mWifiManager.getClass().getMethod("getWifiApState");
 
int tmp = ((Integer)method.invoke(mWifiManager));
 
// Fix for Android 4
if (tmp > 10) {
tmp = tmp - 10;
}
 
return WIFI_AP_STATE.class.getEnumConstants()[tmp];
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return WIFI_AP_STATE.WIFI_AP_STATE_FAILED;
}
}
 
/**
     * Return whether Wi-Fi AP is enabled or disabled.
     * @return {@code true} if Wi-Fi AP is enabled
     * @see #getWifiApState()
     *
     * @hide Dont open yet
     */
    public boolean isWifiApEnabled() {
        return getWifiApState() == WIFI_AP_STATE.WIFI_AP_STATE_ENABLED;
    }
 
    /**
     * Gets the Wi-Fi AP Configuration.
     * @return AP details in {@link WifiConfiguration}
     */
    public WifiConfiguration getWifiApConfiguration() {
try {
Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");
return (WifiConfiguration) method.invoke(mWifiManager);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return null;
}
    }
 
    /**
     * Sets the Wi-Fi AP Configuration.
     * @return {@code true} if the operation succeeded, {@code false} otherwise
     */
    public boolean setWifiApConfiguration(WifiConfiguration wifiConfig) {
    try {
Method method = mWifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
return (Boolean) method.invoke(mWifiManager, wifiConfig);
} catch (Exception e) {
Log.e(this.getClass().toString(), "", e);
return false;
}
}
 
/**
     * Gets a list of the clients connected to the Hotspot, reachable timeout is 300
     * @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
     * @return ArrayList of {@link ClientScanResult}
     */
    public ArrayList<ClientScanResult> getClientList(boolean onlyReachables) {
    return getClientList(onlyReachables, 300);
    }
 
/**
     * Gets a list of the clients connected to the Hotspot
     * @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise
     * @param reachableTimeout Reachable Timout in miliseconds
     * @return ArrayList of {@link ClientScanResult}
     */
public ArrayList<ClientScanResult> getClientList(boolean onlyReachables, int reachableTimeout) {
BufferedReader br = null;
ArrayList<ClientScanResult> result = null;
 
try {
result = new ArrayList<ClientScanResult>();
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
 
if ((splitted != null) && (splitted.length >= 4)) {
// Basic sanity check
String mac = splitted[3];
 
if (mac.matches("..:..:..:..:..:..")) {
boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);
 
if (!onlyReachables || isReachable) {
result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
}
}
}
}
} catch (Exception e) {
Log.e(this.getClass().toString(), e.getMessage());
} finally {
try {
br.close();
} catch (IOException e) {
Log.e(this.getClass().toString(), e.getMessage());
}
}
 
return result;
}
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值