예제 #1
0
        /// <summary>
        /// Consume the iBeacon BLE packets here
        /// </summary>
        /// <param name="advertisement">iBeacon BLE Advertisement</param>
        static void OnIBeaconAdvertisementScanned(IBeaconAdModel advertisement)
        {
            var bluetoothMAC = advertisement.BluetoothAddress.ToByteArray(true).ToHexString(delimeter: ":");
            var maj          = advertisement.Major;
            var min          = advertisement.Minor;
            var proxUUID     = Guid.Parse(advertisement.ProximityUUID).ToString();

            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.Write($" {DateTime.Now.ToString("HH:mm:ss.fff")}: ");
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write($" {bluetoothMAC} ");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write($" | Major ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(maj);
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write($" | Minor ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(min);
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write($" | ProxUUID ");
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine(proxUUID);
            Console.ForegroundColor = ConsoleColor.Gray;
        }
예제 #2
0
        private IBeaconAdModel TryParseAppleIBeacon(AdStructure adStruct, string bluetoothAddress)
        {
            try {
                // Check length of struct
                // (AD_APPLE_IBEACON_SIZE - 1 additional ad struct, size & struct ID bytes from this struct)
                if (adStruct.Data.Length != AD_APPLE_IBEACON_SIZE - 5)
                {
                    return(null);
                }

                var data = adStruct.Data;
                var id   = data.Take(2).ToHexString(true);

                if (!string.Equals(id, APPLE_ID, StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }

                if (data[2] != 0x02)
                {
                    return(null);
                }
                if (data[3] != 0x15)
                {
                    return(null);                 //  length
                }
                var proximityUUID = data.Skip(4).Take(16).ToHexString();
                var major         = BitConverter.ToUInt16(data.Skip(20).Take(2).Reverse().ToArray(), 0);
                var minor         = BitConverter.ToUInt16(data.Skip(22).Take(2).Reverse().ToArray(), 0);
                var measuredPower = (sbyte)data.Last();

                var appleIBeaconData = new IBeaconAdModel {
                    Major            = major,
                    Minor            = minor,
                    ProximityUUID    = proximityUUID,
                    MeasuredPower    = measuredPower,
                    BluetoothAddress = bluetoothAddress
                };

                Debug.WriteLine(
                    $"Parsed AppleIBeaconAdData: " +
                    $"maj={appleIBeaconData.Major} " +
                    $"min={appleIBeaconData.Minor} " +
                    $"prox={appleIBeaconData.ProximityUUID} " +
                    $"txpwr={appleIBeaconData.MeasuredPower}"
                    );

                return(appleIBeaconData);
            } catch (Exception ex) {
                Debug.WriteLine($"Error while parsing AppleIBeacon ad: {ex.GetBaseException().Message}");
                return(null);
            }
        }