android开启Wifi AP

  1 package com.myapp.android.activity.wifi;
  2 
  3 import android.app.ProgressDialog;
  4 import android.content.Context;
  5 import android.net.wifi.WifiConfiguration;
  6 import android.net.wifi.WifiManager;
  7 import android.os.AsyncTask;
  8 import android.os.Bundle;
  9 import android.view.View;
 10 import android.view.WindowManager;
 11 import android.widget.Button;
 12 
 13 import java.lang.reflect.Method;
 14 
 15 public class WifiAP extends BaseActivity {
 16 
 17     // boolean mIsWifiEnabled = false;
 18     private static final int WIFI_AP_STATE_UNKNOWN = -1;
 19     private static final int WIFI_AP_STATE_DISABLING = 0;
 20     private static final int WIFI_AP_STATE_DISABLED = 1;
 21     private static final int WIFI_AP_STATE_ENABLING = 2;
 22     private static final int WIFI_AP_STATE_ENABLED = 3;
 23     private static final int WIFI_AP_STATE_FAILED = 4;
 24 
 25     private final String[] WIFI_STATE_TEXTSTATE = new String[] {
 26         "DISABLING","DISABLED","ENABLING","ENABLED","FAILED"
 27     };
 28 
 29     private WifiManager wifi;
 30 
 31     @Override
 32     protected void onCreate(Bundle icicle) {
 33         super.onCreate(icicle);
 34         setContentView(R.layout.wifi);
 35         getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
 36                 |WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
 37                 |WindowManager.LayoutParams.FLAG_DIM_BEHIND
 38         );
 39 
 40         wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 41     }
 42 
 43     @Override
 44     public void onResume() {
 45         super.onResume();
 46         updateStatusDisplay();
 47     }
 48 
 49     public void toggleWifi(View v) {
 50         boolean wifiApIsOn = getWifiAPState()==WIFI_AP_STATE_ENABLED || getWifiAPState()==WIFI_AP_STATE_ENABLING;
 51         new SetWifiAPTask(!wifiApIsOn,false).execute();
 52     }
 53 
 54     public void close(View v) {
 55         boolean wifiApIsOn = getWifiAPState()==WIFI_AP_STATE_ENABLED || getWifiAPState()==WIFI_AP_STATE_ENABLING;
 56         if (wifiApIsOn) {
 57             new SetWifiAPTask(false,true).execute();
 58         } else {
 59             finish();
 60         }
 61     }
 62 
 63 
 64     /**
 65      * Endable/disable wifi
 66      * @param enabled
 67      * @return WifiAP state
 68      */
 69     private int setWifiApEnabled(boolean enabled) {
 70         Log.d("WifiAP", "*** setWifiApEnabled CALLED **** " + enabled);
 71         if (enabled && wifi.getConnectionInfo() !=null) {
 72             wifi.setWifiEnabled(false);
 73             try {Thread.sleep(1500);} catch (Exception e) {}
 74         }
 75 
 76         //int duration = Toast.LENGTH_LONG;
 77         //String toastText = "MobileAP status: ";
 78         int state = WIFI_AP_STATE_UNKNOWN;
 79         try {
 80             wifi.setWifiEnabled(false);
 81             Method method1 = wifi.getClass().getMethod("setWifiApEnabled",
 82                 WifiConfiguration.class, boolean.class);
 83             method1.invoke(wifi, null, enabled); // true
 84             Method method2 = wifi.getClass().getMethod("getWifiApState");
 85             state = (Integer) method2.invoke(wifi);
 86         } catch (Exception e) {
 87            Log.e(WIFI_SERVICE, e.getMessage());
 88            // toastText += "ERROR " + e.getMessage();
 89         }
 90 
 91         if (!enabled) {
 92             int loopMax = 10;
 93             while (loopMax>0 && (getWifiAPState()==WIFI_AP_STATE_DISABLING
 94                     || getWifiAPState()==WIFI_AP_STATE_ENABLED
 95                     || getWifiAPState()==WIFI_AP_STATE_FAILED)) {
 96                 try {Thread.sleep(500);loopMax--;} catch (Exception e) {}
 97             }
 98             wifi.setWifiEnabled(true);
 99         } else if (enabled) {
100             int loopMax = 10;
101             while (loopMax>0 && (getWifiAPState()==WIFI_AP_STATE_ENABLING
102                     || getWifiAPState()==WIFI_AP_STATE_DISABLED
103                     || getWifiAPState()==WIFI_AP_STATE_FAILED)) {
104                 try {Thread.sleep(500);loopMax--;} catch (Exception e) {}
105             }
106         }
107 
108         return state;
109     }
110 
111 
112     private int getWifiAPState() {
113         int state = WIFI_AP_STATE_UNKNOWN;
114         try {
115             Method method2 = wifi.getClass().getMethod("getWifiApState");
116             state = (Integer) method2.invoke(wifi);
117         } catch (Exception e) {}
118         Log.d("WifiAP", "getWifiAPState.state " + (state==-1?"UNKNOWN":WIFI_STATE_TEXTSTATE[state]));
119         return state;
120     }
121 
122     private void updateStatusDisplay() {
123 
124         if (getWifiAPState()==WIFI_AP_STATE_ENABLED || getWifiAPState()==WIFI_AP_STATE_ENABLING) {
125             ((Button)findViewById(R.id.btnWifiToggle)).setText("Turn off");
126             findViewById(R.id.bg).setBackgroundResource(R.drawable.bg_wifi_on);
127         } else {
128             ((Button)findViewById(R.id.btnWifiToggle)).setText("Turn on");
129             findViewById(R.id.bg).setBackgroundResource(R.drawable.bg_wifi_off);
130         }
131 
132     }
133 
134 
135     class SetWifiAPTask extends AsyncTask<Void, Void, Void> {
136 
137         boolean mMode;
138         boolean mFinish;
139 
140         public SetWifiAPTask(boolean mode, boolean finish) {
141             mMode = mode;
142             mFinish = finish;
143         }
144 
145         ProgressDialog d = new ProgressDialog(WifiAP.this);
146 
147         @Override
148         protected void onPreExecute() {
149             super.onPreExecute();
150             d.setTitle("Turning WiFi AP " + (mMode?"on":"off") + "...");
151             d.setMessage("...please wait a moment.");
152             d.show();
153         }
154 
155         @Override
156         protected void onPostExecute(Void aVoid) {
157             super.onPostExecute(aVoid);
158             try {d.dismiss();} catch (IllegalArgumentException e) {};
159             updateStatusDisplay();
160             if (mFinish) finish();
161         }
162 
163         @Override
164         protected Void doInBackground(Void... params) {
165             setWifiApEnabled(mMode);
166             return null;
167         }
168     }
169 
170 
171 }

这是在stackoverflow上看到的一个例子,但是运行不成功,原因不详..继续了解中...

posted @ 2012-11-16 19:40  ifeixiang  阅读(7638)  评论(1编辑  收藏  举报