I recently ran into an interesting issue with the code for the FindTheMonkey iBeacon app. Previously I had the code for the app to act as an iBeacon using a CBPeripheralManager, which I created in the ViewDidLoad method. This previously worked fine but someone ran into an issue where ranging for the beacon never discovered it. To resolve this involved a couple small changes:
- Move the creation of the CBPeriheralManager into the constructor.
- Call the StartAdvertising method in ViewDidAppear.
peripheralDelegate = new BTPeripheralDelegate (); peripheralMgr = new CBPeripheralManager ( peripheralDelegate, DispatchQueue.DefaultGlobalQueue);
public override void ViewDidAppear (bool animated) { base.ViewDidAppear (animated); var power = new NSNumber (-59); peripheralData = beaconRegion.GetPeripheralData (power); peripheralMgr.StartAdvertising (peripheralData); }
With these changes in place, ranging for the beacon discovers it as it had before: