Search code examples
androidkotlinbluetooth-lowenergybeaconaltbeacon

Getting UUID of nearby beacon


I have an application that i want to scan nearby beacons and get their RSSI , UUID , major and minor. to test my code i used to make a virtual beacon using Beacon Simulator app on another device. I checked several ways but none of them worked fine:

1) in this code i made a scanner class and starting scan in my fragment and get address(I think it is mac address of BLE device) and RSSI but when i want to get UUID it says that it is null

private val mLeScanCallback = BluetoothAdapter.LeScanCallback { device, rssi, scanRecord ->
    if (rssi > signalStrength) {
        mHandler.post{
            val uuid : String
            if(device.uuids != null){
                uuid = device.uuids[0].uuid.toString()
            }
            else{
                uuid = "nullll"
                scanRecord
            }
            Log.i("scan" , "device founded -> address:" + device.address + " name: " + device.name +" uuid: " + uuid + " RSSI: " + rssi + " type: " + device.type)
        }
    }
}

and call this function in my fragment to do the scan:

private fun scanLeDevice(enable: Boolean) {
    if (enable && !isScanning) {
        Log.i("scan" , "starting scan ...")

        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed({
            Log.i("scan" , "stopping scan ...")

            isScanning = false
            mBluetoothAdapter.stopLeScan(mLeScanCallback)

            stopScan()
        }, scanPeriod)

        isScanning = true
        mBluetoothAdapter.startLeScan(mLeScanCallback)
    } else {
        isScanning = false
        mBluetoothAdapter.stopLeScan(mLeScanCallback)
    }
}

2)second way i checked was to use this function in my fragment but nothing happened and no beacon was detected:

private val beaconManager = BeaconManager.getInstanceForApplication(MainApplication.applicationContext())



override fun onBeaconServiceConnect() {
    beaconManager.removeAllRangeNotifiers()
    beaconManager.addRangeNotifier(object : RangeNotifier {
        override fun didRangeBeaconsInRegion(beacons: Collection<Beacon>, region: Region) {
            if (beacons.size > 0) {
                Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.")
            }
        }
    })

    try {
        beaconManager.startRangingBeaconsInRegion(Region("myRangingUniqueId", null, null, null))
    } catch (e: RemoteException) {
    }

}

I really dont know what is wrong ...


Solution

  • For the second listing using the Android Beacon Library, the problem is that you never call beaconManager.bind(this) and as a result, you never get a callback to onBeaconServiceConnect and none of the code inside that method executes.

    Since you are using a Fragment not an Activity, take care that you actually implement all the methods of BeaconConsumer, which the class with the onBeaconServiceConnect method must implement. See here for more info on that chaining.

    Finally, if you are looking for iBeacon transmitters you must set a beacon layout. The java code to do this is beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));