甚至在授予某些设备上的所有必需权限后,调用WifiManager.startLocalOnlyHotspot()也会引发SecurityException [英] Calling WifiManager.startLocalOnlyHotspot() throws SecurityException even after granting all required permissions on some devices

查看:318
本文介绍了甚至在授予某些设备上的所有必需权限后,调用WifiManager.startLocalOnlyHotspot()也会引发SecurityException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个文件共享应用程序.我需要通过调用WifiManager.startLocalOnlyHotspot()以编程方式打开设备的仅本地热点.

I am working on a file sharing app. I need to turn on local only hotspot of a device programmatically by calling WifiManager.startLocalOnlyHotspot().

根据此页面上的android文档-

According to the android docs on this page - https://developer.android.com/reference/android/net/wifi/WifiManager#startLocalOnlyHotspot(android.net.wifi.WifiManager.LocalOnlyHotspotCallback,%2520android.os.Handler),

应用程序需要具有以下权限才能启动LocalOnlyHotspot:Manifest.permission.CHANGE_WIFI_STATE和ACCESS_FINE_LOCATION.没有权限的调用者将触发SecurityException.

Applications need to have the following permissions to start LocalOnlyHotspot: Manifest.permission.CHANGE_WIFI_STATE and ACCESS_FINE_LOCATION. Callers without the permissions will trigger a SecurityException.

我已在清单中添加了这些权限,并在运行时授予了ACCESS_FINE_LOCATION(因为这是运行时权限(危险权限)).

I have added both these permissions in my manifest and also granted ACCESS_FINE_LOCATION at runtime (coz it's a runtime permission (dangerous permission)).

但是在某些设备中调用startLocalOnlyHotspot()仍会引发SecurityException.
引发SecurityException的设备:Samsung Galaxy J7 Max(j7maxlte),Android 8.1 运行正常且不会引发异常的设备:Redmi Note 7 Pro,Android 9 PKQ1.181203.001

But calling startLocalOnlyHotspot() still throws SecurityException in some devices.
Device in which SecurityException is thrown: Samsung Galaxy J7 Max (j7maxlte), Android 8.1 Device in which it works fine without throwing an exception: Redmi Note 7 Pro, Android 9 PKQ1.181203.001

我想念什么?

推荐答案

您需要使用GPS启用位置.

You need to enable location using GPS.

首先,添加gms的依赖项.

First of all, add the dependency for gms.

implementation 'com.google.android.gms:play-services-location:16.0.0'

然后,设置位置服务

  private void setupLocationServices() {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10);
    mLocationRequest.setSmallestDisplacement(10);
    mLocationRequest.setFastestInterval(10);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    LocationSettingsRequest.Builder builder = new
        LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);

    task = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());

    locationSettingsResponseBuilder();
  }

LocationSettingsResponseBuilder:

  private void locationSettingsResponseBuilder() {
    task.addOnCompleteListener(task -> {
      try {
        LocationSettingsResponse response = task.getResult(ApiException.class);
        // All location settings are satisfied. The client can initialize location
        // requests here.

        //Everything is good. You can turn on hotspot here.

        //}
      } catch (ApiException exception) {
        switch (exception.getStatusCode()) {
          case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
            // Location settings are not satisfied. But could be fixed by showing the
            // user a dialog.
            try {
              // Cast to a resolvable exception.
              ResolvableApiException resolvable = (ResolvableApiException) exception;
              // Show the dialog by calling startResolutionForResult(),
              // and check the result in onActivityResult().
              resolvable.startResolutionForResult(
                  MainActivity.this,
                  101);
            } catch (IntentSender.SendIntentException e) {
              // Ignore the error.
            } catch (ClassCastException e) {
              // Ignore, should be an impossible error.
            }
            break;
          case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
            // Location settings are not satisfied. However, we have no way to fix the
            // settings so we won't show the dialog.
            break;
        }
      }
    });
  }

将此案例添加到 onActivityResult 方法.

      case 101:
        final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
        switch (resultCode) {
          case Activity.RESULT_OK:
            // All required changes were successfully made
            Log.v("case 101", states.isLocationPresent() + "");

            //Everything is good. You can turn on hotspot here.

            break;
          case Activity.RESULT_CANCELED:
            // The user was asked to change settings, but chose not to
            Log.v("case 101", "Canceled");
            break;
          default:
            break;
        }
        break;

我最近创建了一个名为 Spotserve 的演示应用.这会为所有API> = 15的设备打开wifi热点,并在该热点上托管一个演示服务器.您可以检查以获取更多详细信息.希望这会有所帮助!

I recently created a demo app called Spotserve. That turns on wifi hotspot for all devices with API>=15 and hosts a demo server on that hotspot. You can check that for more details. Hope this helps!

这篇关于甚至在授予某些设备上的所有必需权限后,调用WifiManager.startLocalOnlyHotspot()也会引发SecurityException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆