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); }
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); }
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); }
public void TestReEncodesLittleEndianBeacon() { var bytes = HexStringToByteArray("02011a1bff1801beac0102030405060708090a0b0c0d0e0f1011121314c509"); var parser = new BeaconParser(); parser.SetBeaconLayout("m:2-3=beac,i:4-9,i:10-15l,i:16-23,p:24-24,d:25-25"); var beacon = parser.FromScanData(bytes, -55, null); var regeneratedBytes = parser.GetBeaconAdvertisementData(beacon); var expectedMatch = Java.Util.Arrays.CopyOfRange(bytes, 7, bytes.Length); AssertEx.AreEqual("beacon advertisement bytes should be the same after re-encoding", ByteArrayToHexString(expectedMatch), ByteArrayToHexString(regeneratedBytes)); }
public void TestReEncodesBeaconForEddystoneTelemetry() { var bytes = HexStringToByteArray("0201060303aafe1516aafe2001021203130414243405152535"); var parser = new BeaconParser(); parser.SetBeaconLayout(BeaconParser.EddystoneTlmLayout); var beacon = parser.FromScanData(bytes, -55, null); var regeneratedBytes = parser.GetBeaconAdvertisementData(beacon); var expectedMatch = Java.Util.Arrays.CopyOfRange(bytes, 11, bytes.Length); AssertEx.AreEqual("beacon advertisement bytes should be the same after re-encoding", ByteArrayToHexString(expectedMatch), ByteArrayToHexString(regeneratedBytes)); }
public void TestReEncodesBeacon() { var bytes = HexStringToByteArray("02011a1bff1801beac2f234454cf6d4a0fadf2f4911ba9ffa600010002c509"); var parser = new BeaconParser(); parser.SetBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"); var beacon = parser.FromScanData(bytes, -55, null); var regeneratedBytes = parser.GetBeaconAdvertisementData(beacon); var expectedMatch = Java.Util.Arrays.CopyOfRange(bytes, 7, bytes.Length); AssertEx.AreEqual("beacon advertisement bytes should be the same after re-encoding", expectedMatch, regeneratedBytes); }
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]); }
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()); }