美文网首页
Android 在APP内、应用内 连接WiFi

Android 在APP内、应用内 连接WiFi

作者: 稻壳_互联网创变者 | 来源:发表于2019-04-22 19:58 被阅读0次

    Android 在应用内连接特定的WiFi

    首先需要在AndroidManifext.xml中添加以下权限

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <!-- for Android 6 and above -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <!-- for Android 6 and above -->
    

    通过如下代码,可以实现连接到这个ssid的WiFi

    String ssid = "work.dookay.com";
    String key = "1q23l_yc45j_";
    
    WifiConfiguration wifiConfig = new WifiConfiguration();
    wifiConfig.SSID = String.format("\"%s\"", ssid);
    wifiConfig.preSharedKey = String.format("\"%s\"", key);
    
     WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
     int netId = wifiManager.addNetwork(wifiConfig);
    wifiManager.disconnect();
     wifiManager.enableNetwork(netId, true);
     wifiManager.reconnect();
    

    启用WiFi

     WifiUtils.withContext(getApplicationContext()).enableWifi(this::checkResult);
     
      private void checkResult(boolean isSuccess)
      {
           if (isSuccess)
               Toast.makeText(MainActivity.this, "WIFI ENABLED", Toast.LENGTH_SHORT).show();
           else
               Toast.makeText(MainActivity.this, "COULDN'T ENABLE WIFI", Toast.LENGTH_SHORT).show();
      }
    

    启用WiFi且无需回调的方式:

     WifiUtils.withContext(getApplicationContext()).enableWifi();
    

    禁用WiFi

    WifiUtils.withContext(getApplicationContext()).disableWifi();
    

    扫描可用WiFi

    WifiUtils.withContext(getApplicationContext()).scanWifi(this::getScanResults).start();
    
    private void getScanResults(@NonNull final List<ScanResult> results)
    {
        if (results.isEmpty())
        {
            Log.i(TAG, "SCAN RESULTS IT'S EMPTY");
            return;
        }
        Log.i(TAG, "GOT SCAN RESULTS " + results);
    }
    

    连接WiFi

    你必须知道这个WiFi的SSID和WPA/WPS2密钥

     WifiUtils.withContext(getApplicationContext())
                            .connectWith("YourWiFi", "YourWiFiPassword")
                            .onConnectionResult(this::checkResult)
                            .start();
    
      private void checkResult(boolean isSuccess)
      {
            if (isSuccess)
                Toast.makeText(MainActivity.this, "CONNECTED YAY", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(MainActivity.this, "COULDN'T CONNECT", Toast.LENGTH_SHORT).show();
      }
    

    取消正在连接

    有两种方式:

    • 如果一个连接所需的时间太长而没要回调,那么你可以用setTimeout(毫秒)方法来设置一个时效,默认时效为30秒
    WifiUtils.withContext(getApplicationContext())
                         .connectWith("YourWiFi", "YourWiFiPassword")
                         .setTimeout(15000) //15秒
                         .onConnectionResult(this::checkResult)
                         .start();
    
    • 可以用以下方式立即取消连接
     WifiConnectorBuilder.WifiUtilsBuilder builder = WifiUtils.withContext(getApplicationContext());
     builder.connectWith("YourWiFi", "YourWiFiPassword")
     .onConnectionResult(this::checkResult)
     .start();
     builder.cancelAutoConnect();
    

    相关文章

      网友评论

          本文标题:Android 在APP内、应用内 连接WiFi

          本文链接:https://www.haomeiwen.com/subject/hvnugqtx.html