Search code examples
androidbluetooth-lowenergyaltbeacon

Detecting Beacon Using android-beacon-library not working


I have a beacon I'm trying to use with Android Beacon Library that I do not know much about.

Here are the details for nRF Connect: details

Basically, none of the methods inside onBeaconServiceConnect are being called except for the didDetermineStateForRegion override that seems to run once at app startup.

I have also granted my app Location permissions.

Application code that is running and registered in manifest.

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"
    }
}

And "MyBeaconParser"

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

Solution

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

    Understand that the numeric offsets in the "m" beacon layout are measured from the manufacturer advertisement type byte indicator "ff". So if your advertisement bytes look like this:

    06 1b ff 51 05 03 d8 ...

    The bytes 51 05 are at offsets 0 and 1 (these are the two byte hardware manufacturer code) followed by 03 d8 at offsets 2 and 3.

    The other terms of your beacon parser expression i and p will set the identifier and measured power to start at byte offsets 0 and byte offset 1, respectively. This will result in your identifier always being set to the manufacturer code of 0x0551 and the measured power to 0x51. This is probably not what you want. Setting these terms correctly require figuring out where (if anywhere) your beacon format has a useful identifier in the byte pattern, and where it has a measured power byte (if it has one at all).