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:
- Scan for near by beacons
- Select beacons to save
- 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.