Altbeacon停止在Android10上工作,并且didEnterRegion根本没有被调用 [英] Altbeacon stopped working on Android10 and didEnterRegion does not get called at all

查看:103
本文介绍了Altbeacon停止在Android10上工作,并且didEnterRegion根本没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

altbeacon 只是停止工作了.尤其是转到Android 10时是否需要进行任何更改?

altbeacon on Android 10 has simply stopped working. Are there any changes required especially to go to Android 10?

我已经为我的AndroidManifest.xml

<uses-permission android:name=\"android.permission.BLUETOOTH_ADMIN\"/>
<uses-permission android:name=\"android.permission.BLUETOOTH\"/>
<uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\"/>
<uses-permission android:name=\"android.permission.ACCESS_BACKGROUND_LOCATION\"/>
<uses-permission android:name=\"android.permission.RECEIVE_BOOT_COMPLETED\" />
<uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\"/>

查看应用程序权限,我的应用程序在运行时具有位置和BT权限.我开始按照 https://altbeacon的规定在onBeaconServiceConnect上寻找信标. github.io/android-beacon-library/samples.html .我想念什么?

Looking at the app permissions, my app has the location and BT permissions granted while running. I start looking for beacons on onBeaconServiceConnect as prescribed at https://altbeacon.github.io/android-beacon-library/samples.html. What am I missing?

推荐答案

Android 10添加了新的权限要求以进行BLE扫描并检测BLE信标-任何设置为targetSdkVersion 29或更高级别的应用都需要如下所述获取权限,否则将不会检测到信标. (如果设置了targetSdkVersion 28或更早版本,则这些新要求在Android 10上运行时将不适用,因为Android会自动授予权限.这使旧应用在升级到Android 10后仍能继续运行而不会受到影响.)

Android 10 adds new permissions requirements to do BLE scans and detect BLE beacons -- any app setting targetSdkVersion 29 or higher needs to obtain permissions as described below or no beacons will be detected. (If you set targetSdkVersion 28 or earlier, these new requirements do not apply when running on Android 10, as Android grants the permissions automatically. This allows old apps to continue running unaffected after upgrading to Android 10.)

如果将项目设置为targetSdkVersion 29或更高版本,则需要进行以下更改:

The changes below are what you need to to if you set your project to targetSdkVersion 29 or higher:

除了将以下权限添加到清单中:

In addition to adding these permissions to the manifest:

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

您还必须按照以下说明从用户动态获取位置权限:

You must also dynamically obtain the location permissions from the user as described here:

private static final int PERMISSION_REQUEST_FINE_LOCATION = 1;
    private static final int PERMISSION_REQUEST_BACKGROUND_LOCATION = 2;
...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ...


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                if (this.checkSelfPermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    if (this.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
                        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setTitle("This app needs background location access");
                        builder.setMessage("Please grant location access so this app can detect beacons in the background.");
                        builder.setPositiveButton(android.R.string.ok, null);
                        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                            @TargetApi(23)
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                                requestPermissions(new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},
                                        PERMISSION_REQUEST_BACKGROUND_LOCATION);
                            }

                        });
                        builder.show();
                    }
                    else {
                        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setTitle("Functionality limited");
                        builder.setMessage("Since background location access has not been granted, this app will not be able to discover beacons in the background.  Please go to Settings -> Applications -> Permissions and grant background location access to this app.");
                        builder.setPositiveButton(android.R.string.ok, null);
                        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                            @Override
                            public void onDismiss(DialogInterface dialog) {
                            }

                        });
                        builder.show();
                    }

                }
            } else {
                if (this.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                                    Manifest.permission.ACCESS_BACKGROUND_LOCATION},
                            PERMISSION_REQUEST_FINE_LOCATION);
                }
                else {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Functionality limited");
                    builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons.  Please go to Settings -> Applications -> Permissions and grant location access to this app.");
                    builder.setPositiveButton(android.R.string.ok, null);
                    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                        @Override
                        public void onDismiss(DialogInterface dialog) {
                        }

                    });
                    builder.show();
                }

            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_FINE_LOCATION: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "fine location permission granted");
                } else {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Functionality limited");
                    builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons.");
                    builder.setPositiveButton(android.R.string.ok, null);
                    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                        @Override
                        public void onDismiss(DialogInterface dialog) {
                        }

                    });
                    builder.show();
                }
                return;
            }
            case PERMISSION_REQUEST_BACKGROUND_LOCATION: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "background location permission granted");
                } else {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Functionality limited");
                    builder.setMessage("Since background location access has not been granted, this app will not be able to discover beacons when in the background.");
                    builder.setPositiveButton(android.R.string.ok, null);
                    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {

                        @Override
                        public void onDismiss(DialogInterface dialog) {
                        }

                    });
                    builder.show();
                }
                return;
            }
        }
    }

如果您认为此方法不起作用,请转到设置"->应用程序"->您的应用程序"->权限",并确认已授予位置.

If you do not see this working, go to Settings -> Apps -> Your App -> Permissions and verify location had been granted.

这篇关于Altbeacon停止在Android10上工作,并且didEnterRegion根本没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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