Android code settings, turn on WiFi hotspot and hotspot connection (GO)

Source: Internet
Author: User

 

A friend with a quick tooth should know that they are using a WiFi hotspot when transferring files between devices, and then connecting the hotspot to another one. The speed of fast tooth transmission should be related to its mechanism. I don't know what its search mechanism is, but I think it should be judged by the name of the hotspot. Let's explore how to create a WiFi hotspot automatically.

Create WiFi hotspot first need phone support, recommend the development of the elder brother the whole better phone, our company those cottage equipment, nearly half is not support hot spot; in fact, creating a hotspot is simple, first get to the WiFi service, then configure the hotspot name, password and so on, and then open it by reflection OK.

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

[HTML]View Plaincopy
  1. Package com.tel.lajoin.wifi.hotspot;
  2. Import Java.lang.reflect.Method;
  3. Import android.app.Activity;
  4. Import Android.content.Context;
  5. Import android.net.wifi.WifiConfiguration;
  6. Import Android.net.wifi.WifiManager;
  7. Import Android.os.Bundle;
  8. Import Android.view.View;
  9. Import Android.widget.Button;
  10. public class Hotspotactivity extends Activity {
  11. Private Wifimanager Wifimanager;
  12. Private Button Open;
  13. Private Boolean flag=false;
  14. @Override
  15. protected void OnCreate (Bundle savedinstancestate) {
  16. TODO auto-generated Method Stub
  17. Super.oncreate (savedinstancestate);
  18. Setcontentview (R.layout.main);
  19. Get WiFi Management Service
  20. Wifimanager = (wifimanager) getsystemservice (Context.wifi_service);
  21. open= (Button) Findviewbyid (r.id.open_hotspot);
  22. Set hot spots with button events
  23. Open.setonclicklistener (New View.onclicklistener () {
  24. @Override
  25. public void OnClick (View v) {
  26. If it is turned off, turn it off if it is turned off
  27. Flag=!flag;
  28. setwifiapenabled (flag);
  29. }
  30. });
  31. }
  32. WiFi hotspot Switch
  33. Public Boolean setwifiapenabled (Boolean enabled) {
  34. if (enabled) {//disable WiFi in any case
  35. WiFi and hotspot cannot be opened at the same time, so you need to turn off WiFi when opening hotspots
  36. Wifimanager.setwifienabled (FALSE);
  37. }
  38. try {
  39. Configuration classes for hotspots
  40. Wificonfiguration apconfig = new Wificonfiguration ();
  41. Configure the name of the hotspot (you can add a random number after the name)
  42. Apconfig.ssid = "Yrcconnection";
  43. Configure a password for a hotspot
  44. apconfig.presharedkey="12122112";
  45. Set a hotspot by reflection call
  46. Method = wifimanager.getclass (). GetMethod (
  47. "Setwifiapenabled", Wificonfiguration.class, Boolean.type);
  48. Return to hotspot open status
  49. Return (Boolean) Method.invoke (Wifimanager, Apconfig, enabled);
  50. } catch (Exception e) {
  51. return false;
  52. }
  53. }
  54. }
Layout will not write it, just a button, everyone knows something, write also no meaning. To implement file transfer, of course, we also need to write a hotspot connection to the client bar. The process of connecting hotspots first is to search for hotspots and then determine if the hotspot meets the rules before connecting.

[HTML]View Plaincopy
  1. Package Com.tel.lajoin.wifiscan;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Import android.app.Activity;
  5. Import Android.content.BroadcastReceiver;
  6. Import Android.content.Context;
  7. Import android.content.Intent;
  8. Import Android.content.IntentFilter;
  9. Import Android.net.wifi.ScanResult;
  10. Import android.net.wifi.WifiConfiguration;
  11. Import Android.net.wifi.WifiManager;
  12. Import Android.os.Bundle;
  13. public class Mainactivity extends Activity {
  14. Private List<scanresult> wifilist;
  15. Private Wifimanager Wifimanager;
  16. Private List<String> passablehotspot;
  17. Private Wifireceiver Wifireceiver;
  18. Private Boolean isconnected=false;
  19. private Button Connect;
  20. @Override
  21. public void OnCreate (Bundle savedinstancestate) {
  22. Super.oncreate (savedinstancestate);
  23. Init ();
  24. }
  25. /* Initialize parameters */
  26. public void init () {
  27. Setcontentview (R.layout.main);
  28. connect= (Button) Findviewbyid (r.id.connect);
  29. Wifimanager = (wifimanager) getsystemservice (Context.wifi_service);
  30. wifireceiver = new Wifireceiver ();
  31. Search hotspots with Button events
  32. Connect.setonclicklistener (New View.onclicklistener () {
  33. @Override
  34. public void OnClick (View v) {
  35. Wifimanager.startscan ();
  36. }
  37. });
  38. }
  39. /* Monitor hotspot changes */
  40. Private Final class Wifireceiver extends Broadcastreceiver {
  41. @Override
  42. public void OnReceive (context context, Intent Intent) {
  43. wifilist = wifimanager.getscanresults ();
  44. if (wifilist = = NULL | | wifilist.size () = = 0 | | isconnected)
  45. Return
  46. Onreceivenewnetworks (wifilist);
  47. }
  48. }
  49. /* Determine if the hotspot meets the specifications when searching for a new WiFi hotspot */
  50. public void Onreceivenewnetworks (List<scanresult> Wifilist) {
  51. passablehotspot=New ArrayList<String> ();
  52. for (Scanresult result:wifilist) {
  53. System.out.println (result. SSID);
  54. if (result. SSID). Contains ("Yrcconnection"))
  55. Passablehotspot.add (result. SSID);
  56. }
  57. Synchronized (this) {
  58. Connecttohotpot ();
  59. }
  60. }
  61. /* Connect to Hotspot */
  62. public void Connecttohotpot () {
  63. if (passablehotspot==null | | passablehotspot.size () ==0)
  64. Return
  65. Wificonfiguration wificonfig=this.setwifiparams (passablehotspot.get (0));
  66. int wcgid = wifimanager.addnetwork (wificonfig);
  67. Boolean flag=wifimanager.enablenetwork (Wcgid, true);
  68. isconnected=Flag;
  69. SYSTEM.OUT.PRINTLN ("Connect success?") +flag);
  70. }
  71. /* Set parameters for the hotspot to be connected */
  72. Public wificonfiguration setwifiparams (String SSID) {
  73. Wificonfiguration apconfig=new Wificonfiguration ();
  74. apconfig.ssid= "\""+ssid+" \ "";
  75. apconfig.presharedkey= "\"12122112\ "";
  76. Apconfig.hiddenssid = true;
  77. apconfig.status = WifiConfiguration.Status.ENABLED;
  78. ApConfig.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.TKIP);
  79. ApConfig.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.CCMP);
  80. ApConfig.allowedKeyManagement.set (WifiConfiguration.KeyMgmt.WPA_PSK);
  81. ApConfig.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.TKIP);
  82. ApConfig.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.CCMP);
  83. ApConfig.allowedProtocols.set (WifiConfiguration.Protocol.RSN);
  84. return apconfig;
  85. }
  86. @Override
  87. protected void OnDestroy () {
  88. Super.ondestroy ();
  89. /* Log off the broadcast when destroyed */
  90. Unregisterreceiver (Wifireceiver);
  91. }
  92. }
The code is simple, and it's all annotated, and I'm sure you can see it. That's it, as for file transfer advice or go to a socket-related article.

Android code settings, turn on WiFi hotspot and hotspot connection (GO)

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.