如果我使用API 27编译但是目标API 25,我正确地获得了这些值。如果我将targetSdkVersion设置为27,那么这些值将无法正确检索。
定位SDK 25或更低版本,值是正确的,但定位26或更多,我得到这些值:
SSID提供<unknown ssid>
BBSSID提供02:00:00:00:00:00
这些是我的清单权限,都是正常权限,不需要用户授权:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
这是示例代码:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
connectionInfo.getSSID();
connectionInfo.getBSSID();
针对SDK 26或更高版本时发生了哪些变化?我们还应该做些什么才能获得这些价值观?
由于
答案 0 :(得分:3)
您需要从API 27开始的位置许可
答案 1 :(得分:1)
Google已使用WifiManager更改了其政策,因为您可以从SDK&gt; = 27跟踪用户的位置跟踪wifi位置。因此,您需要访问位置权限才能使用getSSID()进行应用。 要使用此代码,请遵循以下内容。
正如@Samuel Eminet上面的回答,您需要ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION。我使用了ACCESS_FINE_LOCATION因为 ACCESS_COARSE_LOCATION请求仅限NETWORK_PROVIDE的权限 ACCESS_FINE_LOCATION请求NETWORK_PROVIDE和GPS_PROVIDER的权限。
Refer here for more information
在您的清单文件中
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
以及您的wifi管理员所在的位置,添加此代码以请求权限。
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
//Permission Not Granted
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_FINE_LOCATION);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_FINE_LOCATION:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission granted keep going status
Log.d(TAG, "PERMISSION GRANTED");
}
else {
Log.d(TAG, "PERMISSION DENIED");
}
}
}
答案 2 :(得分:0)
还有一件事需要记住(我学到了很多东西)。如果您将应用程序侧载到设备上,请不要忘记&#34;允许&#34;应用权限中的位置权限(长按应用,应用信息)。它默认是关闭的,因此你会得到
答案 3 :(得分:0)
我遇到了同样的问题。发生此问题的Android版本高于API 23。 解: 您需要在下面添加build.gradle文件。
implementation 'com.google.android.gms:play-services-location:15.0.0'
并且您拥有以下规则的许可。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
下面的代码自动为应用程序中的位置提供权限。
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
LocationRequest request = new LocationRequest()
.setFastestInterval(1500)
.setInterval(3000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
builder = new LocationSettingsRequest.Builder()
.addLocationRequest(request);
try {
Task<LocationSettingsResponse> result =
LocationServices.getSettingsClient(this.getApplicationContext()).checkLocationSettings(builder.build());
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
task.getResult(ApiException.class);
} catch (ApiException e) {
switch (e.getStatusCode()) {
case LocationSettingsStatusCodes
.RESOLUTION_REQUIRED:
ResolvableApiException resolveApiException = (ResolvableApiException) e;
try {
resolveApiException.startResolutionForResult(WifiConnectionActivity.this, REQUEST_CHECK_CODE);
} catch (IntentSender.SendIntentException ex) {
ex.printStackTrace();
} catch (ClassCastException ex) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
}
});
} catch (Exception x) {
}
checkPermissions();
} catch (Exception a) {
}
}
void checkPermissions() {
if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(WifiConnectionActivity.this, new String[]
{
Manifest.permission.ACCESS_COARSE_LOCATION
}, REQUEST_LOCATION);
}
else{
//Toast.makeText(getActivity(), "GPS is already permissioned", Toast.LENGTH_SHORT).show();
}
}
答案 4 :(得分:-2)
下面是代码。
public String getMac() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//Permission Not Granted
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}else{
WifiManager wifimanage = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiinfo = wifimanage.getConnectionInfo();
macAddress = wifiinfo.getBSSID();//Get the mac address of the currently connected network;
}
return macAddress;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission granted keep going status
Log.d("mac", "PERMISSION GRANTED");
WifiManager wifimanage = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiinfo = wifimanage.getConnectionInfo();
macAddress = wifiinfo.getBSSID();//Get the mac address of the currently connected network;
} else {
Log.d("mac", "PERMISSION DENIED");
}
}
}
然后调用getMac()解决此问题。