public async void AddOrReplace(Beacon beacon)
        {
            bool updated = false;

            for (int i = 0; i < BeaconDetailsCollection.Count; ++i)
            {
                if (BeaconDetailsCollection[i].Matches(beacon))
                {
                    BeaconDetailsCollection[i].Update(beacon);
                    updated = true;
                    break;
                }
            }

            if (!updated)
            {
                BeaconDetailsItem item = new BeaconDetailsItem(beacon);
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () =>
                    BeaconDetailsCollection.Add(item));

                if (_updateBeaconTimesTimer == null)
                {
                    _updateBeaconTimesTimer = new Timer(UpdateBeaconTimesAsync, null, 1000, 1000);
                }
            }
        }
        public async Task EventHistory_FlushHistory()
        {
            var beacon = new Beacon();
            beacon.Id1 = "7367672374000000ffff0000ffff0007";
            beacon.Id2 = 8008;
            beacon.Id3 = 5;
            beacon.Timestamp = DateTimeOffset.Now;
            var args = new BeaconEventArgs();
            args.Beacon = beacon;
            args.EventType = BeaconEventType.Exit;
            var resolvedActionEventArgs = new ResolvedActionsEventArgs() {BeaconPid = beacon.Pid, BeaconEventType = BeaconEventType.Enter};

            BeaconAction beaconaction1 = new BeaconAction() {Body = "body", Url = "http://www.com", Uuid = "1223"};
            BeaconAction beaconaction2 = new BeaconAction() {Body = "body", Url = "http://www.com", Uuid = "5678"};
            BeaconAction beaconaction3 = new BeaconAction() {Body = "body", Url = "http://www.com", Uuid = "9678"};
            ResolvedAction res1 = new ResolvedAction() {SuppressionTime = 100, SendOnlyOnce = true, BeaconAction = beaconaction1};
            ResolvedAction res2 = new ResolvedAction() {SuppressionTime = 100, SendOnlyOnce = true, BeaconAction = beaconaction2};
            ResolvedAction res3 = new ResolvedAction() {SuppressionTime = 1, SendOnlyOnce = true, BeaconAction = beaconaction3};

            EventHistory eventHistory = new EventHistory();

            await eventHistory.SaveBeaconEventAsync(args, null);
            await eventHistory.SaveExecutedResolvedActionAsync(resolvedActionEventArgs, beaconaction1);
            await eventHistory.SaveExecutedResolvedActionAsync(resolvedActionEventArgs, beaconaction3);

            await eventHistory.FlushHistoryAsync();
        }
        public void AddOrReplace(Beacon beacon)
        {
            bool updated = false;

            for (int i = 0; i < BeaconDetailsCollection.Count; ++i)
            {
                if (BeaconDetailsCollection[i].Matches(beacon))
                {
                    BeaconDetailsCollection[i].Update(beacon);
                    updated = true;
                    break;
                }
            }

            if (!updated)
            {
                BeaconDetailsItem item = new BeaconDetailsItem(beacon);
                item.VendorName = ResolveVendor(beacon.Id1);
                BeaconDetailsCollection.Add(item);

                if (_updateBeaconTimesTimer == null)
                {
                    _updateBeaconTimesTimer = new Timer(UpdateBeaconTimesAsync, null, 1000, 1000);
                }
            }
        }
        public async Task EventHistory_FlushHistory()
        {

            SDKData.Instance.ApiKey = "540aa95ccf215718295c2c563a2090676994f09927f09a6e09a67c83be10b00c";
            var beacon = new Beacon();
            beacon.Id1 = "7367672374000000ffff0000ffff0007";
            beacon.Id2 = 8008;
            beacon.Id3 = 5;
            beacon.Timestamp = DateTimeOffset.Now;
            var args = new BeaconEventArgs();
            args.Beacon = beacon;
            args.EventType = BeaconEventType.Exit;
            var resolvedActionEventArgs = new ResolvedActionsEventArgs() { BeaconPid = beacon.Pid, BeaconEventType = BeaconEventType.Enter };

            BeaconAction beaconaction1 = new BeaconAction() { Body = "body", Url = "http://www.com", Uuid = "1223" };
            BeaconAction beaconaction2 = new BeaconAction() { Body = "body", Url = "http://www.com", Uuid = "5678" };
            BeaconAction beaconaction3 = new BeaconAction() { Body = "body", Url = "http://www.com", Uuid = "9678" };
            ResolvedAction res1 = new ResolvedAction() { SupressionTime = 100, SendOnlyOnce = true, BeaconAction = beaconaction1 };
            ResolvedAction res2 = new ResolvedAction() { SupressionTime = 100, SendOnlyOnce = true, BeaconAction = beaconaction2 };
            ResolvedAction res3 = new ResolvedAction() { SupressionTime = 1, SendOnlyOnce = true, BeaconAction = beaconaction3 };

            EventHistory eventHistory = new EventHistory();

            await eventHistory.SaveBeaconEventAsync(args);
            await eventHistory.SaveExecutedResolvedActionAsync(resolvedActionEventArgs, beaconaction1);
            await eventHistory.SaveExecutedResolvedActionAsync(resolvedActionEventArgs, beaconaction3);

            await eventHistory.FlushHistoryAsync();


        }
        public void Remove(Beacon beacon)
        {
            int index = 0;

            for (index = 0; index < BeaconDetailsCollection.Count; ++index)
            {
                if (BeaconDetailsCollection[index].Matches(beacon))
                {
                    BeaconDetailsCollection.RemoveAt(index);
                    break;
                }
            }
        }
        public async Task TestDetectEnterExit()
        {
            BeaconManager manager = new BeaconManager(200);
            Beacon beacon = new Beacon() {Id1 = "7367672374000000ffff0000ffff0004", Id2 = 39178, Id3 = 30929};
            Assert.AreEqual(BeaconEventType.Enter, manager.ResolveBeaconState(beacon));

            await Task.Delay(1000);
            Assert.AreEqual(1, manager.ResolveBeaconExits().Count);
            Assert.AreEqual(0, manager.ResolveBeaconExits().Count);


            Assert.AreEqual(BeaconEventType.Enter, manager.ResolveBeaconState(beacon));

            await Task.Delay(1000);
            Assert.AreEqual(1, manager.ResolveBeaconExits().Count);
            Assert.AreEqual(0, manager.ResolveBeaconExits().Count);
        }
        public void Remove(Beacon beacon)
        {
            bool found = false;
            int index = 0;

            for (index = 0; index < BeaconDetailsCollection.Count; ++index)
            {
                if (BeaconDetailsCollection[index].Matches(beacon))
                {
                    found = true;
                    break;
                }
            }

            if (found)
            {
                BeaconDetailsCollection.RemoveAt(index);
            }
        }
Exemple #8
0
        /// <summary>
        /// Starts advertizing based on the set values (beacon ID 1, ID 2 and ID 3).
        /// Note that this method does not validate the values and will throw exception, if the
        /// values are invalid.
        /// </summary>
        public void Start()
        {
            if (!IsStarted)
            {
                _beacon                = new Beacon();
                _beacon.Id1            = BeaconId1;
                _beacon.ManufacturerId = ManufacturerId;
                _beacon.Code           = BeaconCode;
                _beacon.Id2            = BeaconId2;
                _beacon.Id3            = BeaconId3;
                _beacon.MeasuredPower  = DefaultMeasuredPower;

                _advertisementPublisher = new BluetoothLEAdvertisementPublisher();

                BluetoothLEAdvertisementDataSection dataSection = BeaconFactory.BeaconToSecondDataSection(_beacon);
                Logger.Debug("Advertiser.Start(): " + BeaconFactory.DataSectionToRawString(dataSection));
                _advertisementPublisher.Advertisement.DataSections.Add(dataSection);
                _advertisementPublisher.Start();

                IsStarted = true;
            }
        }
        public async Task EventHistory_ShouldSupress()
        {
            var beacon = new Beacon();
            beacon.Id1 = "7367672374000000ffff0000ffff0007";
            beacon.Id2 = 8008;
            beacon.Id3 = 5;
            beacon.Timestamp = DateTimeOffset.Now;
            var args = new BeaconEventArgs();
            args.Beacon = beacon;
            args.EventType = BeaconEventType.Exit;
            var resolvedActionEventArgs = new ResolvedActionsEventArgs() {BeaconPid = beacon.Pid, BeaconEventType = BeaconEventType.Enter};

            BeaconAction beaconaction1 = new BeaconAction() {Body = "body", Url = "http://www.com", Uuid = "1223"};
            BeaconAction beaconaction2 = new BeaconAction() {Body = "body", Url = "http://www.com", Uuid = "5678"};
            BeaconAction beaconaction3 = new BeaconAction() {Body = "body", Url = "http://www.com", Uuid = "9678"};
            ResolvedAction res1 = new ResolvedAction() {SuppressionTime = 100, SendOnlyOnce = true, BeaconAction = beaconaction1};
            ResolvedAction res2 = new ResolvedAction() {SuppressionTime = 100, SendOnlyOnce = true, BeaconAction = beaconaction2};
            ResolvedAction res3 = new ResolvedAction() {SuppressionTime = 1, SendOnlyOnce = true, BeaconAction = beaconaction3};

            EventHistory eventHistory = new EventHistory();

            await eventHistory.SaveBeaconEventAsync(args, null);
            await eventHistory.SaveExecutedResolvedActionAsync(resolvedActionEventArgs, beaconaction1);
            await eventHistory.SaveExecutedResolvedActionAsync(resolvedActionEventArgs, beaconaction3);

            eventHistory.ShouldSupressAsync(res1);
            eventHistory.ShouldSupressAsync(res3);

            await Task.Delay(2000);


            bool shouldSupress1 = eventHistory.ShouldSupressAsync(res1);
            bool shouldSupress2 = eventHistory.ShouldSupressAsync(res2);
            bool shouldSupress3 = eventHistory.ShouldSupressAsync(res3);

            Assert.IsTrue(shouldSupress1);
            Assert.IsFalse(shouldSupress2);
            Assert.IsFalse(shouldSupress3); //Supression time should be over
        }
        /// <summary>
        /// Starts advertizing based on the set values (beacon ID 1, ID 2 and ID 3).
        /// Note that this method does not validate the values and will throw exception, if the
        /// values are invalid.
        /// </summary>
        public void Start()
        {
            if (!IsStarted)
            {
                _beacon = new Beacon();
                _beacon.Id1 = BeaconId1;
                _beacon.ManufacturerId = ManufacturerId;
                _beacon.Code = BeaconCode;
                _beacon.Id2 = BeaconId2;
                _beacon.Id3 = BeaconId3;
                _beacon.MeasuredPower = DefaultMeasuredPower;

                _advertisementPublisher = new BluetoothLEAdvertisementPublisher();

                BluetoothLEAdvertisementDataSection dataSection = BeaconFactory.BeaconToSecondDataSection(_beacon);
                Logger.Debug("Advertiser.Start(): " + BeaconFactory.DataSectionToRawString(dataSection));
                _advertisementPublisher.Advertisement.DataSections.Add(dataSection);
                _advertisementPublisher.Start();

                IsStarted = true;
            }
        }
 private void AddBeaconArgs(Beacon beacon, BeaconEventType eventType)
 {
     var args = new BeaconEventArgs();
     args.Beacon = beacon;
     args.EventType = eventType;
     _beaconArgs.Add(args);
 }
Exemple #12
0
 private bool Equals(Beacon other)
 {
     return(string.Equals(Pid, other.Pid));
 }
Exemple #13
0
 /// <summary>
 /// Compares the given beacon to this.
 /// </summary>
 /// <param name="beacon">The beacon to compare to.</param>
 /// <returns>True, if the beacons match.</returns>
 public bool Matches(Beacon beacon)
 {
     return beacon.Id1.Equals(Id1)
         && beacon.Id2 == Id2
         && beacon.Id3 == Id3;
 }
        public bool Contains(Beacon beacon)
        {
            bool found = false;

            for (int i = 0; i < BeaconDetailsCollection.Count; ++i)
            {
                if (BeaconDetailsCollection[i].Matches(beacon))
                {
                    found = true;
                    break;
                }
            }

            return found;
        }
        public async Task TestMultipleEnterExitEvent()
        {
            Resolver resolver = new Resolver(true);
            resolver.BeaconManager.ExitTimeout = 100;

            int enterCounter = 0;
            int exitCounter = 0;
            resolver.ActionsResolved += (sender, args) =>
            {
                if (args.BeaconEventType == BeaconEventType.Enter)
                {
                    enterCounter++;
                }
                if (args.BeaconEventType == BeaconEventType.Exit)
                {
                    exitCounter++;
                }

            };
            Beacon beacon = new Beacon() { Id1 = "7367672374000000ffff0000ffff0004", Id2 = 39178, Id3 = 30929 };
            await resolver.CreateRequest(new BeaconEventArgs()
            {
                Beacon = beacon,
                EventType = BeaconEventType.Unknown
            });
            await resolver.CreateRequest(new BeaconEventArgs()
            {
                Beacon = beacon,
                EventType = BeaconEventType.Unknown
            });
            await resolver.CreateRequest(new BeaconEventArgs()
            {
                Beacon = beacon,
                EventType = BeaconEventType.Unknown
            });

            await Task.Delay(1000);

            await resolver.CreateRequest(new BeaconEventArgs()
            {
                Beacon = beacon,
                EventType = BeaconEventType.Unknown
            });


            Assert.AreEqual(2, enterCounter, "Not 2 enter action found");


            Assert.AreEqual(1, exitCounter, "Not 1 exit action found");
        }
 public void NotifyBeaconEvent(Beacon beacon, BeaconEventType eventType)
 {
     BeaconEvent?.Invoke(this, new BeaconEventArgs() { Beacon = beacon, EventType = eventType });
 }
 public void SetBeaconRange(Beacon beacon, int range)
 {
     for (int i = 0; i < BeaconDetailsCollection.Count; ++i)
     {
         if (BeaconDetailsCollection[i].Matches(beacon))
         {
             BeaconDetailsCollection[i].Range = range;
             break;
         }
     }
 }
		private async void OnBeaconNotSeenForAWhileAsync(object sender, Beacon e)
		{
			await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
			{
                BeaconModel.SetBeaconRange(e, 0);
			});
		}
 public bool FilterBeaconByUuid(Beacon beacon)
 {
     throw new NotImplementedException();
 }
Exemple #20
0
 /// <summary>
 /// Compares the given beacon to this.
 /// </summary>
 /// <param name="beacon">The beacon to compare to.</param>
 /// <returns>True, if the beacons match.</returns>
 public bool Matches(Beacon beacon)
 {
     return(beacon.Id1 == Id1 &&
            beacon.Id2 == Id2 &&
            beacon.Id3 == Id3);
 }
        /// <summary>
        /// Creates the second part of the beacon advertizing packet.
        /// Uses the beacon IDs 1, 2, 3 and measured power to create the data section.
        /// </summary>
        /// <param name="beacon">A beacon instance.</param>
        /// <param name="includeAuxByte">Defines whether we should add the additional byte or not.</param>
        /// <returns>A newly created data section.</returns>
        public static BluetoothLEAdvertisementDataSection BeaconToSecondDataSection(
            Beacon beacon, bool includeAuxByte = false)
        {
            string[] temp = beacon.Id1.Split(HexStringSeparator);
            string beaconId1 = string.Join("", temp);

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(ManufacturerIdToString(beacon.ManufacturerId));
            stringBuilder.Append(BeaconCodeToString(beacon.Code));
            stringBuilder.Append(beaconId1.ToUpper());

            byte[] beginning = HexStringToByteArray(stringBuilder.ToString());

            byte[] data = includeAuxByte
                ? new byte[SecondBeaconDataSectionMinimumLengthInBytes + 1]
                : new byte[SecondBeaconDataSectionMinimumLengthInBytes];
            
            beginning.CopyTo(data, 0);
            ChangeInt16ArrayEndianess(BitConverter.GetBytes(beacon.Id2)).CopyTo(data, 20);
            ChangeInt16ArrayEndianess(BitConverter.GetBytes(beacon.Id3)).CopyTo(data, 22);
            data[24] = (byte)Convert.ToSByte(beacon.MeasuredPower);
            
            if (includeAuxByte)
            {
                data[25] = beacon.Aux;
            }

            BluetoothLEAdvertisementDataSection dataSection = new BluetoothLEAdvertisementDataSection();
            dataSection.DataType = SecondBeaconDataSectionDataType;
            dataSection.Data = data.AsBuffer();

            return dataSection;
        }
        /// <summary>
        /// Constructs a Beacon instance and sets the properties based on the given data.
        /// 
        /// The expected specification of the data is as follows:
        /// 
        /// Byte(s)     Name
        /// --------------------------
        /// 0-1         Manufacturer ID (16-bit unsigned integer, big endian)
        /// 2-3         Beacon code (two 8-bit unsigned integers, but can be considered as one 16-bit unsigned integer in little endian)
        /// 4-19        ID1 (UUID)
        /// 20-21       ID2 (16-bit unsigned integer, big endian)
        /// 22-23       ID3 (16-bit unsigned integer, big endian)
        /// 24          Measured Power (signed 8-bit integer)
        /// 25          Additional information byte (optional)
        /// 
        /// For more details on the beacon specifications see https://github.com/AltBeacon/spec
        /// 
        /// The minimum length of the given byte array is 25. If it is longer than 26 bits,
        /// everything after the 26th bit is ignored.
        /// </summary>
        /// <param name="data">The data to populate the Beacon instance properties with.</param>
        /// <returns>A newly created Beacon instance or null in case of a failure.</returns>
        public static Beacon BeaconFromByteArray([ReadOnlyArray] byte[] data)
        {
            if (data == null || data.Length < SecondBeaconDataSectionMinimumLengthInBytes)
            {
                // The given data is null or too short
                return null;
            }

            Beacon beacon = new Beacon();
            beacon.Code = BitConverter.ToUInt16(data, 2); // Bytes 2-3
            beacon.Id1 = FormatUuid(BitConverter.ToString(data, 4, 16)); // Bytes 4-19
            beacon.MeasuredPower = Convert.ToSByte(BitConverter.ToString(data, 24, 1), 16); // Byte 24
            
            if (data.Length >= SecondBeaconDataSectionMinimumLengthInBytes + 1)
            {
                beacon.Aux = data[25]; // Byte 25
            }

            // Data is expected to be big endian. Thus, if we are running on a little endian,
            // we need to switch the bytes
            if (BitConverter.IsLittleEndian)
            {
                data = ChangeInt16ArrayEndianess(data);
            }

            beacon.ManufacturerId = BitConverter.ToUInt16(data, 0); // Bytes 0-1
            beacon.Id2 = BitConverter.ToUInt16(data, 20); // Bytes 20-21
            beacon.Id3 = BitConverter.ToUInt16(data, 22); // Bytes 22-23

            return beacon;
        }
 private bool Equals(Beacon other)
 {
     return string.Equals(Pid, other.Pid);
 }
 /// <summary>
 /// Compares the given beacon to this.
 /// </summary>
 /// <param name="beacon">The beacon to compare to.</param>
 /// <returns>True, if the beacons match.</returns>
 public bool Matches(Beacon beacon)
 {
     return beacon.Id1 == Id1
            && beacon.Id2 == Id2
            && beacon.Id3 == Id3;
 }