1

I have been trying to play around with BLE beacons for a while now and have run into some confusing problems.

What I want to achieve:

  1. Scan for near by beacons
  2. Select beacons to save
  3. Monitor when selected beacons are in/out of range.

Here is the code I use to scan for nearby beacons:

public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    if(peripheral.name != nil && peripheral.name!.contains("MkiBeacon")){
        print("\n Beacon   : \(String(describing: peripheral.name)) \(peripheral.identifier)
            \(peripheral.major):\(peripheral.minor)
            \(peripheral.beaconName) \(peripheral.beaconUUID) \(peripheral.deviceID) ")
    }
}

Here I get only peripheral name and identifier all other values like major,minor,UUID are undefined.

After that I try using identifier to monitor beacon region events:

        locationManager = CLLocationManager()
        locationManager?.delegate = self
        locationManager?.requestAlwaysAuthorization()
        let uuid = UUID(uuidString: peripheral.identifier)
        let major:CLBeaconMajorValue = 0
        let minor:CLBeaconMinorValue = 491
        let identifier = "myBeacon"
        let region = CLBeaconRegion(proximityUUID: uuid!, major:major,minor:minor,identifier: identifier)
        region.notifyOnEntry = true
        region.notifyEntryStateOnDisplay = true
        region.notifyOnExit = true
        locationManager?.startRangingBeacons(in: region)
        locationManager?.startMonitoring(for: region)

It seems that peripheral.identifier isn't the correct value it needs, as it doesn't work. It works when I ender the UUID from another beacon scanning app I use to check my beacons.

somerandomusername
  • 1,993
  • 4
  • 23
  • 55
  • 1
    You don't use `CBCentralManager` for beacons. You use `CLLocationManager` and `CLBeaconRegion`. You need to know at least the UUID you are interested in. There is no way to scan for all beacons regardless of UUID. Some beacons ma advertise a BLE service for configuration but this is non-standard. There is no relationship between the peripheral identifier and the beacon UUID – Paulw11 Nov 05 '19 at 10:02
  • Hmm. But I definitely get a list of near by beacons using CBCentralManager. – somerandomusername Nov 06 '19 at 08:28
  • 1
    You probably get the BLE service that that particular type of beacon advertises for configuration. Regardless there is no correlation between that identifier and the configured UUID. You need to know the UUID you are interested in – Paulw11 Nov 06 '19 at 08:30

0 Answers0