Android 连接Wifi和创建Wifi热点的实例

本文实例讲述了连接Wifi和创建Wifi热点的方法。分享给大家供大家参考,具体如下:

android的热点功能不可见,用了反射的技术搞定之外。

Eclipse设置语言为utf-8才能查看中文注释

上代码:

MainActivity.java

package com.widget.hotspot;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button; 

public class MainActivity extends Activity { 

  public static final String TAG = "MainActivity"; 

  private Button mBtn1, mBtn2; 

  private WifiAdmin mWifiAdmin; 

  private Context mContext = null;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    mContext = this; 

    setContentView(R.layout.activity_main); 

    mBtn1 = (Button)findViewById(R.id.button1);
    mBtn2 = (Button)findViewById(R.id.button2);
    mBtn1.setText("点击连接Wifi");
    mBtn2.setText("点击创建Wifi热点");
    mBtn1.setOnClickListener(new Button.OnClickListener() { 

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub 

        mWifiAdmin = new WifiAdmin(mContext) { 

          @Override
          public void myUnregisterReceiver(BroadcastReceiver receiver) {
            // TODO Auto-generated method stub
            MainActivity.this.unregisterReceiver(receiver);
          } 

          @Override
          public Intent myRegisterReceiver(BroadcastReceiver receiver,
              IntentFilter filter) {
            // TODO Auto-generated method stub
            MainActivity.this.registerReceiver(receiver, filter);
            return null;
          } 

          @Override
          public void onNotifyWifiConnected() {
            // TODO Auto-generated method stub
            Log.v(TAG, "have connected success!");
            Log.v(TAG, "###############################");
          } 

          @Override
          public void onNotifyWifiConnectFailed() {
            // TODO Auto-generated method stub
            Log.v(TAG, "have connected failed!");
            Log.v(TAG, "###############################");
          }
        };
        mWifiAdmin.openWifi();
        mWifiAdmin.addNetwork(mWifiAdmin.createWifiInfo("YOU_WIFI", "MM123456", WifiAdmin.TYPE_WPA)); 

      }
    }); 

    mBtn2.setOnClickListener(new Button.OnClickListener() { 

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub 

        WifiApAdmin wifiAp = new WifiApAdmin(mContext);
        wifiAp.startWifiAp("\"HotSpot\"", "hhhhhh123");
      }
    }); 

  } 

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
  } 

   @Override
    public void onResume() {
      super.onResume(); 

      Log.d("Rssi", "Registered");
    } 

    @Override
    public void onPause() {
      super.onPause(); 

      Log.d("Rssi", "Unregistered");
    } 

}

WifiAdmin.java

package com.widget.hotspot;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.util.Log; 

public abstract class WifiAdmin { 

  private static final String TAG = "WifiAdmin"; 

  private WifiManager mWifiManager;
  private WifiInfo mWifiInfo;
  // 扫描出的网络连接列表
  private List<ScanResult> mWifiList;
  private List<WifiConfiguration> mWifiConfiguration; 

  private WifiLock mWifiLock; 

  private String mPasswd = "";
  private String mSSID = ""; 

  private Context mContext = null; 

  public WifiAdmin(Context context) { 

    mContext = context; 

    // 取得WifiManager对象
    mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    // 取得WifiInfo对象
    mWifiInfo = mWifiManager.getConnectionInfo(); 

    Log.v(TAG, "getIpAddress = " + mWifiInfo.getIpAddress());
  } 

  // 打开WIFI
  public void openWifi() {
    if (!mWifiManager.isWifiEnabled()) {
      mWifiManager.setWifiEnabled(true);
    }
  } 

  // 关闭WIFI
  public void closeWifi() {
    if (mWifiManager.isWifiEnabled()) {
      mWifiManager.setWifiEnabled(false);
    }
  } 

  public abstract Intent myRegisterReceiver(BroadcastReceiver receiver, IntentFilter filter); 

  public abstract void myUnregisterReceiver(BroadcastReceiver receiver); 

  public abstract void onNotifyWifiConnected(); 

  public abstract void onNotifyWifiConnectFailed(); 

  // 添加一个网络并连接
  public void addNetwork(WifiConfiguration wcg) { 

    register(); 

    WifiApAdmin.closeWifiAp(mContext); 

    int wcgID = mWifiManager.addNetwork(wcg);
    boolean b = mWifiManager.enableNetwork(wcgID, true);
  } 

  public static final int TYPE_NO_PASSWD = 0x11;
  public static final int TYPE_WEP = 0x12;
  public static final int TYPE_WPA = 0x13; 

  public void addNetwork(String ssid, String passwd, int type) {
    if (ssid == null || passwd == null || ssid.equals("")) {
      Log.e(TAG, "addNetwork() ## nullpointer error!");
      return;
    } 

    if (type != TYPE_NO_PASSWD && type != TYPE_WEP && type != TYPE_WPA) {
      Log.e(TAG, "addNetwork() ## unknown type = " + type);
    } 

    stopTimer();
    unRegister(); 

    addNetwork(createWifiInfo(ssid, passwd, type));
  } 

  private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 

    @Override
    public void onReceive(Context context, Intent intent) {
      // TODO Auto-generated method stub
      if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) {
        Log.d(TAG, "RSSI changed"); 

        //有可能是正在获取,或者已经获取了
        Log.d(TAG, " intent is " + WifiManager.RSSI_CHANGED_ACTION); 

        if (isWifiContected(mContext) == WIFI_CONNECTED) {
          stopTimer();
          onNotifyWifiConnected();
          unRegister();
        } else if (isWifiContected(mContext) == WIFI_CONNECT_FAILED) {
          stopTimer();
          closeWifi();
          onNotifyWifiConnectFailed();
          unRegister();
        } else if (isWifiContected(mContext) == WIFI_CONNECTING) { 

        }
      }
    }
  }; 

  private final int STATE_REGISTRING = 0x01;
  private final int STATE_REGISTERED = 0x02;
  private final int STATE_UNREGISTERING = 0x03;
  private final int STATE_UNREGISTERED = 0x04; 

  private int mHaveRegister = STATE_UNREGISTERED;
  private synchronized void register() {
    Log.v(TAG, "register() ##mHaveRegister = " + mHaveRegister); 

    if (mHaveRegister == STATE_REGISTRING
        || mHaveRegister == STATE_REGISTERED) {
      return ;
    } 

    mHaveRegister = STATE_REGISTRING;
    myRegisterReceiver(mBroadcastReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
    mHaveRegister = STATE_REGISTERED; 

    startTimer();
  } 

  private synchronized void unRegister() {
    Log.v(TAG, "unRegister() ##mHaveRegister = " + mHaveRegister); 

    if (mHaveRegister == STATE_UNREGISTERED
        || mHaveRegister == STATE_UNREGISTERING) {
      return ;
    } 

    mHaveRegister = STATE_UNREGISTERING;
    myUnregisterReceiver(mBroadcastReceiver);
    mHaveRegister = STATE_UNREGISTERED;
  } 

  private Timer mTimer = null;
  private void startTimer() {
    if (mTimer != null) {
      stopTimer();
    } 

    mTimer = new Timer(true);
//   mTimer.schedule(mTimerTask, 0, 20 * 1000);// 20s
    mTimer.schedule(mTimerTask, 30 * 1000);
  } 

  private TimerTask mTimerTask = new TimerTask() { 

    @Override
    public void run() {
      // TODO Auto-generated method stub
      Log.e(TAG, "timer out!");
      onNotifyWifiConnectFailed();
      unRegister();
    }
  }; 

  private void stopTimer() {
    if (mTimer != null) {
      mTimer.cancel();
      mTimer = null;
    }
  } 

  @Override
  protected void finalize() {
    try {
      super.finalize();
      unRegister();
    } catch (Throwable e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  } 

  public WifiConfiguration createWifiInfo(String SSID, String password, int type) { 

    Log.v(TAG, "SSID = " + SSID + "## Password = " + password + "## Type = " + type); 

    WifiConfiguration config = new WifiConfiguration();
    config.allowedAuthAlgorithms.clear();
    config.allowedGroupCiphers.clear();
    config.allowedKeyManagement.clear();
    config.allowedPairwiseCiphers.clear();
    config.allowedProtocols.clear();
    config.SSID = "\"" + SSID + "\""; 

    WifiConfiguration tempConfig = this.IsExsits(SSID);
    if (tempConfig != null) {
      mWifiManager.removeNetwork(tempConfig.networkId);
    } 

    // 分为三种情况:1没有密码2用wep加密3用wpa加密
    if (type == TYPE_NO_PASSWD) {// WIFICIPHER_NOPASS
      config.wepKeys[0] = "";
      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
      config.wepTxKeyIndex = 0; 

    } else if (type == TYPE_WEP) { // WIFICIPHER_WEP
      config.hiddenSSID = true;
      config.wepKeys[0] = "\"" + password + "\"";
      config.allowedAuthAlgorithms
          .set(WifiConfiguration.AuthAlgorithm.SHARED);
      config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
      config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
      config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
      config.allowedGroupCiphers
          .set(WifiConfiguration.GroupCipher.WEP104);
      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
      config.wepTxKeyIndex = 0;
    } else if (type == TYPE_WPA) {  // WIFICIPHER_WPA
      config.preSharedKey = "\"" + password + "\"";
      config.hiddenSSID = true;
      config.allowedAuthAlgorithms
          .set(WifiConfiguration.AuthAlgorithm.OPEN);
      config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
      config.allowedPairwiseCiphers
          .set(WifiConfiguration.PairwiseCipher.TKIP);
      // config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
      config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
      config.allowedPairwiseCiphers
          .set(WifiConfiguration.PairwiseCipher.CCMP);
      config.status = WifiConfiguration.Status.ENABLED;
    }  

    return config;
  } 

  public static final int WIFI_CONNECTED = 0x01;
  public static final int WIFI_CONNECT_FAILED = 0x02;
  public static final int WIFI_CONNECTING = 0x03;
  /**
   * 判断wifi是否连接成功,不是network
   *
   * @param context
   * @return
   */
  public int isWifiContected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifiNetworkInfo = connectivityManager
        .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

    Log.v(TAG, "isConnectedOrConnecting = " + wifiNetworkInfo.isConnectedOrConnecting());
    Log.d(TAG, "wifiNetworkInfo.getDetailedState() = " + wifiNetworkInfo.getDetailedState());
    if (wifiNetworkInfo.getDetailedState() == DetailedState.OBTAINING_IPADDR
        || wifiNetworkInfo.getDetailedState() == DetailedState.CONNECTING) {
      return WIFI_CONNECTING;
    } else if (wifiNetworkInfo.getDetailedState() == DetailedState.CONNECTED) {
      return WIFI_CONNECTED;
    } else {
      Log.d(TAG, "getDetailedState() == " + wifiNetworkInfo.getDetailedState());
      return WIFI_CONNECT_FAILED;
    }
  } 

  private WifiConfiguration IsExsits(String SSID) {
    List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();
    for (WifiConfiguration existingConfig : existingConfigs) {
      if (existingConfig.SSID.equals("\"" + SSID + "\"") /*&& existingConfig.preSharedKey.equals("\"" + password + "\"")*/) {
        return existingConfig;
      }
    }
    return null;
  } 

  // 断开指定ID的网络
  public void disconnectWifi(int netId) {
    mWifiManager.disableNetwork(netId);
    mWifiManager.disconnect();
  } 

  // 检查当前WIFI状态
  public int checkState() {
    return mWifiManager.getWifiState();
  } 

  // 锁定WifiLock
  public void acquireWifiLock() {
    mWifiLock.acquire();
  } 

  // 解锁WifiLock
  public void releaseWifiLock() {
    // 判断时候锁定
    if (mWifiLock.isHeld()) {
      mWifiLock.acquire();
    }
  } 

  // 创建一个WifiLock
  public void creatWifiLock() {
    mWifiLock = mWifiManager.createWifiLock("Test");
  } 

  // 得到配置好的网络
  public List<WifiConfiguration> getConfiguration() {
    return mWifiConfiguration;
  } 

  // 指定配置好的网络进行连接
  public void connectConfiguration(int index) {
    // 索引大于配置好的网络索引返回
    if (index > mWifiConfiguration.size()) {
      return;
    }
    // 连接配置好的指定ID的网络
    mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,
        true);
  } 

  public void startScan() {
    mWifiManager.startScan();
    mWifiList = mWifiManager.getScanResults();
    mWifiConfiguration = mWifiManager.getConfiguredNetworks();
  } 

  // 得到网络列表
  public List<ScanResult> getWifiList() {
    return mWifiList;
  } 

  // 查看扫描结果
  public StringBuilder lookUpScan() {
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < mWifiList.size(); i++) {
      stringBuilder
          .append("Index_" + new Integer(i + 1).toString() + ":");
      // 将ScanResult信息转换成一个字符串包
      // 其中把包括:BSSID、SSID、capabilities、frequency、level
      stringBuilder.append((mWifiList.get(i)).toString());
      stringBuilder.append("/n");
    }
    return stringBuilder;
  } 

  // 得到MAC地址
  public String getMacAddress() {
    return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
  } 

  // 得到接入点的BSSID
  public String getBSSID() {
    return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
  } 

  // 得到IP地址
  public int getIPAddress() {
    return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
  } 

  // 得到连接的ID
  public int getNetworkId() {
    return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
  } 

  // 得到WifiInfo的所有信息包
  public String getWifiInfo() {
    return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
  }
}

WifiApAdmin.java

package com.widget.hotspot; 

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Timer;
import java.util.TimerTask; 

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log; 

/**
 * 创建热点
 *
 */
public class WifiApAdmin {
  public static final String TAG = "WifiApAdmin"; 

  public static void closeWifiAp(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    closeWifiAp(wifiManager);
  } 

  private WifiManager mWifiManager = null; 

  private Context mContext = null;
  public WifiApAdmin(Context context) {
    mContext = context; 

    mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);  

    closeWifiAp(mWifiManager);
  } 

  private String mSSID = "";
  private String mPasswd = "";
  public void startWifiAp(String ssid, String passwd) {
    mSSID = ssid;
    mPasswd = passwd; 

    if (mWifiManager.isWifiEnabled()) {
      mWifiManager.setWifiEnabled(false);
    }  

    stratWifiAp(); 

    MyTimerCheck timerCheck = new MyTimerCheck() { 

      @Override
      public void doTimerCheckWork() {
        // TODO Auto-generated method stub 

        if (isWifiApEnabled(mWifiManager)) {
          Log.v(TAG, "Wifi enabled success!");
          this.exit();
        } else {
          Log.v(TAG, "Wifi enabled failed!");
        }
      } 

      @Override
      public void doTimeOutWork() {
        // TODO Auto-generated method stub
        this.exit();
      }
    };
    timerCheck.start(15, 1000); 

  } 

  public void stratWifiAp() {
    Method method1 = null;
    try {
      method1 = mWifiManager.getClass().getMethod("setWifiApEnabled",
          WifiConfiguration.class, boolean.class);
      WifiConfiguration netConfig = new WifiConfiguration(); 

      netConfig.SSID = mSSID;
      netConfig.preSharedKey = mPasswd; 

      netConfig.allowedAuthAlgorithms
          .set(WifiConfiguration.AuthAlgorithm.OPEN);
      netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
      netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
      netConfig.allowedKeyManagement
          .set(WifiConfiguration.KeyMgmt.WPA_PSK);
      netConfig.allowedPairwiseCiphers
          .set(WifiConfiguration.PairwiseCipher.CCMP);
      netConfig.allowedPairwiseCiphers
          .set(WifiConfiguration.PairwiseCipher.TKIP);
      netConfig.allowedGroupCiphers
          .set(WifiConfiguration.GroupCipher.CCMP);
      netConfig.allowedGroupCiphers
          .set(WifiConfiguration.GroupCipher.TKIP); 

      method1.invoke(mWifiManager, netConfig, true); 

    } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SecurityException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  } 

  private static void closeWifiAp(WifiManager wifiManager) {
    if (isWifiApEnabled(wifiManager)) {
      try {
        Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
        method.setAccessible(true); 

        WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager); 

        Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        method2.invoke(wifiManager, config, false);
      } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  } 

  private static boolean isWifiApEnabled(WifiManager wifiManager) {
    try {
      Method method = wifiManager.getClass().getMethod("isWifiApEnabled");
      method.setAccessible(true);
      return (Boolean) method.invoke(wifiManager); 

    } catch (NoSuchMethodException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } 

    return false;
  } 

}

MyTimeCheck.java

package com.widget.hotspot;
public abstract class MyTimerCheck {
  private int mCount = 0;
  private int mTimeOutCount = 1;
  private int mSleepTime = 1000; // 1s
  private boolean mExitFlag = false;
  private Thread mThread = null; 

  /**
   * Do not process UI work in this.
   */
  public abstract void doTimerCheckWork(); 

  public abstract void doTimeOutWork(); 

  public MyTimerCheck() {
    mThread = new Thread(new Runnable() { 

      @Override
      public void run() {
        // TODO Auto-generated method stub
        while (!mExitFlag) {
          mCount++;
          if (mCount < mTimeOutCount) {
            doTimerCheckWork();
            try {
              mThread.sleep(mSleepTime);
            } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              exit();
            }
          } else {
            doTimeOutWork();
          }
        }
      }
    });
  } 

  /**
   * start
   * @param times How many times will check?
   * @param sleepTime ms, Every check sleep time.
   */
  public void start(int timeOutCount, int sleepTime) {
    mTimeOutCount = timeOutCount;
    mSleepTime = sleepTime; 

    mThread.start();
  } 

  public void exit() {
    mExitFlag = true;
  } 

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android中判断有无可用网络的代码(是否是3G或者WIFI网络)

    复制代码 代码如下: ConnectivityManager mConnectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); TelephonyManager mTelephony = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE); //检查网络连接,如果无网络可用,就不需要进行连网操作等 NetworkInfo inf

  • Android编程实现获取当前连接wifi名字的方法

    本文实例讲述了Android编程实现获取当前连接wifi名字的方法.分享给大家供大家参考,具体如下: WifiManager wifiMgr = (WifiManager) mActivity.getSystemService(Context.WIFI_SERVICE); int wifiState = wifiMgr.getWifiState(); WifiInfo info = wifiMgr.getConnectionInfo(); String wifiId = info != null

  • Android开发之Wifi基础教程

    本文实例讲述了Android开发Wifi的基础知识.分享给大家供大家参考.具体如下: Android提供了WifiManager这个类,通过这个类可以进行wifi相关的各种操作. 通过 wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE) 可获取该类的实例. 1. 获取wifi开启状态 (只要手机的wifi打开了,即认为是开启状态,而与是否连接了某个wifi无关): boolean isOpen = wifiMana

  • Android判断是Wifi还是4G网络代码

    本文实例为大家分享了Android判断是Wifi还是4G网络的具体代码,供大家参考,具体内容如下 package com.chengxinet.bobo.utils; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.telephony.TelephonyManager; /** * Created by Ad

  • Android编程获取Wifi名称(SSID)的方法

    本文实例讲述了Android编程获取Wifi名称(SSID)的方法.分享给大家供大家参考,具体如下: 可以使用WifiManager和WifiInfo来获取当前Wifi连接信息 WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); Log.d("wifiInfo", wifiInfo.toSt

  • Android获取当前已连接的wifi信号强度的方法

    本文实例讲述了Android获取当前已连接的wifi信号强度的方法,是Android程序开发中非常常见的重要技巧.分享给大家供大家参考之用.具体方法如下: 1.得到当前已连接的wifi信息 WifiManager wifi_service = (WifiManager)getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifi_service.getConnectionInfo(); 其中wifiInfo有以下的方法: wifiinfo.ge

  • android wifi信号强度等级区分的修改介绍

    /frameworks/base/wifi/java/android/net/wifi/wifimanager.java calculateSignalLevel为计算信号等级函数,MAX_RSSI和MIN_RSSI分别为最强和最弱信号强度等级的信号强度阀值.

  • android编程实现设置、打开wifi热点共享供他人连接的方法

    本文实例讲述了android编程实现设置.打开wifi热点共享供他人连接的方法.分享给大家供大家参考,具体如下: 用过快牙的朋友应该知道它们在两天设备之间传输文件的时候使用的是wifi热点,然后另一台便连接这个热点再进行传输.快牙传输速度惊人应该跟它的这种机制有关系吧.不知道它的搜索机制是怎样的,但我想应该可以通过热点的名字来进行判断吧.下面我们就来探讨一下如何自动创建一个wifi热点吧 创建wifi热点首先需要手机支持,建议开发的哥们整个好点的手机,我们公司那些个山寨设备,几近有一半是不支持热

  • Android连接指定Wifi的方法实例代码

    本篇文章主要记录一下Android中打开Wifi.获取Wifi接入点信息及连接指接入点的方法. 自己写的demo主要用于测试接口的基本功能,因此界面及底层逻辑比较粗糙. demo的整体界面如下所示: 上图中的OPEN按键负责开启Wifi: GET按键负责获取扫描到的接入点信息. 当获取到接入点信息后,我选取了其中的名称及信号强度,以列表的形式显示在主界面下方,如下图: 当点击列表中的Item时,就会去连接对应的接入点. 自己的逻辑比较简单,测试时的代码,假定连接的是不许要密码或密码已知的接入点.

  • android开发教程之wifi开发示例

    1. WIFI网卡的状态WIFI网卡的状态信息都以整型变量的形式存放在 android.net.wifi.WifiManager 类中,有以下状态:WIFI_STATE_DISABLEDWIFI网卡不可用WIFI_STATE_DISABLINGWIFI网卡正在关闭WIFI_STATE_ENABLEDWIFI网卡可用WIFI_STATE_ENABLINGWIFI网卡正在打开WIFI_STATE_UNKNOWNWIFI网卡状态未知2. 操作WIFI 网卡所需的权限CHANGE_NETWORK_STA

  • 设置Android设备WIFI在休眠时永不断开的代码实现

    MainActivity如下: package cc.ab; import android.os.Bundle; import android.provider.Settings; import android.app.Activity; /** * Demo描述: * 设置设备在睡眠期间始终保持WLAN开启. * * 参考资料: * 1 http://stackoverflow.com/questions/8652031/how-to-modify-wi-fi-sleep-policy-pro

随机推荐