Reputation: 6760
I've been trying to follow this tutorial on how to programmatically configure and start a Wi-Fi hotspot on Android. Here's my main activity file:
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration configuration = new WifiConfiguration();
configuration.SSID = "MyNetwork";
configuration.preSharedKey = "password";
configuration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
configuration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
configuration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiManager.setWifiEnabled(false);
try {
Method method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
if ( (boolean)method.invoke(wifiManager, configuration, true) ) {
Log.i("MyApp", "Hotspot started");
}
else {
Log.e("MyApp", "Failed to start hotspot");
}
}
catch (Exception e) {
Log.e("MyApp", "Well, bugger");
e.printStackTrace();
}
}
}
I gave my app the ACCESS_WIFI_STATE
and CHANGE_WIFI_STATE
permissions. However, when I build and run it on an emulated Pixel 3 (API 28), the line Method method= ...
causes the app to crash with
java.lang.NoSuchMethodException: setWifiApEnabled [class android.net.wifi.WifiConfiguration, boolean]
at java.lang.Class.getMethod(Class.java:2068)
at java.lang.Class.getMethod(Class.java:1690)
at com.example.testjava.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
WifiManager.setWifiApEnabled
appears to exist and take the specified arguments. What am I missing here?
Upvotes: 1
Views: 315
Reputation: 1
New API, new method: startSoftAp(WiFiConfiguration) stopSoftAp()
Upvotes: 0