android - 我是否应该以编程方式打开 Wifi 和蓝牙扫描以获得更好的定位精度?

标签 android

我目前正在制作一个跟踪用户移动的应用程序。基本上,它需要用户以米为单位移动给定的距离。而且由于用户可以在室内移动,所以我非常需要高精度。我正在使用 FusedLocationProviderClient 来完成我的工作。

当应用程序启动时,它会检查位置设置以查看它们是否都令人满意。如果不是,它会提示一个对话框,允许用户仅点击 OK,然后所有位置设置都将正确设置。

我必须使用 LocationSettingsRequest.BuilderResolvableApiException 类来做到这一点。

LocationSettingsRequest.Builder 类获取一个 LocationRequest 对象作为输入,然后检查设备中的位置设置是否满足给定的 LocationRequest对象。如果不满足设备的位置设置,这样做将抛出一个 ResolvableApiException 对象。并且 ResolvableApiException 对象可以提示一个对话框,允许用户正确进行这些设置。更多详情,请访问 Change location settings在 developer.android.com 上

LocationSettingsRequest.Builder 需要一个 LocationRequest 作为输入,所以这是我的 LocationRequest:

LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(2000);

我在 MainActivity 中检查位置设置的代码:

public void checkLocationSettings(){
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(locationRequest);
    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    Task<LocationSettingsResponse> task = settingsClient.checkLocationSettings(builder.build());
    task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            // do works
        }
    });
    task.addOnFailureListener(this, new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            try {
                ResolvableApiException resolvable = (ResolvableApiException) e;
                resolvable.startResolutionForResult(MainActivity.this, 1);
            } catch (IntentSender.SendIntentException e1) {
                e1.printStackTrace();
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    switch (requestCode){
        case 1:
            if(resultCode== RESULT_OK){
                // do works
            }
            else{
                // I will automatically terminate the app in this case
            }
            break;
    }
}

我使用的是 Android 操作系统版本 8.1.0 的 Samsung Galaxy J7 Pro。 Location设置中有Locating methodImprove accuracy两个选项。 Locating method 有 3 个选项,分别是 High accuracyBattery savingPhone onlyImprove accuracy有2个选项,分别是Wi-Fi scanningBluetooth scanning

我尝试关闭设备位置,关闭 提高准确性 中的所有 2 个选项,并将 定位方法 设置为 仅电话。然后我打开我的应用程序,出现了这样一个对话框: enter image description here

忘掉用户单击NO THANKS 时的情况。当用户单击 OK 时,我可以看到 Location 服务已被激活。 定位方法也设置为高精度。但是 Improve accuracy 选项没有变化。

这让我很奇怪。我想知道当 Locating method 设置为 High accuracy 时,Improve accuracy 是否变得无用。或者它根本不是无用的,我也必须以编程方式打开它们。

很抱歉,我不能发布我的所有代码,因为它对于我的问题来说非常复杂和多余。但如果你愿意,我可以创建一个全新的项目来演示这个问题。而且我不知道这是否也是提出此类问题的好地方。如果不是,你能给我看一个吗?我很感激任何想法或解决方案。

编辑:所以我创建了一个新的简单项目来演示这个问题。可以查一下here .

最佳答案

根据扫描设置说明,即使用户关闭了wifi和蓝牙,wifi和蓝牙扫描也可以工作。因此无需手动打开蓝牙/wifi。

根据 documentation ,设置 builder.setNeedBle(true); 应该在用户禁用蓝牙时提示用户启用蓝牙扫描。

关于android - 我是否应该以编程方式打开 Wifi 和蓝牙扫描以获得更好的定位精度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56216357/

相关文章:

android - 如何在一个应用程序中设置两个不同的启动器名称和图标 (android)

android - RxJava : Know when observable is finished and return Boolean Observable to the caller

java - Android音频不会循环播放

java - Android - 如何将 ArrayList 逐行写入文本文件并读回?

java - 为什么不添加文件而不是覆盖文件?

android - phonestateintentreceiver.notifyPhoneCallState 有什么用?

android - 已连接设备列表中的闪烁选项不允许我调试任何应用程序

java - Android 上的简单 XML - 类 MyArrayList<E> 扩展了 ArrayList<E>?

android - 加解密资源库

java.lang.RuntimeException : Failure delivering result ResultInfo requires android. 权限。READ_EXTERNAL_STORAGE 或 grantUriPermissio