Android Включение Wi-Fi точки доступа программно
Я хочу включить точку доступа Wi-Fi программно. Я использую следующий код:
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void enableHotspot(View v){
WifiAccessManager.setWifiApState(this, true);
}
}
public class WifiAccessManager {
private static final String SSID = "1234567890abcdef";
public static boolean setWifiApState(Context context, boolean enabled) {
//config = Preconditions.checkNotNull(config);
try {
WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (enabled) {
mWifiManager.setWifiEnabled(false);
}
WifiConfiguration conf = getWifiApConfiguration();
mWifiManager.addNetwork(conf);
return (Boolean) mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class).invoke(mWifiManager, conf, enabled);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static WifiConfiguration getWifiApConfiguration() {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = SSID;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
return conf;
}
}
Разрешения в файле AndroidmManifest:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Я тестирую его на 2 разных устройствах (Android 4 и Android 6). Это нормально работает на Android 4 (никаких проблем), но на Android 6 не работает, и генерируются следующие журналы:
java.lang.reflect.InvocationTargetException
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err: at java.lang.reflect.Method.invoke(Native Method)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err: at ir.captainweb.wifitests.WifiAccessManager.setWifiApState(WifiAccessManager.java:20)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err: at ir.captainweb.wifitests.MainActivity.enableHotspot(MainActivity.java:17)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err: at java.lang.reflect.Method.invoke(Native Method)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err: at android.view.View.performClick(View.java:5210)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err: at android.view.View$PerformClick.run(View.java:21328)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err: at android.os.Looper.loop(Looper.java:148)
09-02 23:04:25.052 5220-5220/ir.captainweb.wifitests W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5551)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err: at java.lang.reflect.Method.invoke(Native Method)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err: Caused by: java.lang.SecurityException: ir.captainweb.wifitests was not granted this permission: android.permission.WRITE_SETTINGS.
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err: at android.os.Parcel.readException(Parcel.java:1599)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err: at android.os.Parcel.readException(Parcel.java:1552)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err: at android.net.wifi.IWifiManager$Stub$Proxy.setWifiApEnabled(IWifiManager.java:1540)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err: at android.net.wifi.WifiManager.setWifiApEnabled(WifiManager.java:1588)
09-02 23:04:25.053 5220-5220/ir.captainweb.wifitests W/System.err: ... 14 more
Я также добавил разрешение WRITE_SETTINGS, но снова у меня та же проблема
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
1 ответ
Мы должны дать разрешение вручную в Android 6.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// please check here whether you already have these permission or not.
requestPermissions(new String[]{Manifest.permission.WRITE_SETTINGS}, 2909);
}
Вы получите ответ разрешения независимо от того, предоставлено оно или нет.
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 2909: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("Permission", "Granted");
} else {
Log.e("Permission", "Denied");
}
return;
}
}
}