由于项目需要,需要为Android-9.0和Android-10.0系统编写Wifi-AP的完整操控功能。翻看了许多同仁的帖子和资料,大部分都是互相抄,并且存在一些错误。为了方便后来的各位,我把自己编写的代码完整的贴出来,我保证,这份代码在我的两款Android机顶盒上面都有可靠运行(Android-9.0和Android-10.0)。
开启Wifi-AP的代码如下:
private void EnableAp(Context context)
{
Log.d(TAG, "Enable Wi-Fi AP!");
try
{
WifiManager wifiManager = (WifiManager)
context.getSystemService(Context.WIFI_SERVICE);
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "wifi.ap.test";
wifiConfiguration.allowedKeyManagement.set(4);
wifiConfiguration.allowedAuthAlgorithms.set(0);
wifiConfiguration.preSharedKey = "password.test";
if (wifiManager != null)
{
Class extends WifiManager> wifiManagerClass = wifiManager.getClass();
Method setWifiApConfigurationMethod =
wifiManagerClass.getMethod("setWifiApConfiguration", WifiConfiguration.class);
setWifiApConfigurationMethod.invoke(wifiManager, wifiConfiguration);
}
if (connectivityManager != null)
{
ResultReceiver dummyResultReceiver = new ResultReceiver(null);
String fieldName = "mService";
Field internalConnectivityManagerField =
ConnectivityManager.class.getDeclaredField(fieldName);
internalConnectivityManagerField.setAccessible(true);
Object internalConnectivityManagerObject =
internalConnectivityManagerField.get(connectivityManager);
if (internalConnectivityManagerObject != null)
{
String className = internalConnectivityManagerObject.getClass().getName();
Class internalConnectivityManagerClass = Class.forName(className);
String methodName = "startTethering";
Method startTetheringMethod = internalConnectivityManagerClass.getDeclaredMethod(
methodName, int.class, ResultReceiver.class, boolean.class, String.class);
startTetheringMethod.invoke(internalConnectivityManagerObject,
0, dummyResultReceiver, false, context.getPackageName());
}
}
}
catch (Exception e)
{
Log.d(TAG, "EnableAp: Exception");
e.printStackTrace();
}
}
关闭Wifi-AP的代码如下:
private void DisableAp(Context context)
{
Log.d(TAG, "Disable Wi-Fi AP!");
try
{
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null)
{
String fieldName = "mService";
Field internalConnectivityManagerField =
ConnectivityManager.class.getDeclaredField(fieldName);
internalConnectivityManagerField.setAccessible(true);
Object internalConnectivityManagerObject =
internalConnectivityManagerField.get(connectivityManager);
if (internalConnectivityManagerObject != null)
{
String className = internalConnectivityManagerObject.getClass().getName();
Class internalConnectivityManagerClass = Class.forName(className);
String methodName = "stopTethering";
Method stopTethering = internalConnectivityManagerClass.getDeclaredMethod(
methodName, int.class, String.class);
stopTethering.invoke(internalConnectivityManagerObject,
0, context.getPackageName());
}
}
}
catch (Exception e)
{
Log.d(TAG, "DisableAp: Exception");
e.printStackTrace();
}
}
最后,感谢这篇帖子,虽然代码有错误,但是有参考价值:
https://juejin.im/post/5bec1a5d6fb9a049be5d0aa8