WifiConfiguration 这个是wifi热点的信息类
客户通常要求我们修改wifi热点的名称等。但是具体在什么地方修改,以及修改后系统将我们的修改保存到什么地方了呢?
通过对源码framework/opt分析后我们找到了WifiApConfigStore.java

/** Copyright (C) 2014 MediaTek Inc.* Modification based on code covered by the mentioned copyright* and/or permission notice(s).*//* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.android.server.wifi;import android.content.Context;import android.net.wifi.WifiConfiguration;import android.net.wifi.WifiConfiguration.KeyMgmt;import android.os.Environment;import android.util.Log;import com.android.internal.R;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.UUID;import android.os.SystemClock;import com.mediatek.common.MPlugin;import com.mediatek.common.wifi.IWifiFwkExt;import java.util.Random;import android.os.SystemProperties;/** * Provides API for reading/writing soft access point configuration. */public class WifiApConfigStore {    private static final String TAG = "WifiApConfigStore";    private static final String DEFAULT_AP_CONFIG_FILE =            Environment.getDataDirectory() + "/misc/wifi/softap.conf";    private static final int AP_CONFIG_FILE_VERSION = 2;    private WifiConfiguration mWifiApConfig = null;    private ArrayList mAllowed2GChannel = null;    private final Context mContext;    private final String mApConfigFile;    private final BackupManagerProxy mBackupManagerProxy;    WifiApConfigStore(Context context, BackupManagerProxy backupManagerProxy) {        this(context, backupManagerProxy, DEFAULT_AP_CONFIG_FILE);    }    WifiApConfigStore(Context context,                      BackupManagerProxy backupManagerProxy,                      String apConfigFile) {        mContext = context;        mBackupManagerProxy = backupManagerProxy;        mApConfigFile = apConfigFile;        String ap2GChannelListStr = mContext.getResources().getString(                R.string.config_wifi_framework_sap_2G_channel_list);        Log.d(TAG, "2G band allowed channels are:" + ap2GChannelListStr);        if (ap2GChannelListStr != null) {            mAllowed2GChannel = new ArrayList();            String channelList[] = ap2GChannelListStr.split(",");            for (String tmp : channelList) {                mAllowed2GChannel.add(Integer.parseInt(tmp));            }        }        /* Load AP configuration from persistent storage. */        mWifiApConfig = loadApConfiguration(mApConfigFile);        if (mWifiApConfig == null) {            /* Use default configuration. */            Log.d(TAG, "Fallback to use default AP configuration");            mWifiApConfig = getDefaultApConfiguration();            /* Save the default configuration to persistent storage. */            writeApConfiguration(mApConfigFile, mWifiApConfig);        }    }    /**     * Return the current soft access point configuration.     */    public synchronized WifiConfiguration getApConfiguration() {        return mWifiApConfig;    }    /**     * Update the current soft access point configuration.     * Restore to default AP configuration if null is provided.     * This can be invoked under context of binder threads (WifiManager.setWifiApConfiguration)     * and WifiStateMachine thread (CMD_START_AP).     */    public synchronized void setApConfiguration(WifiConfiguration config) {        if (config == null) {            mWifiApConfig = getDefaultApConfiguration();        } else {            mWifiApConfig = config;        }        writeApConfiguration(mApConfigFile, mWifiApConfig);        // Stage the backup of the SettingsProvider package which backs this up        mBackupManagerProxy.notifyDataChanged();    }    public ArrayList getAllowed2GChannel() {        return mAllowed2GChannel;    }    /**     * Load AP configuration from persistent storage.     */    private static WifiConfiguration loadApConfiguration(final String filename) {        WifiConfiguration config = null;        DataInputStream in = null;        try {            config = new WifiConfiguration();            in = new DataInputStream(                    new BufferedInputStream(new FileInputStream(filename)));            int version = in.readInt();            if ((version != 1) && (version != 2)) {                Log.e(TAG, "Bad version on hotspot configuration file");                return null;            }            config.SSID = in.readUTF();            if (version >= 2) {                config.apBand = in.readInt();                config.apChannel = in.readInt();            }            int authType = in.readInt();            config.allowedKeyManagement.set(authType);            if (authType != KeyMgmt.NONE) {                config.preSharedKey = in.readUTF();            }        } catch (IOException e) {            Log.e(TAG, "Error reading hotspot configuration " + e);            config = null;        } finally {            if (in != null) {                try {                    in.close();                } catch (IOException e) {                    Log.e(TAG, "Error closing hotspot configuration during read" + e);                }            }        }        return config;    }    /**     * Write AP configuration to persistent storage.     */    private static void writeApConfiguration(final String filename,                                             final WifiConfiguration config) {        try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(                        new FileOutputStream(filename)))) {            out.writeInt(AP_CONFIG_FILE_VERSION);            out.writeUTF(config.SSID);            out.writeInt(config.apBand);            out.writeInt(config.apChannel);            int authType = config.getAuthType();            out.writeInt(authType);            if (authType != KeyMgmt.NONE) {                out.writeUTF(config.preSharedKey);            }        } catch (IOException e) {            Log.e(TAG, "Error writing hotspot configuration" + e);        }    }    /**     * 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 WifiConfiguration getDefaultApConfiguration() {        WifiConfiguration config = new WifiConfiguration();        IWifiFwkExt wifiFwkExt = MPlugin.createInstance(IWifiFwkExt.class.getName(), mContext);        if (SystemProperties.get("ro.mtk_bsp_package").equals("1")) {            if (wifiFwkExt != null) {                config.SSID = wifiFwkExt.getApDefaultSsid();            } else {                config.SSID = mContext.getResources().getString(                        R.string.wifi_tether_configure_ssid_default);            }        } else {            config.SSID = com.mediatek.custom.CustomProperties.getString(                        com.mediatek.custom.CustomProperties.MODULE_WLAN,                        com.mediatek.custom.CustomProperties.SSID,                        mContext.getString(R.string.wifi_tether_configure_ssid_default));            if (wifiFwkExt != null && wifiFwkExt.needRandomSsid()) {                Random random = new Random(SystemClock.elapsedRealtime());                config.SSID = config.SSID + random.nextInt(1000);                Log.d(TAG, "setDefaultApConfiguration, SSID:" + config.SSID);            }        }        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);        return config;    }}

其中:

    private static final String DEFAULT_AP_CONFIG_FILE =            Environment.getDataDirectory() + "/misc/wifi/softap.conf";

就是我们wifi信息默认保存的地方。

    private WifiConfiguration getDefaultApConfiguration() {        WifiConfiguration config = new WifiConfiguration();        IWifiFwkExt wifiFwkExt = MPlugin.createInstance(IWifiFwkExt.class.getName(), mContext);        if (SystemProperties.get("ro.mtk_bsp_package").equals("1")) {            if (wifiFwkExt != null) {                config.SSID = wifiFwkExt.getApDefaultSsid();            } else {                config.SSID = mContext.getResources().getString(                        R.string.wifi_tether_configure_ssid_default);            }        } else {            config.SSID = com.mediatek.custom.CustomProperties.getString(                        com.mediatek.custom.CustomProperties.MODULE_WLAN,                        com.mediatek.custom.CustomProperties.SSID,                        mContext.getString(R.string.wifi_tether_configure_ssid_default));            if (wifiFwkExt != null && wifiFwkExt.needRandomSsid()) {                Random random = new Random(SystemClock.elapsedRealtime());                config.SSID = config.SSID + random.nextInt(1000);                Log.d(TAG, "setDefaultApConfiguration, SSID:" + config.SSID);            }        }        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);        return config;    }

上面就是wifi默认信息获取的地方,如我们可以修改wifi热点默认名称等。

更多相关文章

  1. android studio 将一个android项目当做另外一个android项目的lib
  2. 仿写Arouter框架实现Activity的跳转
  3. 设置android软键盘,默认不弹出的方法
  4. Ubuntu下为AndroidStudio编译并使用x264(二)AndroidStudio部分
  5. Android(安卓)Studio的各种冷知识,黑科技
  6. Android(安卓)Studio修改项目编译版本
  7. Android(安卓)2.3 CTS测试修改测试结果和调试方法
  8. 获取andrid apn信息
  9. 全志A40i Android7永不休眠及不锁屏的修改方法

随机推荐

  1. Creating a ContextMenu on a ListView
  2. 完成第一个android APP的UI项目
  3. Android 1.6 关于SD卡的新特性
  4. 修改dialog背景以及代码扩展AlertDialog
  5. Android 从代码中打开淘宝和亚马逊、face
  6. Android:资源 id 及资源 id 的动态获取
  7. Android(安卓)Button 你真的会用吗(含福利
  8. Android学习之表格布局管理器
  9. 电话接听并查看该人姓名
  10. Android 智能手机比较