Esempio n. 1
0
        public static iBeaconData iBeaconParseAdvertisement(this BluetoothLEAdvertisement Advertisment,short RawSignalStrengthInDBm)
        {
            iBeaconData beacon = null;
            foreach(var adv in Advertisment.ManufacturerData)
               if (adv.CompanyId==76) //Apple
                {
                    var bytes = adv.Data.ToArray();
                    if (bytes[0] == 0x02 && bytes[1] == 0x15 && bytes.Length==23)
                    {
                        //iBeacon Data
                        beacon = new iBeaconData();
                        beacon.UUID =new Guid(bytes.Skip(2).Take(16).ToArray());
                        beacon.Major = BitConverter.ToUInt16(bytes.Skip(18).Take(2).Reverse().ToArray(),0);
                        beacon.Minor = BitConverter.ToUInt16(bytes.Skip(20).Take(2).Reverse().ToArray(),0);
                        beacon.TxPower =(short) (sbyte) bytes[22];
                        beacon.Rssi = RawSignalStrengthInDBm;

                        //Estimated value
                        //Read this article http://developer.radiusnetworks.com/2014/12/04/fundamentals-of-beacon-ranging.html 
                        beacon.Distance = CalculateDistance(beacon.TxPower,RawSignalStrengthInDBm);

                        Debug.WriteLine("UUID: "+beacon.UUID.ToString()+" Major: "+beacon.Major+" Minor:"+beacon.Minor+" Power: "+ beacon.TxPower+ " Rssi: "+RawSignalStrengthInDBm+" Distance:"+ beacon.Distance);
                    }
                }
   
            return beacon;
        }
Esempio n. 2
0
        public static iBeaconData iBeaconParseAdvertisement(this BluetoothLEAdvertisement Advertisment, short RawSignalStrengthInDBm)
        {
            iBeaconData beacon = null;

            foreach (var adv in Advertisment.ManufacturerData)
            {
                if (adv.CompanyId == 76) //Apple
                {
                    var bytes = adv.Data.ToArray();
                    if (bytes[0] == 0x02 && bytes[1] == 0x15 && bytes.Length == 23)
                    {
                        //iBeacon Data
                        beacon         = new iBeaconData();
                        beacon.UUID    = new Guid(bytes.Skip(2).Take(16).ToArray());
                        beacon.Major   = BitConverter.ToUInt16(bytes.Skip(18).Take(2).Reverse().ToArray(), 0);
                        beacon.Minor   = BitConverter.ToUInt16(bytes.Skip(20).Take(2).Reverse().ToArray(), 0);
                        beacon.TxPower = (short)(sbyte)bytes[22];
                        beacon.Rssi    = RawSignalStrengthInDBm;

                        //Estimated value
                        //Read this article http://developer.radiusnetworks.com/2014/12/04/fundamentals-of-beacon-ranging.html
                        beacon.Distance = CalculateDistance(beacon.TxPower, RawSignalStrengthInDBm);

                        Debug.WriteLine("UUID: " + beacon.UUID.ToString() + " Major: " + beacon.Major + " Minor:" + beacon.Minor + " Power: " + beacon.TxPower + " Rssi: " + RawSignalStrengthInDBm + " Distance:" + beacon.Distance);
                    }
                }
            }

            return(beacon);
        }
Esempio n. 3
0
        public static void iBeaconSetAdvertisement(this BluetoothLEAdvertisement Advertisment, iBeaconData data)
        {
            BluetoothLEManufacturerData manufacturerData = new BluetoothLEManufacturerData();

            // Set Apple as the manufacturer data
            manufacturerData.CompanyId = 76;

            var writer = new DataWriter();
            writer.WriteUInt16(0x0215); //bytes 0 and 1 of the iBeacon advertisment indicator

            if (data!=null& data.UUID!= Guid.Empty)
            {
                //If UUID is null scanning for all iBeacons
                writer.WriteBytes( data.UUID.ToByteArray());
                if (data.Major!=0)
                {
                    //If Major not null searching with UUID and Major
                    writer.WriteBytes(BitConverter.GetBytes(data.Major).Reverse().ToArray());
                    if (data.Minor != 0)
                    {
                        //If Minor not null we are looking for a specific beacon not a class of beacons
                        writer.WriteBytes(BitConverter.GetBytes(data.Minor).Reverse().ToArray());
                        if (data.TxPower != 0)
                            writer.WriteBytes(BitConverter.GetBytes(data.TxPower));
                    }
                }
            }

            manufacturerData.Data = writer.DetachBuffer();

            Advertisment.ManufacturerData.Clear();
            Advertisment.ManufacturerData.Add(manufacturerData);
        }
Esempio n. 4
0
        public static void iBeaconSetAdvertisement(this BluetoothLEAdvertisement Advertisment, iBeaconData data)
        {
            BluetoothLEManufacturerData manufacturerData = new BluetoothLEManufacturerData();

            // Set Apple as the manufacturer data
            manufacturerData.CompanyId = 76;

            var writer = new DataWriter();

            writer.WriteUInt16(0x0215); //bytes 0 and 1 of the iBeacon advertisment indicator

            //if (data!=null& data.UUID!= Guid.Empty)
            //{
            //    //If UUID is null scanning for all iBeacons
            //    writer.WriteBytes( data.UUID.ToByteArray());
            //    if (data.Major!=0)
            //    {
            //        //If Major not null searching with UUID and Major
            //        writer.WriteBytes(BitConverter.GetBytes(data.Major).Reverse().ToArray());
            //        if (data.Minor != 0)
            //        {
            //            //If Minor not null we are looking for a specific beacon not a class of beacons
            //            writer.WriteBytes(BitConverter.GetBytes(data.Minor).Reverse().ToArray());
            //            if (data.TxPower != 0)
            //                writer.WriteBytes(BitConverter.GetBytes(data.TxPower));
            //        }
            //    }
            //}

            manufacturerData.Data = writer.DetachBuffer();

            Advertisment.ManufacturerData.Clear();
            Advertisment.ManufacturerData.Add(manufacturerData);
        }