Altbeacon在android 8+上找不到信标

问题描述 投票:0回答:2

我在项目中使用Alt信标检测信标,它在我的Samsung s7(Android 7)上运行正常,但是在Samsung s10,Oneplus 6和Google Pixel 3 XL(Android 9/10)上却无法正常运行都可以。

该代码似乎没有找到任何信标。

我的代码以权限检查开头:

if (ContextCompat.checkSelfPermission(view, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    Log.d(TAG, "Requesting Permission.")
    openPermissionDialog()
} else {
    startBeacon()
}

然后打开我的起始信标代码蓝牙,并创建我的信标管理器

val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
    if(mBluetoothAdapter != null){
         if (!mBluetoothAdapter.isEnabled) {
                mBluetoothAdapter.enable()
         }

         Log.d(TAG, "Permissions accepted, bluetooth is on.")

         model.beaconManager = BeaconManager.getInstanceForApplication(view)
         BeaconController.startBeacon(model.beaconManager, view)
    }

几乎在BeaconController的内部,我有一个单例伴侣对象,它将每20分钟绑定一次信标。这是因为当我找到一个信标时,它将解除绑定。

class BeaconController {

    companion object {

        private val TAG = "HomePresenter"
        private var hasTimerStarted = false

        fun startBeacon(beaconManager: BeaconManager?, view: HomeActivityView) {

            if (!hasTimerStarted) {

                Log.d(TAG, "Timer starting")
                hasTimerStarted = true

                val handler = Handler()
                Thread(object : Runnable {
                    override fun run() {

                        if (beaconManager?.isBound(view) != null) {
                            Log.d(TAG, "Beacon binding")
                            beaconManager.bind(view)
                            beaconManager.beaconParsers.clear()
                            beaconManager.beaconParsers.add(BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT))
                        } else {
                            Log.d(TAG, "Beacon is bound")
                        }

                        handler.postDelayed(this, 1200000) // 20 Minutes

                    }
                }).start()

            } else {
                Log.d(TAG, "Timer already started.")
            }

        }

    }

}

一旦信标绑定,当我找到信标时,我会在onBeaconServiceConnect中运行一些逻辑以发送分析信息

  override fun onBeaconServiceConnect() {
        model.beaconManager?.addRangeNotifier { beacons, _ ->
            if (beacons.isNotEmpty()) {
                val firstBeacon = beacons.iterator().next()
                val firstBeaconNameSpace = "${firstBeacon.id1}".replace("0x", "")
                val firstBeaconIdentifier = "${firstBeacon.id2}".replace("0x", "")

                Log.d(TAG, "Beacon $firstBeaconNameSpace - $firstBeaconIdentifier is " + firstBeacon.distance + " meters away.")

            }
        }

        /**
         * Attach
         */

        try {
            model.beaconManager?.startRangingBeaconsInRegion(Region("com.MYAPP.MYAPP", null, null, null))
            Log.d(TAG, "Added Range Notifier")
        } catch (e: RemoteException) {
            Log.d(TAG, "Error adding Range Notifier")
            e.printStackTrace()
        }


    }
android kotlin altbeacon eddystone
2个回答
0
投票

该问题可能与Android Beacon库无关,仅与Android操作系统在Android 8+上使用计时器的限制有关。

[从版本8开始,除非执行了ForegroundService,否则在后台执行10分钟后,Android应用将由操作系统终止。因此,除非您一直保持屏幕开启,解锁并在屏幕上可见该应用程序,否则此计时器永远不会触发:handler.postDelayed(this, 1200000) // 20 Minutes

[您可以更改架构以使其在Android 8+上运行,也可以配置该库以使用其内置的ForegroundService来解决该限制。有关说明,请参见here


0
投票

[我知道,因为我在使用Beacons时遇到问题,所以对于运行Oreo并在后台执行任务的设备不起作用。

对于那些我使用JobScheduler的设备。...

这是关于它的教程https://www.youtube.com/watch?v=3EQWmME-hNA

我希望可以帮到您;)

祝你好运

© www.soinside.com 2019 - 2024. All rights reserved.