Android 8.0内置wifi

Android 8.0系统内wifi的连接信息是保存在WifiConfigStore.xml文件中,路径是data/misc/wifi/WifiConfigStore.xml,user版本的系统是没有权限访问这个路径的,所以我是编译出userdebug版本进行调试的.下面介绍如何内置自己的wifi.多说一下,为什么要内置wifi?

我做的是工行的项目,像这种拥有支行或者分店比较多的公司,一般网络名称和密码都是固定的,所以他们要求,只要内置好工行wifi,放到任何网点和支行都可以自动连接相同ssid的wifi.下面简单介绍我内置wifi的思路:

第一步:将自己的wifi热点设置为ICBCOTP,密码设为12341234,然后通过wifi输入密码验证进行连接,连接完成后会在已保存的网络里面看到它,它的wifi信息就保存在data/misc/wifi/WifiConfigStore.xml文件中

 第二步:既然已经知道wifi信息保存的位置,那就去路径下找到WifiConfigStore.xml文件,然后看下信息是如何保存的,我的版本是userdebug版本,调试前先执行adb root,adb remount,然后再通过adb shell命令进去,这样的话就不会提示Permission denied了,

命令截图如下:

 第三步:将WifiConfigStore.xml从系统内pull出来,看下wifi信息如何存储的,通过命令adb pull /data/misc/wifi/WifiConfigStore.xml将文件从系统里拿出来,WifiConfigStore.xml里存储的信息就是我们想要内置的wifi信息,我们只需要将这个信息提前放进这个文件里就行了,WifiConfigStore.xml文件中保存ICBCOTP的信息如下:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<WifiConfigStoreData>
<int name="Version" value="1" />
<NetworkList>
<Network>
<WifiConfiguration>
<string name="ConfigKey">&quot;ICBCOTP&quot;WPA_PSK</string>
<string name="SSID">&quot;ICBCOTP&quot;</string>
<null name="BSSID" />
<boolean name="ShareThisAp" value="false" />
<string name="PreSharedKey">&quot;12341234&quot;</string>
<null name="WEPKeys" />
<int name="WEPTxKeyIndex" value="0" />
<boolean name="HiddenSSID" value="true" />
<boolean name="RequirePMF" value="false" />
<byte-array name="AllowedKeyMgmt" num="1">02</byte-array>
<byte-array name="AllowedProtocols" num="1">0b</byte-array>
<byte-array name="AllowedAuthAlgos" num="1">01</byte-array>
<byte-array name="AllowedGroupCiphers" num="1">0f</byte-array>
<byte-array name="AllowedPairwiseCiphers" num="1">06</byte-array>
<boolean name="Shared" value="true" />
<int name="Status" value="2" />
<null name="FQDN" />
<null name="ProviderFriendlyName" />
<null name="LinkedNetworksList" />
<null name="DefaultGwMacAddress" />
<boolean name="ValidatedInternetAccess" value="false" />
<boolean name="NoInternetAccessExpected" value="false" />
<int name="UserApproved" value="0" />
<boolean name="MeteredHint" value="false" />
<int name="MeteredOverride" value="0" />
<boolean name="UseExternalScores" value="false" />
<int name="NumAssociation" value="0" />
<int name="CreatorUid" value="1000" />
<string name="CreatorName">android.uid.system:1000</string>
<string name="CreationTime">time=08-17 20:38:57.597</string>
<int name="LastUpdateUid" value="1000" />
<string name="LastUpdateName">android.uid.system:1000</string>
<int name="LastConnectUid" value="0" />
<boolean name="IsLegacyPasspointConfig" value="false" />
<long-array name="RoamingConsortiumOIs" num="0" />
</WifiConfiguration>
<NetworkStatus>
<string name="SelectionStatus">NETWORK_SELECTION_ENABLED</string>
<string name="DisableReason">NETWORK_SELECTION_ENABLE</string>
<null name="ConnectChoice" />
<long name="ConnectChoiceTimeStamp" value="-1" />
<boolean name="HasEverConnected" value="false" />
</NetworkStatus>
<IpConfiguration>
<string name="IpAssignment">DHCP</string>
<string name="ProxySettings">NONE</string>
</IpConfiguration>
</Network>
</NetworkList>
<PasspointConfigData>
<long name="ProviderIndex" value="0" />
</PasspointConfigData>
</WifiConfigStoreData>

需要内置的wifi信息已经拿到,如何开机就写进去呢?我考虑的是先将WifiConfigStore.xml提前放到system/etc下,然后接收到开机广播之后就copy到/data/misc/wifi/下去覆盖原生WifiConfigStore.xml,这样系统就可以读取到我内置的wifi了,说做就做,提前将WifiConfigStore.xml放到/system/etc下,然后在设置里接收开机广播,然后去执行copy命令:

1,首先复制WifiConfigStore.xml文件到device/qcom/msm8953_64路径下,在device/qcom/msm8953_64/msm8953_64.mk中添加如下命令:

PRODUCT_COPY_FILES += device/qcom/msm8953_64/WifiConfigStore.xml:system/etc/WifiConfigStore.xml

2.然后接收开机广播,将文件拷贝到/data/misc/wifi/下,去覆盖系统原生的WifiConfigStore.xml,修改文件

packages/apps/Settings/src/com/android/settings/SettingsAdbDebugReceiver.java

public class SettingsAdbDebugReceiver extends BroadcastReceiver {

.......

    if(Intent.ACTION_BOOT_COMPLETED.equals(action)){
       copyFile("/system/etc/WifiConfigStore.xml","/data/misc/wifi/WifiConfigStore.xml");
    }

    public void copyFile(String oldPath, String newPath) { 
	    try { 
		    int bytesum = 0; 
		    int byteread = 0; 
		    File oldfile = new File(oldPath); 
		    if (oldfile.exists()) { //文件存在时 
			    InputStream inStream = new FileInputStream(oldPath); //读入原文件 
			    FileOutputStream fs = new FileOutputStream(newPath); 
			    byte[] buffer = new byte[1444]; 
			    int length; 
			    while ( (byteread = inStream.read(buffer)) != -1) { 
				    bytesum += byteread; //字节数 文件大小 
				    System.out.println(bytesum); 
				    fs.write(buffer, 0, byteread); 
			    } 
			    inStream.close(); 
		    } 
	    } catch (Exception e) { 
		    System.out.println("复制单个文件操作出错"); 
		    e.printStackTrace(); 
	    } 
    }

编译版本,烧录到手机里,发现没有成功,过滤avc日志发现没有权限对data/misc/wifi进行读写,也就是系统设置这个app没有权限去拷贝文件到/data/misc/wifi/下,怎么办呢? 去selinux中添加权限:

1.修改device/qcom/sepolicy/common/system_app.te文件,添加如下命令:

allow system_app wifi_data_file:file { read write create open getattr };

2.修改device/qcom/sepolicy/private/system_app.te文件,添加如下命令:

allow system_app wifi_data_file:file { read write create open getattr };

3.修改device/qcom/sepolicy/msm8953/system_app.te文件,添加如下命令:

allow system_app wifi_data_file:file { read write create open getattr };

 然后清空out目录,再次编译系统,重新烧录,开机之后去检查/data/misc/wifi/WifiConfigStore.xml文件,发现wifi信息已经内置到系统里了,这个需要重启才能生效.

猜你喜欢

转载自blog.csdn.net/lancelots/article/details/86575376