Wifi模块—源码分析Wifi热点的开启(Android P)

一 前言

        Android使用一个修改版wpa_supplicant作为daemon来控制WIFI,它是一个安全中间件,代码位于external/wpa_supplicant,为各种无线网卡提供统一的安全机制。当然在这里只是介绍一下wpa_supplicant和 hostapd,研究分析的部分主要还是应用层和java框架层,有时也会涉及Native层。

wpa_supplicant_8主要有三个子目录 :

       hostapd:当手机进入Soft AP模式时,手机将扮演AP的角色,需要hostapd来提供AP的功能,也就是wifi热点的实现。

       wpa_supplicant:Station模式,也叫Managed模式,这是平时最常见的使用wifi连接AP的情况。

       src:hostapd和wpa_supplicant中都包含一些通用的数据结构和处理方法,这些内容都放在此src目录中。

 

二 图示调用流程

 

三 具体代码流程

       Android P之后,Wifi模块增加了packages/apps/Settings/src/com/android/settings/wifi/tether/路径,相当于把Wifi热点独立放到了tether文件夹下面,并添加了WifiTetherSettings.java类,对应着Wifi热点的界面,而Android P之前是没有的,Wifi热点界面之前是对应在TetherSettings的一部分,有些厂商也还是会改到TetherSettings上。

1 packages/apps/Settings/src/com/android/settings/wifi/tether/WifiTetherSettings.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Assume we are in a SettingsActivity. This is only safe because we currently use
    // SettingsActivity as base for all preference fragments.
    final SettingsActivity activity = (SettingsActivity) getActivity();
    final SwitchBar switchBar = activity.getSwitchBar();
    mSwitchBarController = new WifiTetherSwitchBarController(activity,
            new SwitchBarController(switchBar));
    getLifecycle().addObserver(mSwitchBarController);
    switchBar.show();
}

初始化mSwitchBarController,这个类含有SwitchBar的实例。

2 packages/apps/Settings/src/com/android/settings/wifi/tether/WifiTetherSwitchBarController.java

public class WifiTetherSwitchBarController implements SwitchWidgetController.OnSwitchChangeListener,
        LifecycleObserver, OnStart, OnStop, DataSaverBackend.Listener {
  

3 packages/apps/Settings/src/com/android/settings/widget/SwitchWidgetController.java

/**
* Interface definition for a callback to be invoked when the switch has been toggled.
*/
public interface OnSwitchChangeListener {
    /**
    * Called when the checked state of the Switch has changed.
    *
    * @param isChecked The new checked state of switchView.
    *
    * @return true to update the state of the switch with the new value.
    */
    boolean onSwitchToggled(boolean isChecked);
}

4 packages/apps/Settings/src/com/android/settings/wifi/tether/WifiTetherSwitchBarController.java

@Override
public boolean onSwitchToggled(boolean isChecked) {
    if (!isChecked) {
        stopTether();
    } else if (!mWifiManager.isWifiApEnabled()) {
        startTether();
    }
    return true;
}

startTether()。

void startTether() {
    mSwitchBar.setEnabled(false);
    mConnectivityManager.startTethering(TETHERING_WIFI, true /* showProvisioningUi */,
            mOnStartTetheringCallback, new Handler(Looper.getMainLooper()));
}

       android O开始通过mConnectivityManager.startTethering来启动热点了,之前都是通过WifiManager的setWifiApEnable的方法,该方法现在也已废弃。

5 frameworks/base/core/java/android/net/ConnectivityManager.java

/**
* Convenient overload for
* {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
* handler to run on the current thread's {@link Looper}.
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
public void startTethering(int type, boolean showProvisioningUi,
        final OnStartTetheringCallback callback) {
    startTethering(type, showProvisioningUi, callback, null);
}

startTethering。

/**
* Runs tether provisioning for the given type if needed and then starts tethering if
* the check succeeds. If no carrier provisioning is required for tethering, tethering is
* enabled immediately. If provisioning fails, tethering will not be enabled. It also
* schedules tether provisioning re-checks if appropriate.
*
* @param type The type of tethering to start. Must be one of
*         {@link ConnectivityManager.TETHERING_WIFI},
*         {@link ConnectivityManager.TETHERING_USB}, or
*         {@link ConnectivityManager.TETHERING_BLUETOOTH}.
* @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
*         is one. This should be true the first time this function is called and also any time
*         the user can see this UI. It gives users information from their carrier about the
*         check failing and how they can sign up for tethering if possible.
* @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
*         of the result of trying to tether.
* @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
public void startTethering(int type, boolean showProvisioningUi,
        final OnStartTetheringCallback callback, Handler handler) {
    Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");

    ResultReceiver wrappedCallback = new ResultReceiver(handler) {
        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            if (resultCode == TETHER_ERROR_NO_ERROR) {
                callback.onTetheringStarted();
            } else {
                callback.onTetheringFailed();
            }
        }
    };

    try {
        String pkgName = mContext.getOpPackageName();
        Log.i(TAG, "startTethering caller:" + pkgName);
        mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
    } catch (RemoteException e) {
        Log.e(TAG, "Exception trying to start tethering.", e);
        wrappedCallback.send(TETHER_ERROR_SERVICE_
  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值