android AP 热点介绍 和接口使用
来源:互联网 发布:淘宝天猫卷怎么抢到 编辑:程序博客网 时间:2023/12/03 09:19
以下基于android ics系统
Android AP接口属性为 @hide,不对外开放,但通过revoke机制调用到。
Ap的几个重要接口
getWifiApState
setWifiApEnabled
getWifiApConfiguration
isWifiApEnabled
使用方法:
Java代码 

- package com.lenovo.channel.method.ap.hotspot;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.util.HashMap;
- import java.util.Map;
- import android.net.wifi.WifiConfiguration;
- import android.net.wifi.WifiManager;
- import android.os.Build;
- import com.lenovo.channel.method.ap.hotspot.Hotspot.WifiApState;
- import com.lenovo.common.BeanUtils;
- import com.lenovo.common.Logger;
- class WifiApManager {
- private static final String tag = "WifiApManager";
- private static final String METHOD_GET_WIFI_AP_STATE = "getWifiApState";
- private static final String METHOD_SET_WIFI_AP_ENABLED = "setWifiApEnabled";
- private static final String METHOD_GET_WIFI_AP_CONFIG = "getWifiApConfiguration";
- private static final String METHOD_IS_WIFI_AP_ENABLED = "isWifiApEnabled";
- private static final Map<String, Method> methodMap = new HashMap<String, Method>();
- private static Boolean mIsSupport;
- private static boolean mIsHtc;
- public synchronized static final boolean isSupport() {
- if (mIsSupport != null) {
- return mIsSupport;
- }
- boolean result = Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO;
- if (result) {
- try {
- Field field = WifiConfiguration.class.getDeclaredField("mWifiApProfile");
- mIsHtc = field != null;
- } catch (Exception e) {
- }
- }
- if (result) {
- try {
- String name = METHOD_GET_WIFI_AP_STATE;
- Method method = WifiManager.class.getMethod(name);
- methodMap.put(name, method);
- result = method != null;
- } catch (SecurityException e) {
- Logger.e(tag, "SecurityException", e);
- } catch (NoSuchMethodException e) {
- Logger.e(tag, "NoSuchMethodException", e);
- }
- }
- if (result) {
- try {
- String name = METHOD_SET_WIFI_AP_ENABLED;
- Method method = WifiManager.class.getMethod(name, WifiConfiguration.class, boolean.class);
- methodMap.put(name, method);
- result = method != null;
- } catch (SecurityException e) {
- Logger.e(tag, "SecurityException", e);
- } catch (NoSuchMethodException e) {
- Logger.e(tag, "NoSuchMethodException", e);
- }
- }
- if (result) {
- try {
- String name = METHOD_GET_WIFI_AP_CONFIG;
- Method method = WifiManager.class.getMethod(name);
- methodMap.put(name, method);
- result = method != null;
- } catch (SecurityException e) {
- Logger.e(tag, "SecurityException", e);
- } catch (NoSuchMethodException e) {
- Logger.e(tag, "NoSuchMethodException", e);
- }
- }
- if (result) {
- try {
- String name = getSetWifiApConfigName();
- Method method = WifiManager.class.getMethod(name, WifiConfiguration.class);
- methodMap.put(name, method);
- result = method != null;
- } catch (SecurityException e) {
- Logger.e(tag, "SecurityException", e);
- } catch (NoSuchMethodException e) {
- Logger.e(tag, "NoSuchMethodException", e);
- }
- }
- if (result) {
- try {
- String name = METHOD_IS_WIFI_AP_ENABLED;
- Method method = WifiManager.class.getMethod(name);
- methodMap.put(name, method);
- result = method != null;
- } catch (SecurityException e) {
- Logger.e(tag, "SecurityException", e);
- } catch (NoSuchMethodException e) {
- Logger.e(tag, "NoSuchMethodException", e);
- }
- }
- mIsSupport = result;
- return isSupport();
- }
- private final WifiManager mWifiManager;
- WifiApManager(WifiManager manager) {
- if (!isSupport()) {
- throw new RuntimeException("Unsupport Ap!");
- }
- Logger.i(tag, "Build.BRAND -----------> " + Build.BRAND);
- mWifiManager = manager;
- }
- public WifiManager getWifiManager() {
- return mWifiManager;
- }
- public int getWifiApState() {
- try {
- Method method = methodMap.get(METHOD_GET_WIFI_AP_STATE);
- return (Integer) method.invoke(mWifiManager);
- } catch (Exception e) {
- Logger.e(tag, e.getMessage(), e);
- }
- return WifiApState.WIFI_AP_STATE_UNKWON;
- }
- private WifiConfiguration getHtcWifiApConfiguration(WifiConfiguration standard){
- WifiConfiguration htcWifiConfig = standard;
- try {
- Object mWifiApProfileValue = BeanUtils.getFieldValue(standard, "mWifiApProfile");
- if (mWifiApProfileValue != null) {
- htcWifiConfig.SSID = (String)BeanUtils.getFieldValue(mWifiApProfileValue, "SSID");
- }
- } catch (Exception e) {
- Logger.e(tag, "" + e.getMessage(), e);
- }
- return htcWifiConfig;
- }
- public WifiConfiguration getWifiApConfiguration() {
- WifiConfiguration configuration = null;
- try {
- Method method = methodMap.get(METHOD_GET_WIFI_AP_CONFIG);
- configuration = (WifiConfiguration) method.invoke(mWifiManager);
- if(isHtc()){
- configuration = getHtcWifiApConfiguration(configuration);
- }
- } catch (Exception e) {
- Logger.e(tag, e.getMessage(), e);
- }
- return configuration;
- }
- public boolean setWifiApConfiguration(WifiConfiguration netConfig) {
- boolean result = false;
- try {
- if (isHtc()) {
- setupHtcWifiConfiguration(netConfig);
- }
- Method method = methodMap.get(getSetWifiApConfigName());
- Class<?>[] params = method.getParameterTypes();
- for (Class<?> clazz : params) {
- Logger.i(tag, "param -> " + clazz.getSimpleName());
- }
- if (isHtc()) {
- int rValue = (Integer) method.invoke(mWifiManager, netConfig);
- Logger.i(tag, "rValue -> " + rValue);
- result = rValue > 0;
- } else {
- result = (Boolean) method.invoke(mWifiManager, netConfig);
- }
- } catch (Exception e) {
- Logger.e(tag, "", e);
- }
- return result;
- }
- public boolean setWifiApEnabled(WifiConfiguration configuration, boolean enabled) {
- boolean result = false;
- try {
- Method method = methodMap.get(METHOD_SET_WIFI_AP_ENABLED);
- result = (Boolean)method.invoke(mWifiManager, configuration, enabled);
- } catch (Exception e) {
- Logger.e(tag, e.getMessage(), e);
- }
- return result;
- }
- public boolean isWifiApEnabled() {
- boolean result = false;
- try {
- Method method = methodMap.get(METHOD_IS_WIFI_AP_ENABLED);
- result = (Boolean)method.invoke(mWifiManager);
- } catch (Exception e) {
- Logger.e(tag, e.getMessage(), e);
- }
- return result;
- }
- private void setupHtcWifiConfiguration(WifiConfiguration config) {
- try {
- Logger.d(tag, "config= " + config);
- Object mWifiApProfileValue = BeanUtils.getFieldValue(config, "mWifiApProfile");
- if (mWifiApProfileValue != null) {
- BeanUtils.setFieldValue(mWifiApProfileValue, "SSID", config.SSID);
- BeanUtils.setFieldValue(mWifiApProfileValue, "BSSID", config.BSSID);
- BeanUtils.setFieldValue(mWifiApProfileValue, "secureType", "open");
- BeanUtils.setFieldValue(mWifiApProfileValue, "dhcpEnable", 1);
- }
- } catch (Exception e) {
- Logger.e(tag, "" + e.getMessage(), e);
- }
- }
- public static boolean isHtc() {
- return mIsHtc;
- }
- private static String getSetWifiApConfigName() {
- return mIsHtc? "setWifiApConfig": "setWifiApConfiguration";
- }
- }
下面是一个英文页面发现的文档,集合了很多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;
}
}
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;
}
}
- android AP 热点介绍 和接口使用
- Android AP 热点 使用经验
- android AP热点(wifi热点)开发
- ubuntu使用ap-hotspot建立WIFI热点
- ubuntu使用ap-hotspot建立WIFI热点
- 在ubuntu 12.04/12.10上创建能够给android手机(AP模式)使用的热点
- ubuntu14.04上创建能够给android手机(AP模式)使用的热点
- Deepin Linux系统中开启ap-hotspot wifi热点供其他计算机和android手机上网
- android通过反射获取wifi热点ap的ssid和password
- AP和CP的介绍
- Android AP模式创建有/无密码热点
- 使用usb tplink无线网卡搭建无线热点AP
- 无线AP和无线路由器区别 wifi热点
- 无线AP和无线路由器区别 wifi热点
- 无线AP和无线路由器区别wifi热点
- 树莓派和Ubuntu12.04配置无线连接与无线AP热点
- 无线AP和无线路由器区别 wifi热点
- arm-linux AP热点
- 嵌入式Linux网络编程
- IBM部门介绍:研发部(R&D)【CDL、CRL、CSTL】
- LCD的接口类型详解
- linux 下通过修改内核参数解决大量TIME_WAIT问题
- 一个关于RNA-Seq分析方法的投票
- android AP 热点介绍 和接口使用
- 考输出
- HBase中的奇怪问题之LeaseException
- Nginx防盗链详细解说
- destoon如何去掉后台左边菜单我的面板里面的使用帮助
- 查询外部表报错ORA-29913 ORA-29400
- 【Record】【read coding】
- arm-linux apache
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(二)----使用GUI.Box显示文字