Android programming Implementation settings, open WiFi Hotspot sharing method for others to connect _android

Source: Internet
Author: User
Tags reflection

This article describes the Android programming implementation setting, the way to open a WiFi hotspot to share for others to connect to. Share to everyone for your reference, specific as follows:

Friends who use fast teeth should know that they are using a WiFi hotspot when transferring files between two days of equipment, and then another connects to the hotspot for transmission. The speed of the fast tooth transmission should be related to its mechanism. I don't know what the search mechanism is, but I think we can judge it by the name of the hot spot. Now let's explore how to automatically create a WiFi hotspot.

Creating a WiFi hotspot requires phone support first, recommended the development of the whole good friend of the mobile phone, our company those cottage equipment, almost half of it does not support hot spots; in fact, creating hot spots is very simple, first get the WiFi service, then configure the hotspot name, password, and so on, and then turn it on by reflection OK.

Let's look at the code implementation that creates hotspots:

Package com.tel.lajoin.wifi.hotspot;
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.view.View;
Import Android.widget.Button;
  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 super.oncreate
    (savedinstancestate);
    Setcontentview (R.layout.main);
    Get WIFI Admin Service Wifimanager = (Wifimanager) getsystemservice (Context.wifi_service);
    Open= (Button) Findviewbyid (r.id.open_hotspot);
        Set hotspot Open.setonclicklistener by button event (new View.onclicklistener () {@Override public void OnClick (View v) {
        If it is turned off, turn off the Flag=!flag if it is turned off;
      setwifiapenabled (flag);
  }
    }); }//WiFi hotspot switch public Boolean setwifiapenabled {if (enabled) {//disable WiFi in no case//wifi and hotspots cannot be opened at the same time, so when you open a hotspot
    The need to turn off WiFi wifimanager.setwifienabled (false);
      } try {//Hotspot configuration class Wificonfiguration apconfig = new Wificonfiguration ();
      Configure the name of the hotspot (you can add a random number after the name) Apconfig.ssid = "Yrcconnection";
        Configure the hotspot password apconfig.presharedkey= "12122112"; Set Hotspot method by reflection Call method = Wifimanager.getclass (). GetMethod ("setwifiapenabled", Wificonfiguration.class
      , Boolean.type);
    Returns the Hotspot Open status return (Boolean) Method.invoke (Wifimanager, Apconfig, enabled);
    catch (Exception e) {return false;

 }
  }
}

Layout will not write it, just a button, everyone knows things, wrote also didn't mean anything. To achieve file transfer, of course, we also need to write a link to the hot client. The process of connecting hotspots is first to search for hotspots and then to determine if the hot spots are compliant and then connect.

Package Com.tel.lajoin.wifiscan; 
Import java.util.ArrayList; 
Import java.util.List; 
Import android.app.Activity; 
Import Android.content.BroadcastReceiver; 
Import Android.content.Context; 
Import android.content.Intent; 
Import Android.content.IntentFilter; 
Import Android.net.wifi.ScanResult; 
Import android.net.wifi.WifiConfiguration; 
Import Android.net.wifi.WifiManager; 
Import Android.os.Bundle; 
 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) {super.oncreate (savedinstancestate); 
 Init (); 
  /* Initialization parameter/* public void init () {Setcontentview (r.layout.main); 
  connect= (Button) Findviewbyid (r.id.connect); 
  Wifimanager = (Wifimanager) getsystemservice (Context.wifi_service); WifireceiveR = new Wifireceiver (); 
    Search hotspot Connect.setonclicklistener by button event (new View.onclicklistener () {@Override public void OnClick (View v) { 
   Wifimanager.startscan ();  
 } 
  }); /* Listen for hotspot changes/* Private Final class Wifireceiver extends Broadcastreceiver {@Override public void onreceive (Cont 
   Ext context, Intent Intent) {wifilist = Wifimanager.getscanresults (); 
   if (wifilist = null | | wifilist.size () = = 0 | | isconnected) return; 
  Onreceivenewnetworks (wifilist); }/* To determine if the hotspot meets the specification when searching for a new WiFi hotspot * * * public void Onreceivenewnetworks (list<scanresult> wifilist) {PASSABLEHOTSP 
  Ot=new arraylist<string> (); for (Scanresult result:wifilist) {System.out.println, result. 
   SSID); if (result. SSID). Contains ("Yrcconnection")) Passablehotspot.add (result. 
  SSID); 
  } synchronized (this) {connecttohotpot (); }/* Connect to Hotspot */public void Connecttohotpot () {if (Passablehotspot==null | | | passablehotspot.size () ==0) return;
  Wificonfiguration wificonfig=this.setwifiparams (passablehotspot.get (0)); 
  int wcgid = Wifimanager.addnetwork (wificonfig); 
  Boolean flag=wifimanager.enablenetwork (Wcgid, true); 
  Isconnected=flag; SYSTEM.OUT.PRINTLN ("Connect success?") 
 +flag); /* Set the parameters of the hotspot to be connected */public wificonfiguration setwifiparams (String SSID) {wificonfiguration apconfig=new Wificonfigura 
  tion (); 
  Apconfig.ssid= "\" "+ssid+" ""; 
  Apconfig.presharedkey= "\" 12122112\ ""; 
  Apconfig.hiddenssid = true; 
  Apconfig.status = WifiConfiguration.Status.ENABLED; 
  ApConfig.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.TKIP); 
  ApConfig.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.CCMP); 
  ApConfig.allowedKeyManagement.set (WifiConfiguration.KeyMgmt.WPA_PSK); 
  ApConfig.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.TKIP); 
  ApConfig.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.CCMP); 
  ApConfig.allowedProtocols.set (WifiConfiguration.Protocol.RSN); Return APConfig; 
  } @Override protected void OnDestroy () {Super.ondestroy (); 
 /* Cancellation of Broadcast/Unregisterreceiver (Wifireceiver) at the time of destruction;

 } 
}

The code is very simple, and there are comments, I believe that everyone can see. That's it, as for file transfer suggestions or to see the socket related articles.

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.