在Android 6中访问WIFI的权限

时间:2016-10-17 04:59:23

标签: android android-studio android-6.0-marshmallow android-permissions

我是Android工作室的新手,我以前在我的应用程序上使用Android版本5.1它工作正常但现在在6我无法获得许可或我不确定我的方法是否错误。我正在创建一个应用程序,它使用我的手机可用的访问点列表。但即使我被授予权限,我的应用程序也没有向我显示wifi列表,但相同的代码适用于早期版本。

void
updatePoint(int *arg)
{
    int distance;

    distance = findDistance(arg[0], arg[1], 0, -1);
    if (distance >= 1)
        goto noset1;
    if (arg[2] >= 0)
        goto noset1;
    arg[2] = -arg[2];
noset1:

    distance = findDistance(arg[0], arg[1], 10, -1);
    if (distance >= 1)
        goto noset2;
    if (arg[2] <= 0)
        goto noset2;
    arg[2] = -arg[2];
noset2:

    distance = findDistance(arg[0], arg[1], -1, 0);
    if (distance >= 1)
        goto noset3;
    if (arg[3] >= 0)
        goto noset3;
    arg[3] = -arg[3];
noset3:

    distance = findDistance(arg[0], arg[1], -1, 10);
    if (distance >= 1)
        goto noset4;
    if (arg[3] <= 0)
        goto noset4;
    arg[3] = -arg[3];
noset4:

    arg[0] = arg[0] + arg[2];
    arg[1] = arg[1] + arg[3];
}

清单:

Mainactivity:

package com.wiferange.wifi_test;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;

public class MainActivity extends AppCompatActivity {
ListView lv;
String lists[]={"a","b"};
WifiManager wifi;
String wifis[];
WifiScanReceiver wifiReciever;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    lv = (ListView)findViewById(R.id.listView);
    wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
    wifiReciever = new WifiScanReceiver();

    getPermission();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                        }
    });

}

protected void onPause() {
    unregisterReceiver(wifiReciever);
    super.onPause();
}

protected void onResume() {
    registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    super.onResume();
}
private void getPermission() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED){
        requestPermissions(new String[]{Manifest.permission.ACCESS_WIFI_STATE},0x12345);
       // lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,lists));
        //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
    }else{
        wifi.startScan();

        //do scanning, permission was previously granted; or legacy device
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { //funcitno executes when some permission was granted
    if (requestCode == 0x12345) {
        for (int grantResult : grantResults) {
            if (grantResult != PackageManager.PERMISSION_GRANTED) { //check if permission was already grnated and start scannig if yes
                wifi .startScan() ;
                return;
            }
        }
        getPermission(); //ask for permission if not given
    }
}

private class WifiScanReceiver extends BroadcastReceiver {
    public void onReceive(Context c, Intent intent) {
        List<ScanResult> wifiScanList = wifi.getScanResults();
        wifis = new String[wifiScanList.size()];

        for(int i = 0; i < wifiScanList.size(); i++){
            wifis[i] = ((wifiScanList.get(i)).toString());
        }

        lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,wifis));

    }
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

请帮我解决这个问题。提前致谢 !!!!

3 个答案:

答案 0 :(得分:7)

您正在请求错误的运行时权限。根据{{​​3}}:

  

要通过蓝牙和Wi-Fi扫描访问附近外部设备的硬件标识符,您的应用现在必须具有Android 6.0 changesACCESS_FINE_LOCATION权限

您必须请求并获得其中一个运行时权限才能执行wifi扫描。

答案 1 :(得分:0)

如果权限位于自动授予权限列表或“危险”权限列表中,则需要查看权限。如果那是在危险许可列表中,那么你需要像android M这样的运行时权限。 只是通过这个link您正在请求错误的运行时权限。 ACCESS_WIFI_STATE权限自动被授予权限,无需访问它运行时。

答案 2 :(得分:0)

  

似乎为了在扫描时与WifiManager一起使用   来自Android 6.0的连接需要访问您的位置,以便   无论是优越的位置还是粗糙的位置,我补充说   跟随我的清单文件:   

我认为这就是你所需要的 Application (WiFi connections) doesn't work anymore on Android 6.0 Marshmallow