Turn from: http://blog.csdn.net/luoboo525/article/details/7883998 Pro test available
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
- 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 Management Service
- Wifimanager = (wifimanager) getsystemservice (Context.wifi_service);
- open= (Button) Findviewbyid (r.id.open_hotspot);
- Set hot spots with button events
- Open.setonclicklistener (New View.onclicklistener () {
- @Override
- public void OnClick (View v) {
- If it is turned off, turn it off if it is turned off
- 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 hotspots
- Wifimanager.setwifienabled (FALSE);
- }
- try {
- Configuration classes for hotspots
- Wificonfiguration apconfig = new Wificonfiguration ();
- Configure the name of the hotspot (you can add a random number after the name)
- Apconfig.ssid = "Yrcconnection";
- Configure a password for a hotspot
- apconfig.presharedkey="12122112";
- Set a hotspot by reflection call
- Method = wifimanager.getclass (). GetMethod (
- "Setwifiapenabled", Wificonfiguration.class, Boolean.type);
- Return to 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 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
- 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 ();
- }
- /* Initialize parameters */
- public void init () {
- Setcontentview (R.layout.main);
- connect= (Button) Findviewbyid (r.id.connect);
- Wifimanager = (wifimanager) getsystemservice (Context.wifi_service);
- wifireceiver = new Wifireceiver ();
- Search hotspots with Button events
- Connect.setonclicklistener (New View.onclicklistener () {
- @Override
- public void OnClick (View v) {
- Wifimanager.startscan ();
- }
- });
- }
- /* Monitor hotspot changes */
- Private Final class Wifireceiver extends Broadcastreceiver {
- @Override
- public void OnReceive (context context, Intent Intent) {
- wifilist = wifimanager.getscanresults ();
- if (wifilist = = NULL | | wifilist.size () = = 0 | | isconnected)
- Return
- Onreceivenewnetworks (wifilist);
- }
- }
- /* Determine if the hotspot meets the specifications when searching for a new WiFi hotspot */
- public void Onreceivenewnetworks (List<scanresult> Wifilist) {
- passablehotspot=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 parameters for the hotspot to be connected */
- Public wificonfiguration setwifiparams (String SSID) {
- Wificonfiguration apconfig=new Wificonfiguration ();
- 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 ();
- /* Log off the broadcast when destroyed */
- Unregisterreceiver (Wifireceiver);
- }
- }
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 mobile phone 3g traffic sharing to let others connect to this WiFi