Ejemplo n.º 1
0
 /// <summary>
 /// Construct a new Bluetooth beacon based on the received advertisement.
 /// Tries to find out if it's a known type, and then parses the contents accordingly.
 /// </summary>
 /// <param name="btAdv">Bluetooth advertisement to parse, as received from
 /// the Windows Bluetooth LE API.</param>
 public Beacon(BeaconPacket btAdv)
 {
     BluetoothAddress = btAdv.BluetoothAddress;
     Rssi             = btAdv.RawSignalStrengthInDBm;
     Timestamp        = btAdv.Timestamp;
     Region           = btAdv.Region;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Analyze the received Bluetooth LE advertisement, and either add a new unique
        /// beacon to the list of known beacons, or update a previously known beacon
        /// with the new information.
        /// </summary>
        /// <param name="btAdv">Bluetooth advertisement to parse, as received from
        /// the Windows Bluetooth LE API.</param>
        private void ReceivedBeacon(BeaconPacket btAdv)
        {
            if (btAdv == null)
            {
                return;
            }

            // Beacon was not yet known - add it to the list.
            var newBeacon = new Beacon(btAdv);

            BeaconReceived?.Invoke(this, newBeacon);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Convert the Windows specific Bluetooth LE advertisment data to the universal cross-platform structure
        /// used by the Universal Beacon Library.
        /// </summary>
        /// <param name="args">Windows Bluetooth LE advertisment data to convert to cross-platform format.</param>
        /// <returns>Packet data converted to cross-platform format.</returns>
        public static BeaconPacket ToUniversalBLEPacket(this BluetoothLEAdvertisementReceivedEventArgs args)
        {
            var packet = new BeaconPacket
            {
                Timestamp              = args.Timestamp,
                BluetoothAddress       = args.BluetoothAddress,
                RawSignalStrengthInDBm = args.RawSignalStrengthInDBm,
                // AdvertisementType = (BeaconType) args.AdvertisementType,
                // Advertisement = args.Advertisement.ToUniversalAdvertisement()
            };

            return(packet);
        }
Ejemplo n.º 4
0
        private string Pack(string command, object data = null, bool requireSerialization = false)
        {
            var packet = new BeaconPacket
            {
                Command    = command,
                Serialized = requireSerialization
            };

            if (data != null)
            {
                packet.Data = requireSerialization
                        ? JsonConvert.SerializeObject(data)
                        : (string)data;
            }

            var stringified    = packet.ToString();
            var plainTextBytes = Encoding.UTF8.GetBytes(stringified);

            return(Convert.ToBase64String(plainTextBytes));
        }
Ejemplo n.º 5
0
        public override void OnScanResult([GeneratedEnum] ScanCallbackType callbackType, ScanResult result)
        {
            base.OnScanResult(callbackType, result);

            switch (result.Device.Type)
            {
            case BluetoothDeviceType.Le:
            case BluetoothDeviceType.Unknown:
                try
                {
                    byte[] scanData = result.ScanRecord.GetBytes();

                    var beacon = BeaconRawDataParser.ParseRawData(scanData);

                    if (beacon is null || beacon.BeaconType != Core.Entities.Beacon.BeaconTypeEnum.iBeacon)
                    {
                        return;
                    }

                    var packet = new BeaconPacket(
                        beacon.Region,
                        result.Device.Address.ToNumericAddress(),
                        DateTimeOffset.FromUnixTimeMilliseconds(result.TimestampNanos / 1000),
                        (short)result.Rssi);

                    OnAdvertisementPacketReceived?.Invoke(this, new BeaconPacketArgs(packet));
                }
                catch (Exception)
                {
                    Debug.WriteLine("Failed to parse beacon", LogTag);
                }
                break;

            default:
                //Debug.WriteLine($"Skipped parsing bluetooth device type {result.Device.Type}", LogTag);
                break;
            }
        }
Ejemplo n.º 6
0
 public BeaconPacketArgs(BeaconPacket data)
 {
     Data = data;
 }
Ejemplo n.º 7
0
 private static void UdpListener_BeaconPacketReceived(BeaconPacket packet)
 {
     packetHandler.AddPacket(packet);
 }