AOSP定制 A13修改默认Wifi直连投屏热点名称密码

文章介绍了如何在Android13系统中修改默认的Wi-Fi直连投屏热点名称,由于源码闭源,提出了两种解决方案:一是替换开源代码,二是通过创建BroadcastReceiver在系统启动后重新设置。具体实现涉及WifidefaultBroadcastReceiver.java的编写,以及在特定时机如Launcher启动和网络重置时发送广播来更新热点名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.在Android13上按照之前的方式更改

packages\modules\Wifi\service\java\com\android\server\wifi\WifiApConfigStore.java
private SoftApConfiguration getDefaultApConfiguration() {
.............
--        configBuilder.setSsid(mContext.getResources().getString(
                R.string.wifi_tether_configure_ssid_default) + "_" + getRandomIntForDefaultSsid());
++      configBuilder.setSsid("默认name");
 ...............

在A13是这样更改是不生效的,打印log也不走,原因是由于packages\modules\Wifi被闭源了。系统实际上packages\modules\Wifi编译出来的是com.android.wifi.apex,使用的是com.google.android.wifi.apex。那么方案就有两种:1.暴力把闭源的换成开开源的即可 (这种就不讲了比较简单)2.等系统开机后重新设置一次

二.研究源码即可抽出来如下代码:

1.WifidfaultBroadcastReceiver.java

(建议加在系统应用里面)

import android.content.Intent;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.util.Log;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import java.util.UUID;
import android.provider.Settings;
import android.text.TextUtils;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Looper;
import android.os.Handler;
import android.os.Message;


public class WifidfaultBroadcastReceiver extends BroadcastReceiver{
   private Context mContext;
   private SoftApConfiguration mSoftApConfig;
   private  WifiManager mWifiManager;
   private SoftApConfiguration config;
   private static final String DEFAULT_SSID = "AndroidAP";
   private static final String TAG = "WifidfaultBroadcastReceiverTAG";
   private String mSSID;
   private int mBandIndex;
   private boolean mIs5GhzSupported = false;
   private boolean mSettingOn;
   private WifiP2pManager mWifiP2pManager;
   private WifiP2pManager.Channel mChannel;
   private boolean mWifi =true;
    
    @Override
    public void onReceive(Context context, Intent intent) {
	 Log.i(TAG, "WifidfaultBroadcastReceiver onReceive");
        mContext = context;
        //设置热点默认相关参数
        mSoftApConfigSet();
        //设置投屏名字 wifi直连名称也是这个
        setDefaultWifiDictName("默认名称");
    }
    
    
    private WifiP2pManager getWifiP2pManager(Context context) {
        return  (WifiP2pManager)context.getSystemService(Context.WIFI_P2P_SERVICE);
    }

    private WifiManager getWifiManager(Context context) {
        return  (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        }

    private boolean initChannel() {
        if (mWifiP2pManager == null) {
            mWifiP2pManager = getWifiP2pManager(mContext);
        }
        if (mChannel != null) {
            return true;
        }
        if (mWifiP2pManager != null) {
            mChannel = mWifiP2pManager.initialize(mContext,
            Looper.getMainLooper(), null);
        }
        if (mChannel == null) {
            Log.e(TAG, "Failed to set up connection with wifi p2p service");
            return false;
        }
        return true;
    }
    private void setDefaultWifiDictName(String name){
        //设置个标志位,防止每次开机都会被重置
        Settings.System.putInt(mContext.getContentResolver(), "defWp2pName_tag", 1);
        mWifiManager =  getWifiManager(mContext);
        mWifi =  mWifiManager.isWifiEnabled();
        if(!mWifi){
            mWifiManager.setWifiEnabled(true);
            //必须添加,否则会导致热点名称设置失败
            mWifiManager.startScan();
        } 
        initChannel();
        mWifiP2pManager.setDeviceName(mChannel,
                name,
                new WifiP2pManager.ActionListener() {
                    public void onSuccess() {
                        if (true) Log.d(TAG, "rename success");
                        mWifiManager.setWifiEnabled(mWifi);
                    }
                    public void onFailure(int reason) {
                        if (true) Log.d(TAG, "rename Failed");
                        mWifiManager.setWifiEnabled(mWifi);
                    }
                });
    }

    private boolean mSoftApConfigSet(){
        mWifiManager = mContext.getSystemService(WifiManager.class);
        config = mWifiManager.getSoftApConfiguration();
        mIs5GhzSupported = mWifiManager.is5GHzBandSupported();
        mSoftApConfig = buildNewConfig();
        mWifiManager.setSoftApConfiguration(mSoftApConfig);
        return true;
    }
    
    
    private String getSSID() {
        final SoftApConfiguration config = (mSoftApConfig == null) ? mWifiManager.getSoftApConfiguration() : mSoftApConfig;
        if (config != null) {
            mSSID = "默认名字";
        } else {
            mSSID = DEFAULT_SSID;
        }
        return mSSID;
    }

        private SoftApConfiguration buildNewConfig() {
        final SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder();
        final int securityType = 2;
        //热点名称
            configBuilder.setSsid(getSSID());
        // 安全性
        if (securityType != SoftApConfiguration.SECURITY_TYPE_OPEN) {
            configBuilder.setPassphrase(
                    mGetPasswordValidated(),
                    securityType);
        }
        //频段
        configBuilder.setBand(getBandIndex());
       //隐藏SSID
        configBuilder.setHiddenSsid(false);
        //最大连接数
        configBuilder.setMaxNumberOfClients(10);
        //白名单
        configBuilder.setClientControlByUserEnabled(getIsWhiteListMode());
        //白名单
        configBuilder.setAllowedClientList(config.getAllowedClientList());
        //自动关闭热点
        configBuilder.setAutoShutdownEnabled(config.isAutoShutdownEnabled());
        return configBuilder.build();
    }
    
    //   public int getMaximumClientNumber() {
    //      return mClientNumberValue;
    //  }
    private String mGetPasswordValidated(){
    return "12345678";//要求密码8位+
    /*    if(TextUtils.isEmpty(config.getPassphrase())){
            return generateRandomPassword();
        }else{
            return config.getPassphrase();
        }*/
    }

    private int getBandIndex() {
        final SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
    if (config == null) {
        mBandIndex = SoftApConfiguration.BAND_2GHZ;
        //Log.d(TAG, "Updating band index to BAND_2GHZ because no config");
    } else if (is5GhzBandSupported()) {
        mBandIndex = validateSelection(config.getBand());
        // Log.d(TAG, "Updating band index to " + mBandIndex);
    } else {
        mWifiManager.setSoftApConfiguration(
                new SoftApConfiguration.Builder(config).setBand(SoftApConfiguration.BAND_2GHZ)
                    .build());
        mBandIndex = SoftApConfiguration.BAND_2GHZ;
        // Log.d(TAG, "5Ghz not supported, updating band index to 2GHz");
    }
        return mBandIndex;
    }

    private int validateSelection(int band) {
        // unsupported states:
        // 1: BAND_5GHZ only - include 2GHZ since some of countries doesn't support 5G hotspot
        // 2: no 5 GHZ support means we can't have BAND_5GHZ - default to 2GHZ
        if (SoftApConfiguration.BAND_5GHZ == band) {
            if (!is5GhzBandSupported()) {
                return SoftApConfiguration.BAND_2GHZ;
            }
            return SoftApConfiguration.BAND_5GHZ | SoftApConfiguration.BAND_2GHZ;
        }

        return band;
    }

    private boolean is5GhzBandSupported() {
        final String countryCode = mWifiManager.getCountryCode();
        if (!mIs5GhzSupported || countryCode == null) {
            return false;
        }
        return true;
    }
    
    public boolean getIsWhiteListMode() {
        mSettingOn = config.isClientControlByUserEnabled();
        return mSettingOn;
    }
    
    private static String generateRandomPassword() {
        String randomUUID = UUID.randomUUID().toString();
        //first 12 chars from xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
        return randomUUID.substring(0, 8) + randomUUID.substring(9, 13);
    }

}

2.AndroidManifest.xml

<receiver android:name=".WifidfaultBroadcastReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="action.WifidfaultBroadcastReceiver" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
 </receiver>

3.添加重置时机

launcher开机后必定走,所以我将此处设置为重置时机

packages\apps\Launcher3\src\com\android\launcher3\Launcher.java
    protected void onCreate(Bundle savedInstanceState) {
++       boolean defWp2pName =  Settings.System.getInt(this.getContentResolver(), "defWp2pName_tag", 0) != 1;
++        if(defWp2pName){
++           sendWifidfaultBroadcastReceiver(this);
++      }
    ...............
    }
    
++    private void sendWifidfaultBroadcastReceiver(Context context){
++		Intent intent = new Intent("action.WifidfaultBroadcastReceiver");
++		intent.setComponent(new ComponentName("刚才新增广播的包名","包名.WifidfaultBroadcastReceiver "));
++      intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
++		context.sendBroadcast(intent);
++	}

还有一个时机即用户重置WLAN移动数据网络和蓝牙设置的时候,

packages/apps/Settings/src/com/android/settings/ResetNetworkConfirm.java
 @Override
 protected void onPostExecute(Boolean succeeded) {
..............
  if (succeeded) {
       Toast.makeText(mContext, R.string.reset_network_complete_toast, Toast.LENGTH_SHORT).show();
				sendWifidfaultBroadcastReceiver(mContext);
..............		

++private void sendWifidfaultBroadcastReceiver(Context context){
++		Intent intent = new Intent("action.WifidfaultBroadcastReceiver");
++		intent.setComponent(new ComponentName("刚才新增广播的包名","包名.WifidfaultBroadcastReceiver "));
++      intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
++		context.sendBroadcast(intent);
++	}

4.添加标志位

frameworks\base\packages\SettingsProvider\src\com\android\providers\settings\DatabaseHelper.java
    private void loadSystemSettings(SQLiteDatabase db) {
        SQLiteStatement stmt = null;
        try {
++ loadSetting(stmt, "defWp2pName_tag", 0);
.....................
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值