DMIBeaconBluetoothTag.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Linq;
  2. namespace InABox.DigitalMatter
  3. {
  4. public class DMIBeaconBluetoothTag : DMBluetoothTag
  5. {
  6. public byte[] UUID { get; set; }
  7. public ushort MajorID { get; set; }
  8. public ushort MinorID { get; set; }
  9. public sbyte TxPower { get; set; }
  10. public byte[] MacAddress { get; set; }
  11. public override string ID()
  12. {
  13. return MacAddress != null
  14. ? string.Join(":", MacAddress.Reverse().Select(x => x.ToString("X2")))
  15. : string.Format("{0}.{1}", MajorID, MinorID);
  16. }
  17. protected override void DoDecode(IDMReadBuffer buffer)
  18. {
  19. UUID = buffer.TakeBytes(16);
  20. MajorID = buffer.TakeUInt16();
  21. MinorID = buffer.TakeUInt16();
  22. TxPower = buffer.TakeInt8();
  23. if (DataLength == 27)
  24. MacAddress = buffer.TakeBytes(6);
  25. }
  26. protected override void BeforeEncode(IDMWriteBuffer buffer)
  27. {
  28. base.BeforeEncode(buffer);
  29. DataLength = (byte)(MacAddress == null ? 21 : 27);
  30. }
  31. protected override void DoEncode(IDMWriteBuffer buffer)
  32. {
  33. buffer.AddBytes(UUID);
  34. buffer.AddUInt16(MajorID);
  35. buffer.AddUInt16(MinorID);
  36. buffer.AddInt8(TxPower);
  37. if (MacAddress != null)
  38. buffer.AddBytes(MacAddress);
  39. }
  40. public override string Type()
  41. {
  42. return "iBeacon";
  43. }
  44. }
  45. }