2016-11-28 46 views
0

我想掃描可用的Wi-Fi網絡來獲取網絡列表並選擇特定的網絡連接。它的工作是棒棒糖,但不是棉花糖。根據thisthis博客,幾乎沒有什麼變化,但我沒有得到如何實現這些變化。棉花糖wifi掃描和連接不工作

目前在棉花糖,應用程序只連接到由應用程序創建的那些網絡。當我嘗試連接到其他網絡時,應用程序崩潰。

這裏是我的活動代碼:

import android.Manifest; 
import android.annotation.TargetApi; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.pm.PackageManager; 
import android.net.wifi.ScanResult; 
import android.net.wifi.WifiConfiguration; 
import android.net.wifi.WifiManager; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 


public class CustomWifiListAcivity extends AppCompatActivity { 

    private TextView mainText; 
    private ListView wifiDeviceList; 
    private WifiManager mainWifi; 
    private CustomWifiListAcivity.WifiReceiver receiverWifi; 
    private List<ScanResult> wifiList; 
    private StringBuilder sb; 
    private String ssid_selected=null; 
    private Context context=null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.wifi_list); 
     context=getApplicationContext(); 
     setTitle("Choose Switchbox"); 
     wifiDeviceList=(ListView)findViewById(R.id.listView); 
     mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
     if (!mainWifi.isWifiEnabled()) 
     { 
      Toast.makeText(getApplicationContext(), "Turning WiFi ON...", Toast.LENGTH_LONG).show(); 
      mainWifi.setWifiEnabled(true); 
     } 

     wifiDeviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 
       final String ssid = '"' + wifiList.get(position).SSID + '"'; 
       final String mac = wifiList.get(position).BSSID; 
       String pass = ""; 



       LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); 
       View promptView = inflater.inflate(R.layout.dialog_wifi, null); 

       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CustomWifiListAcivity.this); 
       alertDialogBuilder.setView(promptView); 
       final EditText ssid_et = (EditText) promptView.findViewById(R.id.editText_ssid); 
       final EditText pass_et = (EditText) promptView.findViewById(R.id.editText_pswd); 
       ssid_et.setText(wifiList.get(position).SSID); 
       alertDialogBuilder 
         .setCancelable(true) 
         .setPositiveButton("Connect", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           String ssid = '"' + wifiList.get(position).SSID + '"'; 
           String password = '"' + pass_et.getText().toString() + '"'; 

           System.out.println("ssid: "+ssid); 
           System.out.println("password: "+password); 
           connectToHost(getApplicationContext(),ssid,password); 

          } 
         }) 
         .setNegativeButton("Cancel", 
           new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int id) { 
             dialog.cancel(); 
            } 
           }) 
         .create() 
         .show(); 
      } 
     }); 
    } 
    public void connectToHost(Context context,String host,String password){ 
     mainWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
     WifiConfiguration wc=new WifiConfiguration(); 
     wc.SSID= host; 
     wc.preSharedKey = password;; 
     wc.status = WifiConfiguration.Status.ENABLED; 
     wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 
     wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA 
     wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2 
     wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
     wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); 
     wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
     wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
     wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
     wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 


     int netId=mainWifi.addNetwork(wc); 

     try { 
      mainWifi.enableNetwork(netId, true); 
      mainWifi.setWifiEnabled(true); 
      System.out.println("enabled network"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println(e.getMessage()); 
     } 
    } 

    class WifiReceiver extends BroadcastReceiver { 
     public void onReceive(Context c, Intent intent) { 
      String action = intent.getAction(); 
      if(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)){ 
       sb = new StringBuilder(); 
       wifiList = mainWifi.getScanResults(); 
       sb.append("\n  Number Of Wifi connections :"+wifiList.size()+"\n\n"); 

       ArrayList<String> deviceList = new ArrayList<String>(); 
       for(int i = 0; i < wifiList.size(); i++){ 
        sb.append(new Integer(i+1).toString() + ". "); 
        sb.append((wifiList.get(i)).toString()); 
        sb.append("\n\n"); 
        deviceList.add(wifiList.get(i).SSID); 
       } 


       ArrayAdapter arrayAdapter = new ArrayAdapter(CustomWifiListAcivity.this, 
         android.R.layout.simple_list_item_1, deviceList.toArray()); 
       wifiDeviceList.setAdapter(arrayAdapter); 
      } 

     } 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     unregisterReceiver(receiverWifi); 
    } 

    @Override 
    protected void onPostResume() { 
     super.onPostResume(); 
     receiverWifi = new CustomWifiListAcivity.WifiReceiver(); 
     IntentFilter intentFilter = new IntentFilter(); 
     intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 
     registerReceiver(receiverWifi, intentFilter); 
     getWifi(); 
    } 
    private void getWifi() { 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      Toast.makeText(CustomWifiListAcivity.this, "version>=marshmallow", Toast.LENGTH_SHORT).show(); 


      if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       Toast.makeText(CustomWifiListAcivity.this, "location turned off", Toast.LENGTH_SHORT).show(); 
       requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1); 
      } 
      else{ 

       Toast.makeText(CustomWifiListAcivity.this, "location turned on", Toast.LENGTH_SHORT).show(); 
       mainWifi.startScan(); 
      } 
     }else { 

      Toast.makeText(CustomWifiListAcivity.this, "scanning", Toast.LENGTH_SHORT).show(); 
      mainWifi.startScan(); 
     } 
    } 
    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 

     if (requestCode == 1) { 

      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       Toast.makeText(CustomWifiListAcivity.this, "permission granted", Toast.LENGTH_SHORT).show(); mainWifi.startScan(); 
      }else{ 
       Toast.makeText(CustomWifiListAcivity.this, "permission not granted", Toast.LENGTH_SHORT).show(); 
       return; 
      } 


     } 
    } 
} 

,這裏是來自清單文件的權限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
+0

在授予'mainWifi.startScan();'權限並檢查後,在onRequestPermissionsResult成功上添加此代碼。 – Tasneem

+0

代碼 – Piyush

+0

已經具有運行時權限,除了顯示敬酒之外,您在收到權限後不會執行任何操作。 –

回答

2

我已成功地解決這一問題。以下代碼適用於棉花糖。在棉花糖中不允許重新配置以前配置的網絡。

import android.Manifest; 
import android.annotation.TargetApi; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.pm.PackageManager; 
import android.net.wifi.ScanResult; 
import android.net.wifi.WifiConfiguration; 
import android.net.wifi.WifiManager; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 


public class CustomWifiListAcivity extends AppCompatActivity { 

    private TextView mainText; 
    private ListView wifiDeviceList; 
    private WifiManager mainWifi; 
    private CustomWifiListAcivity.WifiReceiver receiverWifi; 
    private List<ScanResult> wifiList; 
    private StringBuilder sb; 
    private String ssid_selected=null; 
    private Context context=null; 

    private static final int MY_PERMISSIONS_REQUEST =1 ; 

    Button connect_btn; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.wifi_list); 
     context=getApplicationContext(); 
     setTitle("Choose Switchbox"); 
     connect_btn=(Button) findViewById(R.id.connect_btn); 

     wifiDeviceList=(ListView)findViewById(R.id.listView); 
     mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
     if (!mainWifi.isWifiEnabled()) 
     { 
      Toast.makeText(getApplicationContext(), "Turning WiFi ON...", Toast.LENGTH_LONG).show(); 
      mainWifi.setWifiEnabled(true); 
     } 

     wifiDeviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 
       final String ssid = '"' + wifiList.get(position).SSID + '"'; 
       final String mac = wifiList.get(position).BSSID; 
       String pass = ""; 


       LayoutInflater inflater = LayoutInflater.from(getApplicationContext()); 
       View promptView = inflater.inflate(R.layout.dialog_wifi, null); 

       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(CustomWifiListAcivity.this); 
       alertDialogBuilder.setView(promptView); 
       final EditText ssid_et = (EditText) promptView.findViewById(R.id.editText_ssid); 
       final EditText pass_et = (EditText) promptView.findViewById(R.id.editText_pswd); 
       ssid_et.setText(wifiList.get(position).SSID); 
       alertDialogBuilder 
         .setCancelable(true) 
         .setPositiveButton("Connect", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           String ssid = '"' + wifiList.get(position).SSID + '"'; 
           String password = '"' + pass_et.getText().toString() + '"'; 

           System.out.println("ssid: "+ssid); 
           System.out.println("password: "+password); 

           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
            connectToHost(getApplicationContext(), ssid, password); 
           } 
           else{ 
            connectToHost2(getApplicationContext(), ssid, password); 
           } 

          } 
         }) 
         .setNegativeButton("Cancel", 
           new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int id) { 
             dialog.cancel(); 
            } 
           }) 
         .create() 
         .show(); 
      } 
     }); 

    } 



    public void connectToHost(Context context,String host,String password){ 
     mainWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
     WifiConfiguration wc=new WifiConfiguration(); 

     wc.SSID= host; 
     wc.preSharedKey = password; 

     int netId=mainWifi.addNetwork(wc); 


     try { 
      mainWifi.enableNetwork(netId, true); 
      mainWifi.setWifiEnabled(true); 

      System.out.println("enabled network"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println(e.getMessage()); 
     } 
    } 


    public void connectToHost2(Context context,String host,String password){ 
     mainWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
     WifiConfiguration wc=new WifiConfiguration(); 

     wc.SSID= host; 
     wc.preSharedKey = password; 
      wc.status = WifiConfiguration.Status.ENABLED; 
      wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 
      wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA 
      wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2 
      wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
      wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); 
      wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
      wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
      wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
      wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 


     int netId=mainWifi.addNetwork(wc); 


     try { 
      mainWifi.enableNetwork(netId, true); 
      mainWifi.setWifiEnabled(true); 

      System.out.println("enabled network"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println(e.getMessage()); 
     } 
    } 
    class WifiReceiver extends BroadcastReceiver { 
     public void onReceive(Context c, Intent intent) { 
      String action = intent.getAction(); 
      if(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)){ 
       sb = new StringBuilder(); 
       wifiList = mainWifi.getScanResults(); 
       sb.append("\n Number Of Wifi connections :"+wifiList.size()+"\n\n"); 

       ArrayList<String> deviceList = new ArrayList<String>(); 
       for(int i = 0; i < wifiList.size(); i++){ 
        sb.append(new Integer(i+1).toString() + ". "); 
        sb.append((wifiList.get(i)).toString()); 
        sb.append("\n\n"); 
        deviceList.add(wifiList.get(i).SSID); 
       } 

       ArrayAdapter arrayAdapter = new ArrayAdapter(CustomWifiListAcivity.this, 
         android.R.layout.simple_list_item_1, deviceList.toArray()); 

       wifiDeviceList.setAdapter(arrayAdapter); 
      } 

     } 

    } 



    @Override 
    protected void onPause() { 
     super.onPause(); 
     unregisterReceiver(receiverWifi); 
    } 

    @Override 
    protected void onPostResume() { 
     super.onPostResume(); 
     receiverWifi = new CustomWifiListAcivity.WifiReceiver(); 
     IntentFilter intentFilter = new IntentFilter(); 
     intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 
     registerReceiver(receiverWifi, intentFilter); 
     getWifi(); 
    } 
    private void getWifi() { 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      Toast.makeText(CustomWifiListAcivity.this, "version>=marshmallow", Toast.LENGTH_SHORT).show(); 

      if (ContextCompat.checkSelfPermission(CustomWifiListAcivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       Toast.makeText(CustomWifiListAcivity.this, "location turned off", Toast.LENGTH_SHORT).show(); 

       ActivityCompat.requestPermissions(CustomWifiListAcivity.this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST); 

      } 

      else{ 
       Toast.makeText(CustomWifiListAcivity.this, "location turned on", Toast.LENGTH_SHORT).show(); 
       mainWifi.startScan(); 
      } 

     }else { 

      Toast.makeText(CustomWifiListAcivity.this, "scanning", Toast.LENGTH_SHORT).show(); 
      mainWifi.startScan(); 
     } 
    } 


    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 

     if (requestCode == MY_PERMISSIONS_REQUEST) { 

      if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       Toast.makeText(CustomWifiListAcivity.this, "permission granted", Toast.LENGTH_SHORT).show(); 
       mainWifi.startScan(); 

      }else{ 
       Toast.makeText(CustomWifiListAcivity.this, "permission not granted", Toast.LENGTH_SHORT).show(); 
       return; 
      } 

     } 
    } 
} 

connectToHost()方法是用於棉花糖。 connectToHost2()方法適用於棉花糖以下的設備。