iBeasons - does not feel stable

wallymwallym USInsider, Beta ✭✭✭

I'm using the following code to test out iBeacons. I have it working, but it does not seem to always work right. Here are my questions/thoughts. Feel free to throw out any suggestions/ideas:

  1. If I understand the code properly, the code will broadcast a uuid with an identifier (via StartAdvertising(), and then look for the same identifier (via the location manager RegionEntered). If it is advertising and looking for the same value, won't it always find the beacon? Or at least it should and it should result in the "You're red hot!" being written to the console? Am I missing something on this?

  2. Sometimes the code work and sometimes it does not. When it does work, the general sequence is a few entries of "red hot", a few entries of "I can't tell", then it seems to stop. Any idea why it will fade out like this?

  3. When the DidRangeBeacons fires, it will sometimes not return any beacons. I found that I had to check the length or I would get an error about a null reference exception. Any ideas on this?

  4. On my iPhone 5, the bluetooth always seems to be powered off, even when it is on. I had to put in a while loop to test of the state of the bluetooth peripharel manager. This seems rather hokey. Is there any event that I can look for in the delegate to look for? Suggestions are welcome.

Overall, any ideas for improvement are welcome. Thanks.

        locationManager = new CLLocationManager();
        var beaconId = new NSUuid("E437C1AF-36CE-4BBC-BBE2-6CE802977C46");
        var beaconRegion = new CLBeaconRegion(beaconId, "My Beacon");
        locationManager.RegionEntered += (s, e) => {
            if(e.Region.Identifier == "My Beacon")
            {
                Console.WriteLine("Found My Beacon");
                //Fire up ranging
                locationManager.StartRangingBeacons(beaconRegion);
                locationManager.DidRangeBeacons += (lm, rangeEvents) => {
                    if ( rangeEvents.Beacons.Length > 0 )
                    {
                        switch(rangeEvents.Beacons[0].Proximity)
                        {
                        case CLProximity.Far:
                            Console.WriteLine("You're getting colder!");
                            break;
                        case CLProximity.Near:
                            Console.WriteLine("You're getting warmer!");
                            break;
                        case CLProximity.Immediate:
                            Console.WriteLine("You're red hot!");
                            break;
                        case CLProximity.Unknown:
                            Console.WriteLine("I can't tell");
                            break;
                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                };
            }
        };
        locationManager.StartMonitoring(beaconRegion);

//Create a beacon
var peripheralManager = new CBPeripheralManager(new MyPeripheralDelegate(),
DispatchQueue.DefaultGlobalQueue, new NSDictionary());
var beaconOptions = beaconRegion.GetPeripheralData(null);
while (peripheralManager.State != CBPeripheralManagerState.PoweredOn) {
System.Threading.Thread.Sleep (50);
}
peripheralManager.StartAdvertising(beaconOptions);

Sign In or Register to comment.