TheWaterProgrammer
TheWaterProgrammer

Reputation: 8259

Can a third party app create a hotspot on Android?

Is it possible for a third party app to open a WiFi Hotspot on Android? Following is basically the crux of how I am doing this.

public void setWifiApEnabled(int channel) {
    WifiConfiguration wifiConfig = new WifiConfiguration();
    wifiConfig.SSID = "GunaMelodyHotspot";
    wifiConfig.preSharedKey = "Melody123";
    wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

    // Using reflection to set the hotspot configuration
    try {
        Method setWifiApConfigurationMethod = wifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class);
        setWifiApConfigurationMethod.invoke(wifiManager, wifiConfig);

        Method getWifiApStateMethod = wifiManager.getClass().getMethod("getWifiApState");
        int state = (int) getWifiApStateMethod.invoke(wifiManager);

        if (state == 12 || state == 13) { // AP state is enabled or enabling
            Method getWifiApConfigurationMethod = wifiManager.getClass().getMethod("getWifiApConfiguration");
            WifiConfiguration currentConfig = (WifiConfiguration) getWifiApConfigurationMethod.invoke(wifiManager);

            if (currentConfig != null) {
                Field apBandField = currentConfig.getClass().getDeclaredField("apBand");
                Field apChannelField = currentConfig.getClass().getDeclaredField("apChannel");

                apBandField.setAccessible(true);
                apChannelField.setAccessible(true);

                apBandField.setInt(currentConfig, 0); // 0 for 2.4GHz, 1 for 5GHz
                apChannelField.setInt(currentConfig, channel);

                setWifiApConfigurationMethod.invoke(wifiManager, currentConfig);
            }
        } else {
            Log.e("HotspotHelper", "Failed to set hotspot configuration");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I have the following permissions decalred in my AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

But I get the following exception.

W System.err: Caused by: java.lang.SecurityException: App not allowed to read or update stored WiFi AP config (uid = 10370)
W System.err:   at android.os.Parcel.createExceptionOrNull(Parcel.java:3069)
W System.err:   at android.os.Parcel.createException(Parcel.java:3053)
W System.err:   at android.os.Parcel.readException(Parcel.java:3036)
W System.err:   at android.os.Parcel.readException(Parcel.java:2978)
W System.err:   at android.net.wifi.IWifiManager$Stub$Proxy.setWifiApConfiguration(IWifiManager.java:4742)
W System.err:   at android.net.wifi.WifiManager.setWifiApConfiguration(WifiManager.java:6078)
W System.err:   ... 19 more
W System.err: Caused by: android.os.RemoteException: Remote stack trace:
W System.err:   at com.android.server.wifi.WifiServiceImpl.setWifiApConfiguration(WifiServiceImpl.java:2872)
W System.err:   at android.net.wifi.IWifiManager$Stub.onTransact(IWifiManager.java:2281)
W System.err:   at android.os.Binder.execTransactInternal(Binder.java:1375)
W System.err:   at android.os.Binder.execTransact(Binder.java:1311)

Is it somehow allowed for a normal third-party to create a hotspot on Android?

Upvotes: 0

Views: 87

Answers (1)

Manjunath Karamudi
Manjunath Karamudi

Reputation: 100

I think there are restrictions when you programmatically want to switch the wifi on cause of Security Issues,

The most you can do use Start an Intent Action to open wifi settings which would directly lead user to turn it on by Manually.

Upvotes: 1

Related Questions