在android 8(oreo)上使用altbeacon库,后台信标扫描不起作用 [英] using altbeacon library on android 8(oreo), background beacon scan not working

查看:219
本文介绍了在android 8(oreo)上使用altbeacon库,后台信标扫描不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用Altbeacon库进行信标扫描.

My app using Altbeacon library for beacon scanning.

我的代码在api级别24(android 7)上运行良好,但在oreo(8.0.0)上却无法正常工作

my code working well to api level 24(android 7) but doesn't not working on oreo(8.0.0)

首先,我的gradle设置compileSdkVersion和targetSdkVersion为26,我认为是因为这个原因.

at first, my gradle setting compileSdkVersion and targetSdkVersion is 26, i thought it was because of this.

因此,我引用了这个** https://developer. android.com/about/versions/oreo/background.html#services **, 修复我的gradle设置(compileSdkVersion 25,targetSdkVersion 25).

so, i reference this**https://developer.android.com/about/versions/oreo/background.html#services**, fix my gradle setting(compileSdkVersion 25, targetSdkVersion 25).

并在设备上安装此应用(API级别24、26) 在24级设备上工作正常,但在26级设备上工作不正常.

and install this app on device (apilevel 24, 26) on 24 level device working well, but 26 is not.

那是我的代码.

Activity.onCreate

    Intent intent = new Intent(this, BeaconService.class);
    startService(intent);

BeaconService (Altbeacon库)

in BeaconService(Altbeacon Library)

@Override
public void onBeaconServiceConnect()
{
    final Handler h = new Handler(getApplicationContext().getMainLooper());
    Beacon[] tempBeacon = new Beacon[2];

    bm.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(final Collection beacons, final Region region) {

            if (beacons.size() > 0) {

                //call method this context

            }
            Log.d("beacon", "detect");
        }
    });

    try
    {
            bm.startRangingBeaconsInRegion(new Region("test", Identifier.parse("A1200001-EABC-9876-D001-E00000000001"), Identifier.parse("32001"), null));
    } catch (RemoteException e) {
    }
}

可以检测到信标,但是需要很长时间.

beacon is detected, but it takes a very long time.

我在下面看到了日志

03-15 18:25:02.639 2419-2419/app.eco.inulibrary D/ScanJobScheduler:计划ScanJob(job:208352940/app.eco.inulibrary/org.altbeacon.beacon.service.ScanJob)运行每个310000毫 03月15日15:25:02.917 2419-2419/app.eco.inulibrary D/ScanJob调度程序:调度ScanJob(job:208352940/app.eco.inulibrary/org.altbeacon.beacon.service.ScanJob)每300000毫秒运行一次 03月15日15:36:00.176 2419-2419/app.eco.inulibrary I/ScanJob:运行定期扫描作业:实例为org.altbeacon.beacon.service.ScanJob@a6ca148 03月15日15:36:01.751 2419-3951/app.eco.inulibrary D/RangeState:将id1:a1200001-eabc-9876-d001-e00000000001 id2:32001 id3:2001添加到org.altbeacon.beacon的现有范围. service.RangedBeacon@bf2cb74 03月15日15:36:01.968 2419-2419/app.eco.inulibrary D/回调:尝试通过本地广播意图进行回调:org.altbeacon.beacon.range_notification

03-15 18:25:02.639 2419-2419/app.eco.inulibrary D/ScanJobScheduler: Scheduling ScanJob (job:208352940/app.eco.inulibrary/org.altbeacon.beacon.service.ScanJob) to run every 310000 millis 03-15 18:25:02.917 2419-2419/app.eco.inulibrary D/ScanJobScheduler: Scheduling ScanJob (job:208352940/app.eco.inulibrary/org.altbeacon.beacon.service.ScanJob) to run every 300000 millis 03-15 18:36:00.176 2419-2419/app.eco.inulibrary I/ScanJob: Running periodic scan job: instance is org.altbeacon.beacon.service.ScanJob@a6ca148 03-15 18:36:01.751 2419-3951/app.eco.inulibrary D/RangeState: adding id1: a1200001-eabc-9876-d001-e00000000001 id2: 32001 id3: 2001 to existing range for: org.altbeacon.beacon.service.RangedBeacon@bf2cb74 03-15 18:36:01.968 2419-2419/app.eco.inulibrary D/Callback: attempting callback via local broadcast intent: org.altbeacon.beacon.range_notification

如何解决这个问题?

推荐答案

Android O引入了对后台服务的新限制.该应用程序切换到后台模式后不久,代码中显示的BeaconService之类的服务将被操作系统杀死.

Android O introduces new limits on Background services. Services like the BeaconService shown in the code will be killed by the operating system shortly after the app switches to background mode.

AndroidBeaconLibrary已更新,以解决此问题,方法是使用Android作业调度程序处理Android 8+上的信标扫描.但这要起作用,您必须使用BeaconManagerRegionBootsrap类开始扫描,而不是直接使用Intent启动BeaconService.这样,库就知道可以在Android 8+和早期版本的BeaconService上使用作业调度程序.

The AndroidBeaconLibrary has been updated to account for this by using the Android job scheduler to handle beacon scanning on Android 8+. But for this to work, you must use the BeaconManager or RegionBootsrap classes to start scanning, rather than starting the BeaconService directly with an Intent. This way, the library will know to use the job scheduler on Android 8+ and the BeaconService on earlier versions.

此外,您将需要移动代码以开始扫描并将初始化范围从Activity移到自定义android.app.Application类中.这是因为Android 8在后台运行时会杀死您的Activity和该应用程序,并且您需要在Android组件中设置范围,当在后台重新启动该应用程序时,该组件会自动创建扫描.

Further, you will need to move your code that starts scanning and initializes ranging out of an Activity and into a custom android.app.Application class. This is because Android 8 will kill your Activity along with the app when it is in the background, and you will need to have your ranging set up in an Android component that is created automatically when the app is re-launched in the background for periodic scanning.

我建议您按照示例代码

I recommend you rework your setup be as described in Starting an App in the Background in the sample code here. That sample only sets up monitoring, but you can start ranging in the didDetermineStateForRegion callback shown.

最后,请确保您拥有库版本2.13+,该版本完全支持Android 8.1中的更改​​.

Finally, make sure you have library version 2.13+ which has full support for changes in Android 8.1.

要了解有关Android 8中背景信标检测如何变化的更多信息,请参阅我的博客文章

To understand more about how background beacon detection has changed in Android 8, see my blog post here.

这篇关于在android 8(oreo)上使用altbeacon库,后台信标扫描不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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