使用 android-beacon-library 检测 Beacon 不工作

Detecting Beacon Using android-beacon-library not working

我有一个信标,我正尝试与 Android 信标库一起使用,但我不太了解。

以下是 nRF Connect 的详细信息:

基本上,onBeaconServiceConnect 中的 none 方法被调用,除了 didDetermineStateForRegion 覆盖似乎 运行 在应用程序启动时被调用一次。

我也已授予我的应用位置权限。

运行正在清单中注册并注册的应用程序代码。

class MyApplicationName : Application(), BeaconConsumer  {
    var beaconManager : BeaconManager? = null

    override fun onCreate() {
        super.onCreate()
        Log.d(TAG, "App started up")
        beaconManager = BeaconManager.getInstanceForApplication(this)
        beaconManager?.beaconParsers?.add(MyBeaconParser())
        beaconManager?.bind(this)
    }

    override fun onBeaconServiceConnect() {
        beaconManager?.removeAllMonitorNotifiers()
        beaconManager?.addMonitorNotifier(object : MonitorNotifier {
            override fun didEnterRegion(region: Region) {
                Log.i(TAG, "I just saw an beacon for the first time!")
            }

            override fun didExitRegion(region: Region) {
                Log.i(TAG, "I no longer see an beacon")
            }

            override fun didDetermineStateForRegion(state: Int, region: Region) {
                Log.i(TAG, "I have just switched from seeing/not seeing beacons: $state")
            }
        })

        try {
            beaconManager?.startMonitoringBeaconsInRegion(Region("myMonitoringUniqueId", null, null, null))
        } catch (e: RemoteException) {
        }

    }

    companion object {
        private val TAG = ".MyApplicationName"
    }
}

和"MyBeaconParser"

class MyBeaconParser : BeaconParser() {
    init {
        mHardwareAssistManufacturers = intArrayOf(0x0551)
        this.setBeaconLayout("m:0-1=03d8,i:0-1,p:1-1")
        this.mIdentifier = "sylerobeacon"
    }
}

尝试:setBeaconLayout("m:2-3=03d8,i:0-1,p:1-1")

了解 "m" 信标布局中的数字偏移量是根据制造商广告类型字节指示符 "ff" 测量的。因此,如果您的广告字节如下所示:

06 1b ff 51 05 03 d8 ...

字节 51 05 位于偏移量 0 和 1(这些是两个字节的硬件制造商代码)后跟偏移量 2 和 3 处的 03 d8。

信标解析器表达式 i 和 p 的其他项将设置标识符和测量功率,分别从字节偏移量 0 和字节偏移量 1 开始。这将导致您的标识符始终设置为制造商代码 0x0551,并将测量功率设置为 0x51。这可能不是您想要的。正确设置这些术语需要弄清楚您的信标格式在何处(如果有的话)在字节模式中具有有用的标识符,以及在何处具有测量功率字节(如果它有的话)。