Android获取WiFi的ssid

 

1.在Androidmanifast.xml文件中添加权限

    
    
    
    
    
    
    

2.Andorid6.0开始动态申请权限,Android8.0获取ssid的方式有变化,Android9.0要开启GPS定位权限才能获取ssid。

2.1 动态申请权限

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wi_fi);
        
        Log.d(TAG, "android ver = " + Build.VERSION.SDK_INT);
        //Android6.0开始动态申请权限
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
            getWifiName();
        } else {
            requestPermissions();
        }
    }
    // 声明权限,将权限添加到list集合中再一次性申请
    private void requestPermissions(){
        List permissionList = new ArrayList<>();
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
//        if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
//        }
//        if (checkSelfPermission(Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.ACCESS_WIFI_STATE);
//        }
        if (!permissionList.isEmpty()) {
            String[] permissions = permissionList.toArray(new String[permissionList.size()]);
            requestPermissions(permissions, REQ_CODE);
        }else {
            beforeGetWifiName();
        }
    }
@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode){
            case REQ_CODE:
                if (grantResults.length > 0){
                    for (int result : grantResults){
                        if (result != PackageManager.PERMISSION_GRANTED){
                            Log.d(TAG, "必须同意所有权限才能使用本程序 !");
                            finish();
                            return;
                        }
                    }
                    beforeGetWifiName();
                }
                break;
        }
    }

2.2 AndroidP,开启GPS定位

    private boolean checkGpsIsOpen() {
        boolean isOpen;
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        isOpen = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        Log.d(TAG, "Gps is Open = " + isOpen);
        return isOpen;
    }

    private void beforeGetWifiName() {
        //Android9.0开始要开启GPS定位
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            if (checkGpsIsOpen()){
                getWifiName();
            }else {
                showGPSConfirmDlg();
            }
        } else {
            getWifiName();
        }
    }

    private void showGPSConfirmDlg() {
        DialogHelper dh = new DialogHelper(this);
        dialogDismiss();

        dlg = dh.bindHintDialog("HINT", "LOCATION", "YES", "NO", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogDismiss();
                        //跳转到手机原生设置页面
                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivityForResult(intent,GPS_REQUEST_CODE);
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogDismiss();
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                });
        dlg.setCanceledOnTouchOutside(false);
        dlg.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "onActivityResult: requestCode = " + requestCode);
        if (requestCode == GPS_REQUEST_CODE){
            beforeGetWifiName();
        }
    }

2.3 获取ssid

    private void getWifiName(){
        WifiManager wifiMgr = (WifiManager) getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifiMgr.getConnectionInfo();
        String ssid = info == null ? "" : info.getSSID();
        Log.d(TAG, "ssid = " + ssid);
        
        //Android8.0 要用以下方法,获取ssid
        if ("".equalsIgnoreCase(ssid)){
            ConnectivityManager ctm = (ConnectivityManager) getApplicationContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = ctm.getActiveNetworkInfo();
            ssid = networkInfo.getExtraInfo();
            Log.d(TAG, "ssid1 = " + ssid);
        }
    }

 

你可能感兴趣的:(android)