Android: Programmatically Turn on WiFi hotspot

2019-01-25 15:07发布

问题:

I am trying to turn on the portable Wifi hotspot ON, by referring this link:
how to set advanced settings of android wifihotspot

This is working well on Samsung Galaxy S3 Android v4.4.2.(no issues)
But on other devices with the same or lower Android version, the application crashes and restarts the device.

The code is as follows:

package com.android.startwifi;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class Main extends Activity {

public WifiManager wifiManager;
public Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i("hi1","hi");
    createWifiAccessPoint();
}
private void createWifiAccessPoint() {
    WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
    if(wifiManager.isWifiEnabled())
    {
        wifiManager.setWifiEnabled(false);          
    }       
    Method[] wmMethods = wifiManager.getClass().getDeclaredMethods();  
    boolean methodFound=false;
    for(Method method: wmMethods){
        if(method.getName().equals("setWifiApEnabled")){
            methodFound=true;
            WifiConfiguration netConfig = new WifiConfiguration();
            netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            try {
                boolean apstatus=(Boolean) method.invoke(wifiManager, netConfig,true);          
                //statusView.setText("Creating a Wi-Fi Network \""+netConfig.SSID+"\"");
                for (Method isWifiApEnabledmethod: wmMethods)
                {
                    if(isWifiApEnabledmethod.getName().equals("isWifiApEnabled")){
                        while(!(Boolean)isWifiApEnabledmethod.invoke(wifiManager)){
                        };
                        for(Method method1: wmMethods){
                            if(method1.getName().equals("getWifiApState")){
                                int apstate;
                                apstate=(Integer)method1.invoke(wifiManager);
                                //                    netConfig=(WifiConfiguration)method1.invoke(wifi);
                                //statusView.append("\nSSID:"+netConfig.SSID+"\nPassword:"+netConfig.preSharedKey+"\n");
                            }
                        }
                    }
                }
                if(apstatus)
                {
                    System.out.println("SUCCESSdddd");  
                    //statusView.append("\nAccess Point Created!");
                    //finish();
                    //Intent searchSensorsIntent = new Intent(this,SearchSensors.class);            
                    //startActivity(searchSensorsIntent);
                }else
                {
                    System.out.println("FAILED");   
                    //statusView.append("\nAccess Point Creation failed!");
                }
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }      
    }
    if(!methodFound){
        //statusView.setText("Your phone's API does not contain setWifiApEnabled method to configure an access point");
    }
}  

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}


Permissions that I have defined in the manifest file are:

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


The log file generated is as follows:

09-10 18:35:01.644: D/jdwp(29752): prepping for JDWP over ADB
09-10 18:35:01.644: D/jdwp(29752): ADB transport startup
09-10 18:35:01.644: D/dalvikvm(29752): Elevating priority from 0 to -8
09-10 18:35:01.645: D/jdwp(29752): JDWP: thread running
09-10 18:35:01.645: D/jdwp(29752): acceptConnection
09-10 18:35:01.646: D/jdwp(29752): trying to receive file descriptor from ADB
09-10 18:35:01.646: D/dalvikvm(29752): zygote get thread init done
09-10 18:35:01.653: D/jdwp(29752): received file descriptor 34 from ADB
09-10 18:35:01.658: D/jdwp(29752): processIncoming
09-10 18:35:01.659: D/jdwp(29752): processIncoming
09-10 18:35:01.659: D/jdwp(29752): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000040, flags=0x0, dataLen=0x8
09-10 18:35:01.661: D/jdwp(29752): processIncoming
09-10 18:35:01.661: D/jdwp(29752): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x17, id=0x40000041, flags=0x0, dataLen=0xC
09-10 18:35:01.663: D/jdwp(29752): processIncoming
09-10 18:35:01.663: D/jdwp(29752): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000042, flags=0x0, dataLen=0x8
09-10 18:35:01.665: D/jdwp(29752): processIncoming
09-10 18:35:01.665: D/jdwp(29752): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000043, flags=0x0, dataLen=0x8
09-10 18:35:01.676: D/jdwp(29752): sendBufferedRequest : len=0x3D
09-10 18:35:01.736: D/jdwp(29752): sendBufferedRequest : len=0x45
09-10 18:35:01.754: W/asset(29752): AssetManager-->addDefaultAssets CIP path not exsit!
09-10 18:35:02.219: I/hi1(29752): hi
09-10 18:35:02.261: D/WifiManager(29752): Enter init, sThreadRefCount:0
09-10 18:35:02.268: D/WifiManager(29752): Create WifiManager handlerthread
09-10 18:35:03.599: I/System.out(29752): SUCCESSdddd
09-10 18:35:03.634: V/PhoneWindow(29752): DecorView setVisiblity: visibility = 4
09-10 18:35:03.745: V/PhoneWindow(29752): DecorView setVisiblity: visibility = 0
09-10 18:35:03.922: D/libEGL(29752): loaded /system/lib/egl/libEGL_mali.so
09-10 18:35:03.924: D/libEGL(29752): loaded /system/lib/egl/libGLESv1_CM_mali.so
09-10 18:35:03.927: D/libEGL(29752): loaded /system/lib/egl/libGLESv2_mali.so
09-10 18:35:04.111: D/OpenGLRenderer(29752): Enabling debug mode 0
09-10 18:35:10.610: E/InputEventReceiver(29752): channel '41f21f48 com.android.startwifi/com.android.startwifi.Main (client)' ~ Publisher closed input channel or an error occurred.  events=0x9


Thus I have the following questions:

  • I would like to know why is this app behaving normally on Samsung S3 but not on other devices?
  • How can I fix this issue?

回答1:

I had the same issue. You must to erase the following code line:

netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);    

That works for me in versions 4.1.1, 4.2 and 4.3.



回答2:

Try this code .I test in android 5.0.1 :

      public boolean setHotSpot(String SSID,String passWord){
        Method[] mMethods = mWifiManager.getClass().getDeclaredMethods();

        for(Method mMethod: mMethods){

            if(mMethod.getName().equals("setWifiApEnabled")) {
                WifiConfiguration netConfig = new WifiConfiguration();
                if(passWord==""){
                    netConfig.SSID = SSID;
                    netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                    netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                    netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);     
                }else{
                netConfig.SSID = SSID ;
                netConfig.preSharedKey = passWord;
                netConfig.hiddenSSID = true;
                netConfig.status = WifiConfiguration.Status.ENABLED;
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                }
                try {              
                    mMethod.invoke(mWifiManager, netConfig,false);
                    mWifiManager.saveConfiguration();
                    return true;

                } catch (Exception e) {
                  e.getMessage();
                }
            }
        }
        return false; 
  }

Insert this permission in AndroidManifest:

<uses-permission android:name="android.permission.ACCESS_WIFI_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.CHANGE_NETWORK_STATE" />


回答3:

If you are using WPA_PSK authentication, you have to specify preSharedKey.

        netConfig.preSharedKey = "testPassword";
        netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

Otherwise it will crash and restart the device.