private static async void RangingBeaconsInRegion(object sender, RangeEventArgs e) { //_data.Clear (); //var allBeacons = new List<Beacon> (); newBeacons.Clear (); foreach (var b in e.Beacons) { //allBeacons.Add (b); //System.Diagnostics.Debug.WriteLine (string.Format ("Beacon: {0} / {1} - {2}.{3} = {4:N0} cm", b.BluetoothName, b.Id1, b.Id2, b.Id3, b.Distance * 100)); string id = string.Format ("{0}.{1}", b.Id2, b.Id3); OneBeacon ob = new OneBeacon (); ob.Id = id; ob.Distance = Convert.ToInt64 (b.Distance * 100.0); // par rapport à iOS // on a pas la proximity immediate/near/far // on simule en fonction de la distance if (ob.Distance < 30) ob.Proximity = 1; else if (ob.Distance < 100) ob.Proximity = 5; else ob.Proximity = 10; newBeacons.Add (id, ob); } if (newBeacons.Count > 0) { List<OneBeacon> list = new List<OneBeacon> (); foreach (KeyValuePair<string,OneBeacon> kvp in newBeacons) { list.Add (kvp.Value); } // on envoi l'info Founded (list); } }
static void HandleDidRangeBeacons(object sender, CLRegionBeaconsRangedEventArgs e) { // if (!e.Region.Identifier.Equals (regionName)) { // Console.WriteLine (string.Format ("Found Region: {0}", e.Region.Identifier)); // return; // } newBeacons.Clear (); foreach (CLBeacon b in e.Beacons) { if (b.Proximity == CLProximity.Unknown) continue; try { // on extrait la distance en metre, que l'on converti en cm string s = b.ToString (); int start = s.IndexOf ("+/-"); start += 3; int end = s.IndexOf ("m,", start); string val = s.Substring (start, end - start); long v = 0; try { double tmp1 = Convert.ToDouble (val, CultureInfo.InvariantCulture); double tmp2 = tmp1 * 100; v = Convert.ToInt64 (tmp2, CultureInfo.InvariantCulture); } catch (Exception) { v = Convert.ToInt64 (Convert.ToDouble (val.Replace (".", ",")) * 100); } string id = string.Format ("{0}.{1}", b.Major, b.Minor); OneBeacon ob = new OneBeacon (); ob.Id = id; ob.Distance = v; switch (b.Proximity) { case CLProximity.Far: ob.Proximity = 10; break; case CLProximity.Near: ob.Proximity = 5; break; case CLProximity.Immediate: ob.Proximity = 1; break; } newBeacons.Add (id, ob); } catch (Exception err) { Console.WriteLine (err.Message); } } if (newBeacons.Count > 0) { List<OneBeacon> list = new List<OneBeacon> (); foreach (KeyValuePair<string,OneBeacon> kvp in newBeacons) { list.Add (kvp.Value); } // on envoi l'info Founded (list); } }