android.net.wifi.WifiManager类的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(230)

本文整理了Java中android.net.wifi.WifiManager类的一些代码示例,展示了WifiManager类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WifiManager类的具体详情如下:
包路径:android.net.wifi.WifiManager
类名称:WifiManager

WifiManager介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

public class WifiReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {

   NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
   if(info != null && info.isConnected()) {
    // Do your work. 

    // e.g. To check the Network Name or other info:
    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String ssid = wifiInfo.getSSID();
   }
  }
}

代码示例来源:origin: stackoverflow.com

private boolean checkWifiOnAndConnected() {
  WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);

  if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON

    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

    if( wifiInfo.getNetworkId() == -1 ){
      return false; // Not connected to an access point
    }
    return true; // Connected to an access point
  }
  else {
    return false; // Wi-Fi adapter is OFF
  }
}

代码示例来源:origin: RobotiumTech/robotium

/**
   * Sets if wifi data should be turned on or off. Requires android.permission.CHANGE_WIFI_STATE in the AndroidManifest.xml of the application under test. 
   *  
   * 
   * @param turnedOn true if mobile wifi is to be turned on and false if not
   */

  public void setWiFiData(Boolean turnedOn){
    WifiManager wifiManager = (WifiManager)instrumentation.getTargetContext().getSystemService(Context.WIFI_SERVICE);
    try{
      wifiManager.setWifiEnabled(turnedOn);
    }catch(Exception e){
      e.printStackTrace();
    }
  }
}

代码示例来源:origin: stackoverflow.com

WifiConfiguration wifiConf = null;
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
    List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();        
    for (WifiConfiguration conf : configuredNetworks){
      if (conf.networkId == connectionInfo.getNetworkId()){
        wifiConf = conf;
        break;              
      }
    }

代码示例来源:origin: stackoverflow.com

WifiConfiguration wc = new WifiConfiguration(); 
wc.SSID = "\"SSID_NAME\""; //IMP! This should be in Quotes!!
wc.hiddenSSID = true;
boolean res1 = wifiManag.setWifiEnabled(true);
int res = wifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res );
boolean es = wifi.saveConfiguration();
Log.d("WifiPreference", "saveConfiguration returned " + es );
boolean b = wifi.enableNetwork(res, true);   
Log.d("WifiPreference", "enableNetwork returned " + b );

代码示例来源:origin: google/ExoPlayer

@Override
public void onStart() {
 Context appContext = getApplicationContext();
 WifiManager wifiManager = (WifiManager) appContext.getSystemService(Context.WIFI_SERVICE);
 wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, TAG);
 wifiLock.acquire();
 PowerManager powerManager = (PowerManager) appContext.getSystemService(Context.POWER_SERVICE);
 wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
 wakeLock.acquire();
 super.onStart();
}

代码示例来源:origin: amitshekhariitbhu/Android-Debug-Database

public static String getAddressLog(Context context, int port) {
  WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
  @SuppressLint("DefaultLocale")
  final String formattedIpAddress = String.format("%d.%d.%d.%d",
      (ipAddress & 0xff),
      (ipAddress >> 8 & 0xff),
      (ipAddress >> 16 & 0xff),
      (ipAddress >> 24 & 0xff));
  return "Open http://" + formattedIpAddress + ":" + port + " in your browser";
}

代码示例来源:origin: wangdan/AisenWeiBo

public static String getDeviceInfo(Context context) {
  try {
    org.json.JSONObject json = new org.json.JSONObject();
    android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);
    String device_id = null;
    if (checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
      device_id = tm.getDeviceId();
    }
    android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context
        .getSystemService(Context.WIFI_SERVICE);
    String mac = wifi.getConnectionInfo().getMacAddress();
    json.put("mac", mac);
    if (TextUtils.isEmpty(device_id)) {
      device_id = mac;
    }
    if (TextUtils.isEmpty(device_id)) {
      device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
          android.provider.Settings.Secure.ANDROID_ID);
    }
    json.put("device_id", device_id);
    return json.toString();
  } catch (Exception e) {
    e.printStackTrace();
  }
  return null;
}

代码示例来源:origin: facebook/facebook-android-sdk

@Override
public void initAndCheckEligibility() throws ScannerException {
  if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
    throw new ScannerException(ScannerException.Type.NOT_SUPPORTED);
  }
  if (!Validate.hasWiFiPermission(context)) {
    throw new ScannerException(ScannerException.Type.PERMISSION_DENIED);
  }
  if (wifiManager == null) {
    wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  }
  boolean isWifiScanningAlwaysOn = isWifiScanningAlwaysOn();
  if (!isWifiScanningAlwaysOn && !wifiManager.isWifiEnabled()) {
    throw new ScannerException(ScannerException.Type.DISABLED);
  }
}

代码示例来源:origin: syncthing/syncthing-android

private boolean isWifiConnectionWhitelisted(Set<String> whitelistedSsids) {
  WifiManager wifiManager = (WifiManager) mContext.getApplicationContext()
      .getSystemService(Context.WIFI_SERVICE);
  WifiInfo wifiInfo = wifiManager.getConnectionInfo();
  if (wifiInfo == null) {
    // May be null, if wifi has been turned off in the meantime.
    Log.d(TAG, "isWifiConnectionWhitelisted: SSID unknown due to wifiInfo == null");
    return false;
  }
  String wifiSsid = wifiInfo.getSSID();
  if (wifiSsid == null) {
    Log.w(TAG, "isWifiConnectionWhitelisted: Got null SSID. Try to enable android location service.");
    return false;
  }
  return whitelistedSsids.contains(wifiSsid);
}

代码示例来源:origin: syncthing/syncthing-android

/**
 * Load the configured WiFi networks, sort them by SSID.
 *
 * @return a sorted array of WifiConfiguration, or null, if data cannot be retrieved
 */
private WifiConfiguration[] loadConfiguredNetworksSorted() {
  WifiManager wifiManager = (WifiManager)
      getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  if (wifiManager != null) {
    List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
    // if WiFi is turned off, getConfiguredNetworks returns null on many devices
    if (configuredNetworks != null) {
      WifiConfiguration[] result = configuredNetworks.toArray(new WifiConfiguration[configuredNetworks.size()]);
      Arrays.sort(result, (lhs, rhs) -> {
        // See #620: There may be null-SSIDs
        String l = lhs.SSID != null ? lhs.SSID : "";
        String r = rhs.SSID != null ? rhs.SSID : "";
        return l.compareToIgnoreCase(r);
      });
      return result;
    }
  }
  // WiFi is turned off or device doesn't have WiFi
  return null;
}

代码示例来源:origin: smuyyh/BookReader

/**
 * 获取当前连接wifi的名称
 *
 * @return
 */
public static String getConnectWifiSsid(Context context) {
  if (isWifiConnected(context)) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    return wifiInfo.getSSID();
  }
  return null;
}

代码示例来源:origin: stackoverflow.com

ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
 final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
 final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
 if (connectionInfo != null && !(connectionInfo.getSSID().equals(""))) {
  ssid = connectionInfo.getSSID();
 WifiInfo info = wifiManager.getConnectionInfo();
 String textStatus = "";
 textStatus += "\n\nWiFi Status: " + info.toString();
 String BSSID = info.getBSSID();
 String MAC = info.getMacAddress();
 List<ScanResult> results = wifiManager.getScanResults();
 ScanResult bestSignal = null;
 int count = 1;
 List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
 for (WifiConfiguration config : configs) {
   textStatus+= "\n\n" + config.toString();

代码示例来源:origin: smuyyh/BookReader

/**
 * 获取 MAC 地址
 * 须配置android.permission.ACCESS_WIFI_STATE权限
 */
public static String getMacAddress(Context context) {
  //wifi mac地址
  WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  WifiInfo info = wifi.getConnectionInfo();
  String mac = info.getMacAddress();
  LogUtils.i(TAG, " MAC:" + mac);
  return mac;
}

代码示例来源:origin: stackoverflow.com

protected String wifiIpAddress(Context context) {
  WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
  int ipAddress = wifiManager.getConnectionInfo().getIpAddress();

  // Convert little-endian to big-endianif needed
  if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
    ipAddress = Integer.reverseBytes(ipAddress);
  }

  byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();

  String ipAddressString;
  try {
    ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
  } catch (UnknownHostException ex) {
    Log.e("WIFIIP", "Unable to get host address.");
    ipAddressString = null;
  }

  return ipAddressString;
}

代码示例来源:origin: sytolk/TaxiAndroidOpen

private int isHighBandwidth() {
  ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo info = cm.getActiveNetworkInfo();
  if (info.getType() == ConnectivityManager.TYPE_WIFI) {
    WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    return wm.getConnectionInfo().getLinkSpeed();
  } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
    TelephonyManager tm = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
    return tm.getNetworkType();
  }
  return 0;
}

代码示例来源:origin: stackoverflow.com

public String getWifiName(Context context) {
  WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  if (manager.isWifiEnabled()) {
    WifiInfo wifiInfo = manager.getConnectionInfo();
    if (wifiInfo != null) {
     DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
     if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) {
       return wifiInfo.getSSID();
     }
    }
  }
  return null;
}

代码示例来源:origin: stackoverflow.com

IntentFilter i = new IntentFilter(); 
i.addAction (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 
registerReceiver(new BroadcastReceiver(){ 
   public void onReceive(Context c, Intent i){ 
   // Code to execute when SCAN_RESULTS_AVAILABLE_ACTION event occurs 
   WifiManager w = (WifiManager) c.getSystemService(Context.WIFI_SERVICE); 
   myScanResultHandler(w.getScanResults()); // your method to handle Scan results
   if (ScanAsFastAsPossible) w.startScan(); // relaunch scan immediately
   else { /* Schedule the scan to be run later here */}
   } 
  }, i ); 

  // Launch  wifiscanner the first time here (it will call the broadcast receiver above)
  WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
  boolean a = wm.startScan();

代码示例来源:origin: mayubao/KuaiChuan

public static boolean configApState(Context context, String apName) {
    WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    WifiConfiguration wificonfiguration = null;
    try {
      wificonfiguration = new WifiConfiguration();
      wificonfiguration.SSID = apName;
      // if WiFi is on, turn it off
      if(isApOn(context)) {
        wifimanager.setWifiEnabled(false);
        // if ap is on and then disable ap
        disableAp(context);
      }
      Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
      method.invoke(wifimanager, wificonfiguration, !isApOn(context));
      return true;
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }
} // end of class

代码示例来源:origin: NimbleDroid/FriendlyDemo

private boolean isOnGoodWIFIConnection(@NonNull NetworkInfo netInfo) {
  return (netInfo.getType() == TYPE_WIFI)
      && (WifiManager.calculateSignalLevel(wifiManager.getConnectionInfo().getRssi(), 4) > 0);
}

相关文章