/// <summary>
        /// Tries to read proximity beacon data from the given advertisementReceivedEventArgs.
        /// </summary>
        /// <param name="advertisementReceivedEventArgs">The bluetooth advertisement message from which to read a proximity beacon message from.</param>
        /// <param name="beacon">The resulting proximity beacon data that can be read from the advertisement.</param>
        /// <param name="requestedTypes">The type(s) of proximity beacons to look for.</param>
        /// <returns>True when proximity beacon data can be read from the advertisement.</returns>
        public static bool TryReadProximityBeacon(BluetoothLEAdvertisementReceivedEventArgs advertisementReceivedEventArgs,
            out ProximityBeacon beacon, params ProximityBeaconType[] requestedTypes)
        {
            if (requestedTypes.Count() == 1 && requestedTypes[0] == ProximityBeaconType.Unknown)
            {
                throw new ArgumentException($"Proximity Beacon Type {nameof(ProximityBeaconType.Unknown)} cannot be parsed!");
            }

            bool result = false;
            beacon = null;

            foreach (var parser in AvailableParsers.Where(a => requestedTypes.Contains(a.Type)))
            {
                if (parser.CanParseAdvertisement(advertisementReceivedEventArgs))
                {
                    try
                    {
                        beacon = parser.Parse(advertisementReceivedEventArgs);
                        result = true;
                        break;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
                else
                    continue;
            }
            return result;
        }
Exemplo n.º 2
0
        private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
        {
            ProximityBeacon beacon = null;

            if (ProximityBeaconReader.TryReadProximityBeacon(args, out beacon, ProximityBeaconType.IBeacon))
            {
                await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    if (beacon.BeaconType == ProximityBeaconType.IBeacon)
                    {
                        var iBeacon = beacon as IBeacon;

                        Debug.WriteLine($"RSSI: {args.RawSignalStrengthInDBm}, distance: {ProximityBeaconHelper.CalculateDistance(iBeacon.MeasuredPower, args.RawSignalStrengthInDBm)}");

                        // Display these information on the list
                        ReceivedAdvertisementListBox.Items.Add(
                            $"Beacon: UUID={iBeacon.UUID}\n\tMajor={iBeacon.Major}\n\tMinor={iBeacon.Minor}\n\trssi={args.RawSignalStrengthInDBm}\n\ttimestamp={args.Timestamp.ToString("HH\\:mm\\:ss\\.fff")}\n\tAddress:{args.BluetoothAddress}");
                    }
                });
            }
        }