Example #1
0
        public void TestBeaconAdvertisingBytesForEddystone()
        {
            Context context = Application.Context;

            Beacon beacon = new Beacon.Builder()
                            .SetId1("0x2f234454f4911ba9ffa6")
                            .SetId2("0x000000000001")
                            .SetManufacturer(0x0118)
                            .SetTxPower(-59)
                            .Build();
            BeaconParser beaconParser = new BeaconParser()
                                        .SetBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19");

            byte[]            data = beaconParser.GetBeaconAdvertisementData(beacon);
            BeaconTransmitter beaconTransmitter = new BeaconTransmitter(context, beaconParser);
            // TODO: can't actually start transmitter here because Robolectric does not support API 21

            String byteString = "";

            for (int i = 0; i < data.Length; i++)
            {
                byteString += String.Format("{0:x2}", data[i]);
                byteString += " ";
            }

            AssertEx.AreEqual("Data should be 24 bytes long", 18, data.Length);
            AssertEx.AreEqual("Advertisement bytes should be as expected", "00 C5 2F 23 44 54 F4 91 1B A9 FF A6 00 00 00 00 00 01 ", byteString);
        }
        public void testBeaconAdvertisingBytes()
        {
            Context context = Application.Context;


            Beacon beacon = new Beacon.Builder()
                            .SetId1("0x454452e29735323d81c0")
                            .SetId2("0x060504030201")
                            .SetDataFields(new List <Java.Lang.Long> {
                new Java.Lang.Long(0x25L)
            })
                            .SetTxPower(-59)
                            .Build();
            // TODO: need to use something other than the d: prefix here for an internally generated field
            BeaconParser beaconParser = new BeaconParser()
                                        .SetBeaconLayout("s:0-1=0123,m:2-2=00,d:3-3,p:4-4,i:5-14,i:15-20");

            byte[]            data = beaconParser.GetBeaconAdvertisementData(beacon);
            BeaconTransmitter beaconTransmitter = new BeaconTransmitter(context, beaconParser);

            // TODO: can't actually start transmitter here because Robolectric does not support API 21

            AssertEx.AreEqual("Data should be 19 bytes long", 19, data.Length);
            String byteString = "";

            for (int i = 0; i < data.Length; i++)
            {
                byteString += String.Format("{0:x2}", data[i]);
                byteString += " ";
            }
            AssertEx.AreEqual("Advertisement bytes should be as expected", "00 25 C5 45 44 52 E2 97 35 32 3D 81 C0 06 05 04 03 02 01 ", byteString);
        }
Example #3
0
        public void TestBeaconAdvertisingBytes()
        {
            Context context = Application.Context;

            Beacon beacon = new Beacon.Builder()
                            .SetId1("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")
                            .SetId2("1")
                            .SetId3("2")
                            .SetManufacturer(0x0118)
                            .SetTxPower(-59)
                            .SetDataFields(new List <Java.Lang.Long> {
                new Java.Lang.Long(0L)
            })
                            .Build();
            BeaconParser beaconParser = new BeaconParser()
                                        .SetBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25");

            byte[]            data = beaconParser.GetBeaconAdvertisementData(beacon);
            BeaconTransmitter beaconTransmitter = new BeaconTransmitter(context, beaconParser);

            // TODO: can't actually start transmitter here because Robolectric does not support API 21

            AssertEx.AreEqual("Data should be 24 bytes long", 24, data.Length);
            String byteString = "";

            for (int i = 0; i < data.Length; i++)
            {
                byteString += String.Format("{0:x2}", data[i]);
                byteString += " ";
            }
            AssertEx.AreEqual("Advertisement bytes should be as expected", "BE AC 2F 23 44 54 CF 6D 4A 0F AD F2 F4 91 1B A9 FF A6 00 01 00 02 C5 00 ", byteString);
        }
Example #4
0
        public void TestCanParseLocationBeacon()
        {
            double latitude  = 38.93;
            double longitude = -77.23;
            var    beacon    = new Beacon.Builder()
                               .SetManufacturer(0x0118) // Radius Networks
                               .SetId1("1")             // device sequence number
                               .SetId2(String.Format("{0:X8}", (long)((latitude + 90) * 10000.0)))
                               .SetId3(String.Format("{0:X8}", (long)((longitude + 180) * 10000.0)))
                               .SetTxPower(-59) // The measured transmitter power at one meter in dBm
                               .Build();
            // TODO: make this pass if data fields are little endian or > 4 bytes (or even > 2 bytes)
            var p = new BeaconParser().
                    SetBeaconLayout("m:2-3=10ca,i:4-9,i:10-13,i:14-17,p:18-18");
            var bytes       = p.GetBeaconAdvertisementData(beacon);
            var headerBytes = HexStringToByteArray("02011a1bff1801");
            var advBytes    = new byte[bytes.Length + headerBytes.Length];

            Array.Copy(headerBytes, 0, advBytes, 0, headerBytes.Length);
            Array.Copy(bytes, 0, advBytes, headerBytes.Length, bytes.Length);

            Beacon parsedBeacon = p.FromScanData(advBytes, -59, null);

            AssertEx.NotNull(String.Format("Parsed beacon from {0} should not be null", ByteArrayToHexString(advBytes)), parsedBeacon);
            double parsedLatitude  = Int64.Parse(parsedBeacon.Id2.ToString().Substring(2), System.Globalization.NumberStyles.HexNumber) / 10000.0 - 90.0;
            double parsedLongitude = Int64.Parse(parsedBeacon.Id3.ToString().Substring(2), System.Globalization.NumberStyles.HexNumber) / 10000.0 - 180.0;

            long encodedLatitude = (long)((latitude + 90) * 10000.0);

            AssertEx.AreEqual("encoded latitude hex should match", string.Format("0x{0:x8}", encodedLatitude).ToLowerInvariant(), parsedBeacon.Id2.ToString().ToLowerInvariant());
            AssertEx.AreEqual("device sequence num should be same", "0x000000000001", parsedBeacon.Id1.ToString());
            AssertEx.AreEqual("latitude should be about right", latitude, parsedLatitude, 0.0001);
            AssertEx.AreEqual("longitude should be about right", longitude, parsedLongitude, 0.0001);
        }
Example #5
0
        public void testBeaconDoesMatchRegionWithLongerIdentifierListWithSomeNull()
        {
            Beacon beacon = new Beacon.Builder().SetId1("1").SetId2("2").SetRssi(4)
                            .SetBeaconTypeCode(5).SetTxPower(6).SetBluetoothAddress("1:2:3:4:5:6").Build();
            Region region = new Region("myRegion", null, null, null);

            AssertEx.True("Beacon should match region with more identifers than the beacon, if the region identifiers are null", region.MatchesBeacon(beacon));
        }
Example #6
0
        public void TestCalculateAccuracyWithRssiEqualsPowerOnInternalProperties()
        {
            Beacon.DistanceCalculator = new ModelSpecificDistanceCalculator(null, null);
            var    beacon   = new Beacon.Builder().SetTxPower(-55).SetRssi(-55).Build();
            double distance = beacon.Distance;

            AssertEx.AreEqual("Distance should be one meter if mRssi is the same as power", 1.0, distance, 0.1);
        }
Example #7
0
        public void testBeaconDoesntMatchRegionWithLongerIdentifierList()
        {
            Beacon beacon = new Beacon.Builder().SetId1("1").SetId2("2").SetRssi(4)
                            .SetBeaconTypeCode(5).SetTxPower(6).SetBluetoothAddress("1:2:3:4:5:6").Build();
            Region region = new Region("myRegion", Identifier.Parse("1"), Identifier.Parse("2"), Identifier.Parse("3"));

            AssertEx.False("Beacon should not match region with more identifers than the beacon", region.MatchesBeacon(beacon));
        }
Example #8
0
        public void TestCalculateAccuracyWithRssiEqualsPowerOnInternalPropertiesAndRunningAverage()
        {
            var beacon = new Beacon.Builder().SetTxPower(-55).SetRssi(0).Build();

            beacon.SetRunningAverageRssi(-55);
            double distance = beacon.Distance;

            AssertEx.AreEqual("Distance should be one meter if mRssi is the same as power", 1.0, distance, 0.1);
        }
Example #9
0
        public void TestCanGetAdvertisementDataForUrlBeacon()
        {
            var beacon = new Beacon.Builder()
                         .SetManufacturer(0x0118)
                         .SetId1("02646576656c6f7065722e636f6d") // http://developer.com
                         .SetTxPower(-59)                        // The measured transmitter power at one meter in dBm
                         .Build();
            var p = new BeaconParser().
                    SetBeaconLayout("s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v");
            var bytes = p.GetBeaconAdvertisementData(beacon);

            AssertEx.AreEqual("First byte of url should be in position 3", 0x02, bytes[2]);
        }
Example #10
0
        public void StartAdvertising(UserDataModel userData)
        {
            Beacon beacon = new Beacon.Builder()
                            .SetId1(AppConstants.iBeaconAppUuid)
                            .SetId2(userData.Major)
                            .SetId3(userData.Minor)
                            .SetTxPower(-59)
                            .SetManufacturer(AppConstants.CompanyCoreApple)
                            .Build();

            BeaconParser beaconParser = new BeaconParser().SetBeaconLayout(AppConstants.iBeaconFormat);

            _beaconTransmitter = new BeaconTransmitter(_mainActivity, beaconParser);
            _beaconTransmitter.StartAdvertising(beacon);
        }
        public void testAllowsAccessToTelemetryBytes()
        {
            var telemetryFields = new List <Java.Lang.Long>();

            telemetryFields.Add(new Java.Lang.Long(0x01L));       // version
            telemetryFields.Add(new Java.Lang.Long(0x0212L));     // battery level
            telemetryFields.Add(new Java.Lang.Long(0x0313L));     // temperature
            telemetryFields.Add(new Java.Lang.Long(0x04142434L)); // pdu count
            telemetryFields.Add(new Java.Lang.Long(0x05152535L)); // uptime

            Beacon beaconWithTelemetry = new Beacon.Builder().SetId1("0x0102030405060708090a").SetId2("0x01020304050607").SetTxPower(-59).SetExtraDataFields(telemetryFields).Build();

            byte[] telemetryBytes = new EddystoneTelemetryAccessor().GetTelemetryBytes(beaconWithTelemetry);

            byte[] expectedBytes = { 0x20, 0x01, 0x02, 0x12, 0x03, 0x13, 0x04, 0x14, 0x24, 0x34, 0x05, 0x15, 0x25, 0x35 };
            AssertEx.AreEqual("Should be equal", ByteArrayToHexString(telemetryBytes), ByteArrayToHexString(expectedBytes));
        }
Example #12
0
        public void StartAdvertising()
        {
            Beacon beacon = new Beacon.Builder()
                            .SetId1(AppConstants.AppUUID)
                            .SetId2("2111")
                            .SetId3("3123")
                            .SetTxPower(-59)
                            .SetManufacturer(AppConstants.COMPANY_CODE_APPLE)
                            .Build();

            // iBeaconのバイト列フォーマットをBeaconParser(アドバタイズ時のバイト列定義)にセットする。
            BeaconParser beaconParser = new BeaconParser().SetBeaconLayout(AppConstants.IBEACON_FORMAT);

            // iBeaconの発信を開始する。
            _beaconTransmitter = new BeaconTransmitter(Application.Context, beaconParser);
            _beaconTransmitter.StartAdvertising(beacon);
        }
        /// <summary>
        /// iBeacon発信開始処理(UUID、Major、Minorを直指定して発信)
        /// </summary>
        /// <param name="uuid">UUID</param>
        /// <param name="major">Major</param>
        /// <param name="minor">Minor</param>
        public void StartTransmission(Guid uuid, ushort major, ushort minor, sbyte txPower)
        {
            // Android用のビーコン定義を作成
            Beacon beacon = new Beacon.Builder()
                            .SetId1(uuid.ToString())
                            .SetId2(major.ToString())
                            .SetId3(minor.ToString())
                            .SetTxPower(txPower)
                            .SetManufacturer(Const.COMPANY_CODE_APPLE)
                            .Build();

            // iBeaconのバイト列フォーマットをBeaconParser(アドバタイズ時のバイト列定義)にセットする。
            BeaconParser beaconParser = new BeaconParser().SetBeaconLayout(IBEACON_FORMAT);

            // iBeaconの発信を開始する。
            _beaconTransmitter = new BeaconTransmitter(Android.App.Application.Context, beaconParser);
            _beaconTransmitter.StartAdvertising(beacon);
        }
        public void testAllowsAccessToBase64EncodedTelemetryBytes()
        {
            var telemetryFields = new List <Java.Lang.Long>();

            telemetryFields.Add(new Java.Lang.Long(0x01L));       // version
            telemetryFields.Add(new Java.Lang.Long(0x0212L));     // battery level
            telemetryFields.Add(new Java.Lang.Long(0x0313L));     // temperature
            telemetryFields.Add(new Java.Lang.Long(0x04142434L)); // pdu count
            telemetryFields.Add(new Java.Lang.Long(0x05152535L)); // uptime

            Beacon beaconWithTelemetry = new Beacon.Builder().SetId1("0x0102030405060708090a").SetId2("0x01020304050607").SetTxPower(-59).SetExtraDataFields(telemetryFields).Build();

            byte[] telemetryBytes = new EddystoneTelemetryAccessor().GetTelemetryBytes(beaconWithTelemetry);

            String encodedTelemetryBytes = new EddystoneTelemetryAccessor().GetBase64EncodedTelemetry(beaconWithTelemetry);

            AssertEx.NotNull("Should not be null", telemetryBytes);
        }
Example #15
0
        public async void StartAdvertisingBeacons(UserDataModel userData)
        {
            await Task.Run(() =>
            {
                Beacon beacon = new Beacon.Builder()
                                .SetId1(AppConstants.iBeaconAppUuid)
                                .SetId2(userData.Major)
                                .SetId3(userData.Minor)
                                .SetTxPower(-59)
                                .SetManufacturer(AppConstants.CompanyCodeApple)
                                .Build();

                BeaconParser beaconParser = new BeaconParser().SetBeaconLayout(AppConstants.iBeaconFormat);

                _beaconTransmitter = new BeaconTransmitter(this, beaconParser);
                _beaconTransmitter.StartAdvertising(beacon);
            });
        }
Example #16
0
        public void StartAdvertising(UserDataModel userData)
        {
            // TODO 出力調整どうするか考える。

            Beacon beacon = new Beacon.Builder()
                            .SetId1(AppConstants.iBeaconAppUuid)
                            .SetId2(userData.Major)
                            .SetId3(userData.Minor)
                            .SetTxPower(-59)
                            .SetManufacturer(AppConstants.CompanyCoreApple)
                            .Build();

            // iBeaconのバイト列フォーマットをBeaconParser(アドバタイズ時のバイト列定義)にセットする。
            BeaconParser beaconParser = new BeaconParser().SetBeaconLayout(AppConstants.iBeaconFormat);

            // iBeaconの発信を開始する。
            _beaconTransmitter = new BeaconTransmitter(_mainActivity, beaconParser);
            _beaconTransmitter.StartAdvertising(beacon);
        }
Example #17
0
        public void StartAdvertising(UserData userData)
        {
            // TODO 出力調整どうするか考える。

            Beacon beacon = new Beacon.Builder()
                            .SetId1(AppConstants.AppUUID)
                            .SetId2(userData.Major)
                            .SetId3(userData.Minor)
                            .SetTxPower(-59)
                            .SetManufacturer(AppConstants.COMPANY_CODE_APPLE)
                            .Build();

            // iBeaconのバイト列フォーマットをBeaconParser(アドバタイズ時のバイト列定義)にセットする。
            BeaconParser beaconParser = new BeaconParser().SetBeaconLayout(AppConstants.IBEACON_FORMAT);

            // iBeaconの発信を開始する。
            _beaconTransmitter = new BeaconTransmitter(_mainActivity, beaconParser);
            _beaconTransmitter.StartAdvertising(beacon);
        }
Example #18
0
        public void TestCanParseLongDataTypeOfDifferentSize()
        {
            // Create a beacon parser
            var parser = new BeaconParser();

            parser.SetBeaconLayout("m:2-3=0118,i:4-7,p:8-8,d:9-16,d:18-21,d:22-25");

            // Generate sample beacon for test purpose.
            var sampleData = new List <Java.Lang.Long>();
            var now        = DateTimeOffset.Now.ToUnixTimeMilliseconds();

            sampleData.Add(new Java.Lang.Long(now));
            sampleData.Add(new Java.Lang.Long(1234L));
            sampleData.Add(new Java.Lang.Long(9876L));
            var beacon = new Beacon.Builder()
                         .SetManufacturer(0x0118)
                         .SetId1("02646576656c6f7065722e636f6d")
                         .SetTxPower(-59)
                         .SetDataFields(sampleData)
                         .Build();

            AssertEx.AreEqual("beacon contains a valid data on index 0", now, beacon.DataFields[0].LongValue());

            // Make byte array
            byte[] headerBytes = HexStringToByteArray("1bff1801");
            byte[] bodyBytes   = parser.GetBeaconAdvertisementData(beacon);
            byte[] bytes       = new byte[headerBytes.Length + bodyBytes.Length];
            Array.Copy(headerBytes, 0, bytes, 0, headerBytes.Length);
            Array.Copy(bodyBytes, 0, bytes, headerBytes.Length, bodyBytes.Length);

            // Try parsing the byte array
            Beacon parsedBeacon = parser.FromScanData(bytes, -59, null);

            AssertEx.AreEqual("parsed beacon should contain a valid data on index 0", now, parsedBeacon.DataFields[0].LongValue());
            AssertEx.AreEqual("parsed beacon should contain a valid data on index 1", Convert.ToInt64(1234L), parsedBeacon.DataFields[1].LongValue());
            AssertEx.AreEqual("parsed beacon should contain a valid data on index 2", Convert.ToInt64(9876L), parsedBeacon.DataFields[2].LongValue());
        }