1. 程式人生 > >Android 預設AP名字,以及AP名字儲存路徑

Android 預設AP名字,以及AP名字儲存路徑

最近在修改Settings.apk,其中有一條需求是修改預設的ap名字。於是我們跟蹤一下原始碼來解決一下困擾我的問題。

首先在Settings\src\com\android\settings\TetherSettings.java的程式碼中

    private void initWifiTethering() {
        final Activity activity = getActivity();
        mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        mWifiConfig = mWifiManager.getWifiApConfiguration
(); mSecurityType = getResources().getStringArray(R.array.wifi_ap_security); mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY); if (mWifiConfig == null) { final String s = activity.getString( com.android.internal.R.string.wifi_tether_configure_ssid_default);
mCreateNetwork.setSummary(String.format(activity.getString(CONFIG_SUBTEXT), s, mSecurityType[WifiApDialog.OPEN_INDEX])); } else { int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig); mCreateNetwork.setSummary(String.format(activity.getString
(CONFIG_SUBTEXT), mWifiConfig.SSID, mSecurityType[index])); } }

WifiManager的getWifiApConfiguration可以獲取ap相關資訊。進入getWifiApConfiguration檢視可以得知該類是呼叫WifiService,WifiService是呼叫WifiServiceImpl的getWifiApConfiguration。

    /**
     * see {@link WifiManager#getWifiApConfiguration()}
     * @return soft access point configuration
     */
    public WifiConfiguration getWifiApConfiguration() {
        enforceAccessPermission();
        return mWifiStateMachine.syncGetWifiApConfiguration();
    }

在WifiStateMachine.java中

    public WifiConfiguration syncGetWifiApConfiguration() {
        Message resultMsg = mWifiApConfigChannel.sendMessageSynchronously(CMD_REQUEST_AP_CONFIG);
        WifiConfiguration ret = (WifiConfiguration) resultMsg.obj;
        resultMsg.recycle();
        return ret;
    }

其中針對mWifiApConfigChannel的定義如下:

    class InitialState extends State {
        @Override
        public void enter() {
            mWifiNative.unloadDriver();
            if (mWifiP2pChannel == null) {
                mWifiP2pChannel = new AsyncChannel();
                mWifiP2pChannel.connect(mContext, getHandler(),
                    mWifiP2pServiceImpl.getP2pStateMachineMessenger());
            }
            if (mWifiApConfigChannel == null) {
                mWifiApConfigChannel = new AsyncChannel();
                WifiApConfigStore wifiApConfigStore = WifiApConfigStore.makeWifiApConfigStore(
                        mContext, getHandler());
                wifiApConfigStore.loadApConfiguration();
                mWifiApConfigChannel.connectSync(mContext, getHandler(),
                        wifiApConfigStore.getMessenger());
            }
        }

其中WifiApConfigStore wifiApConfigStore = WifiApConfigStore.makeWifiApConfigStore(
mContext, getHandler());就是我們最後所有問題的答案。在WifiApConfigStore.java這個類中:

    class WifiApConfigStore extends StateMachine {
    private Context mContext;
    private static final String TAG = "WifiApConfigStore";
    private static final String AP_CONFIG_FILE = Environment.getDataDirectory() +
        "/misc/wifi/softap.conf";

這個檔案就是存放修改ap名字的檔案。而:

    /* Generate a default WPA2 based configuration with a random password.
       We are changing the Wifi Ap configuration storage from secure settings to a
       flat file accessible only by the system. A WPA2 based default configuration
       will keep the device secure after the update */
    private void setDefaultApConfiguration() {
        WifiConfiguration config = new WifiConfiguration();
        config.SSID = mContext.getString(R.string.wifi_tether_configure_ssid_default);
        config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
        String randomUUID = UUID.randomUUID().toString();
        //first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
        config.preSharedKey = randomUUID.substring(0, 8) + randomUUID.substring(9,13);
        sendMessage(WifiStateMachine.CMD_SET_AP_CONFIG, config);
    }

也就是獲取預設ap名字和密碼的程式碼。