Android wifi热点开关代码记录
Android wifi热点开关代码记录
一、前言
wifi和热点开关的代码可以用WifiManager也可以调用ConnectivityManager的接口实现。
下面记录一下。
二、代码实现
1、wifi开关
//wifi开关
public static void setWifiState(Context context, boolean isSetWifiEnable) {
WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int wifiState = mWifiManager.getWifiState();
DebugLog.debug("wifiState = " + wifiState + "(DISABLING = 0;DISABLED = 1;ENABLING = 2;ENABLED = 3;)");
DebugLog.debug("set wifi =isSetWifiEnable" + isSetWifiEnable);
mWifiManager.setWifiEnabled(isSetWifiEnable);
}
2、热点开关
//热点开关
public static void setWifiApState(Context context, boolean isSetWifiApEnable) {
WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int apState = mWifiManager.getWifiApState();
LogUtil.debug("apState = " + apState + "(DISABLING = 10;DISABLED = 11;ENABLING = 12;ENABLED = 13;)");
LogUtil.debug("set wifi =isSetWifiApEnable" + isSetWifiApEnable);
if (isSetWifiApEnable) {
mWifiManager.startTetheredHotspot(null); //根据可以设置热点配置SoftApConfiguration对象信息
} else {
mWifiManager.stopSoftAp();
}
}
3、第二种方式开关热点
//热点关闭2
public static void stopHotSpot(Context context) {
LogUtil.debug("stop hotspot.");
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
mConnectivityManager.stopTethering(ConnectivityManager.TETHERING_WIFI);
}
//热点开启2
public static void startHotSpot(Context context, long delaytime) {
LogUtil.debug("setup hotspot.");
WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int apState = mWifiManager.getWifiApState();
LogUtil.debug("apState = " + apState + "(DISABLING = 10;DISABLED = 11;ENABLING = 12;ENABLED = 13;)");
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
mConnectivityManager.startTethering(ConnectivityManager.TETHERING_WIFI, true /* showProvisioningUi */, mOnStartTetheringCallback, new Handler(Looper.getMainLooper()));
}
//热点开启回调监听
final static ConnectivityManager.OnStartTetheringCallback mOnStartTetheringCallback =
new ConnectivityManager.OnStartTetheringCallback() {
@Override
public void onTetheringFailed() {
super.onTetheringFailed();
LogUtil.error("onTetheringFailed");
}
@Override
public void onTetheringStarted() {
super.onTetheringStarted();
LogUtil.debug("onTetheringStarted");
}
};
具体情况,热点的两种开启控制代码,看哪种方便就用哪种。
上面看起来是使用WifiManager调用的方式比较方便,
虽然官方是提示废弃的,但是系统系统应用还是可以正常调用。
两个对象的方法调用好像都是要系统权限的!所以普通应用基本无法调用!
三、其他
1、热点启动流程
(1)ConnectivityManager.startTethering
(2)TetheringManager.startTethering(request, executor, tetheringCallback)
(3)TetheringService.TetheringConnector.startTethering
(4)Tethering.startTethering(request, listener);
//方法名变化,使用null 对象开启热点
(5)WifiManager.startTetheredHotspot(null /* use existing softap config */)
(6)WifiServiceImpl.startTetheredHotspot(@Nullable SoftApConfiguration softApConfig)
//方法名再变化
(7)ActiveModeWarden.startSoftAp(apModeConfig);
(8)ActiveModeManager.start();
ActiveModeManager manager = mWifiInjector.makeSoftApManager(listener, callback, softApConfig);
listener.setActiveModeManager(manager);
manager.start();
ActiveModeManager是接口类,会调用到SoftApManager.start()
(9)SoftApManager.startSoftAp()
(10)WifiNative.startSoftAp(mApInterfaceName, localConfigBuilder.build(), mSoftApListener)
(11)HostapdHal.addAccessPoint(ifaceName, config, listener::onFailure)
(12)根据硬件版本调用不同的接口实现:addAccessPoint_X_X
可以看到ConnectivityManager的后续流程也是调用到WifiManager流程往下走。
详解:
https://blog.csdn.net/wenzhi20102321/article/details/128473734