更改Android热点设置
作者:互联网
随着API级别26的发布,我的应用程序的核心功能破了,这就是改变了应用程序中用户的热点设置.
要获取和设置此配置,我使用WifiManager隐藏的api:getWifiApConfiguration和setWifiApConfiguration中的以下函数.
Method getWifiApConfiguration = wifiManager.getClass().getMethod("getWifiApConfiguration");
getWifiApConfiguration.invoke(wifiManager);
这适用于Android O之前的设备,但在此版本中我收到以下错误:
App not allowed to read or update stored WiFi Ap config (uid = 10168)
我在清单中声明的权限是:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.NETWORK_STACK"/>
<uses-permission android:name="android.permission.TETHER_PRIVILEGED" />
如何使用最新的API执行此操作?
解决方法:
从Android Oreo(26)开始,新的权限检查被添加到getWifiApConfiguration()方法的服务实现中:
来自WifiServiceImpl.java的片段
/**
* see {@link WifiManager#getWifiApConfiguration()}
* @return soft access point configuration
* @throws SecurityException if the caller does not have permission to retrieve the softap
* config
*/
@Override
public WifiConfiguration getWifiApConfiguration() {
enforceAccessPermission();
int uid = Binder.getCallingUid();
// only allow Settings UI to get the saved SoftApConfig
if (!mWifiPermissionsUtil.checkConfigOverridePermission(uid)) {
// random apps should not be allowed to read the user specified config
throw new SecurityException("App not allowed to read or update stored WiFi Ap config "
+ "(uid = " + uid + ")");
}
mLog.trace("getWifiApConfiguration uid=%").c(uid).flush();
return mWifiStateMachine.syncGetWifiApConfiguration();
}
深入研究代码,您将很快发现要成功调用此方法,您的应用程序必须持有android.permission.OVERRIDE_WIFI_CONFIG权限,该权限是系统级别受保护的权限:
框架AndroidManifest.xml的片段
<!-- @SystemApi @hide Allows an application to modify any wifi configuration, even if created
by another application. Once reconfigured the original creator cannot make any further
modifications.
<p>Not for use by third-party applications. -->
<permission android:name="android.permission.OVERRIDE_WIFI_CONFIG"
android:protectionLevel="signature|privileged" />
这意味着您的应用程序需要由平台密钥签名或有权使用此API.
标签:android,android-wifi,android-networking,tethering 来源: https://codeday.me/bug/20190717/1486698.html