棉花糖wifi扫描和连接不工作

时间:2016-11-28 05:34:36

标签: android android-6.0-marshmallow

我正在尝试扫描可用的Wi-Fi网络以获取网络列表并选择要连接的特定网络。它在棒棒糖上工作但不在棉花糖上工作。根据{{​​3}}和this博客,几乎无法进行任何更改,但我没有了解如何实施这些更改。

目前在棉花糖中,应用程序仅连接到由app创建的网络。当我尝试连接到其他网络时,应用程序崩溃。

这是我的活动代码:

class Program
{
    static void Main(string[] args)
    {
        int[] akw, zuk;
        function(out akw, out zuk);
        Debug.WriteLine(akw.Length);
        Debug.WriteLine(zuk.Length);
    }

    public static void function(out int[] A, out int[] B) 
    {
        A=new int[25];
        B=new int[15];
        A[0]=42;
        B[0]=21;
    }
}

这是清单文件的权限

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;
            }


        }
    }
}

1 个答案:

答案 0 :(得分:2)

我设法解决了这个问题。以下代码适用于棉花糖。 marshmallow中不允许重新配置以前配置的网络。

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()方法适用于棉花糖以下的设备。