SoFunction
Updated on 2025-03-11

Android programming method to implement settings and open wifi hotspot sharing for others to connect

This article describes the method of implementing android programming to set up and opening wifi hotspot sharing for others to connect. Share it for your reference, as follows:

Friends who use too fast teeth should know that when they transfer files between devices for two days, they use a wifi hotspot, and then the other one connects to this hotspot and then transmits it. The amazing transmission speed of fast teeth should be related to its mechanism. I don’t know what its search mechanism is, but I think it can be judged by the name of the hot topic. Let's discuss how to automatically create a wifi hotspot

To create a wifi hotspot, you need mobile phone support. It is recommended that the developer develops a good phone. Almost half of the counterfeit devices in our company do not support hotspots. In fact, it is very simple to create a hotspot. First get the wifi service, then configure the hotspot name, password, etc., and then open it through reflection.

Let's take a look at the code implementation for creating hotspots:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class HotspotActivity extends Activity {
  private WifiManager wifiManager;
  private Button open;
  private boolean flag=false;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    (savedInstanceState);
    setContentView();
    //Get wifi management service    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    open=(Button)findViewById(.open_hotspot);
    //Set hot spots through button events    (new () {
      @Override
      public void onClick(View v) {
        //If it is open, close, if it is closed, open        flag=!flag;
        setWifiApEnabled(flag);
      }
    });
  }
  // Wifi hotspot switch  public boolean setWifiApEnabled(boolean enabled) {
    if (enabled) { // disable WiFi in any case
      //Wifi and hotspot cannot be opened at the same time, so you need to turn off wifi when opening hotspot      (false);
    }
    try {
      //Hotspot configuration class      WifiConfiguration apConfig = new WifiConfiguration();
      //Configure the name of the hotspot (you can add a random number after the name)       = "YRCCONNECTION";
      //Configure the password for hotspot      ="12122112";
        //Set hotspots through reflection call      Method method = ().getMethod(
          "setWifiApEnabled", , );
      //Return to the hotspot open status      return (Boolean) (wifiManager, apConfig, enabled);
    } catch (Exception e) {
      return false;
    }
  }
}

I won’t write the layout, just one button, something that everyone knows, and it’s meaningless to write it. To achieve file transfer, of course we also need to write a client connecting to the hotspot. The process of connecting to hot spots is to search for hot spots, then determine whether the hot spots comply with the rules and then connect.

package ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
public class MainActivity extends Activity { 
 private List<ScanResult> wifiList; 
 private WifiManager wifiManager; 
 private List<String> passableHotsPot; 
 private WifiReceiver wifiReceiver; 
 private boolean isConnected=false; 
 private Button connect; 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  (savedInstanceState); 
  init(); 
 } 
 /* Initialization parameters */ 
 public void init() { 
  setContentView(); 
  connect=(Button)findViewById(); 
  wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
  wifiReceiver = new WifiReceiver(); 
  //Search hot spots through button events  (new () { 
   @Override 
   public void onClick(View v) { 
    (); 
   } 
  });  
 } 
 /* Monitor hot spot changes */ 
 private final class WifiReceiver extends BroadcastReceiver { 
  @Override 
  public void onReceive(Context context, Intent intent) { 
   wifiList = (); 
   if (wifiList == null || () == 0 || isConnected) 
    return; 
   onReceiveNewNetworks(wifiList); 
  } 
 } 
 /* When searching for a new wifi hotspot, determine whether the hotspot meets the specifications*/ 
 public void onReceiveNewNetworks(List<ScanResult> wifiList){ 
  passableHotsPot=new ArrayList<String>(); 
  for(ScanResult result:wifiList){ 
   (); 
   if(().contains("YRCCONNECTION")) 
    (); 
  } 
  synchronized (this) { 
   connectToHotpot(); 
  } 
 } 
 /*Connect to hotspot*/ 
 public void connectToHotpot(){ 
  if(passableHotsPot==null || ()==0) 
   return; 
  WifiConfiguration wifiConfig=((0)); 
  int wcgID = (wifiConfig); 
  boolean flag=(wcgID, true); 
  isConnected=flag; 
  ("connect success? "+flag); 
 } 
 /*Set the parameters of the hotspot to be connected*/ 
 public WifiConfiguration setWifiParams(String ssid){ 
  WifiConfiguration apConfig=new WifiConfiguration(); 
  ="\""+ssid+"\""; 
  ="\"12122112\""; 
   = true; 
   = ; 
  (); 
  (); 
  (.WPA_PSK); 
  (); 
  (); 
  (); 
  return apConfig; 
 } 
 @Override 
 protected void onDestroy() { 
  (); 
  /*Register broadcast when destroyed*/ 
  unregisterReceiver(wifiReceiver); 
 } 
}

The code is very simple and has comments, so I believe everyone can understand it. Then that's it. As for file transfer, it's better to check out the socket-related articles.

I hope this article will be helpful to everyone's Android programming design.