-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from kobush/master
Adds ProximityBeaconFrame. Thanks for the pull request and the high quality code! Sorry that it took quite some time, but there should be no issues with the code that I can see! Will update the lib on NuGet + the example app in the store as soon as possible.
- Loading branch information
Showing
9 changed files
with
217 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System; | ||
|
||
namespace UniversalBeaconLibrary.Beacon | ||
{ | ||
public class ProximityBeaconFrame : BeaconFrameBase | ||
{ | ||
public const ushort CompanyId = 0x004C; | ||
public const byte DataType = 0x02; | ||
public const byte DataLength = 0x15; | ||
|
||
private Guid _uuid; | ||
private ushort _major; | ||
private ushort _minor; | ||
private sbyte _txPower; | ||
|
||
public ProximityBeaconFrame(byte[] payload) | ||
: base(payload) | ||
{ | ||
ParsePayload(); | ||
} | ||
|
||
public string UuidAsString => Uuid.ToString("D"); | ||
|
||
public Guid Uuid | ||
{ | ||
get { return _uuid; } | ||
set | ||
{ | ||
if (_uuid == value) return; | ||
_uuid = value; | ||
OnPropertyChanged(); | ||
OnPropertyChanged(nameof(UuidAsString)); | ||
} | ||
} | ||
|
||
public string MajorAsString => Major.ToString("x4"); | ||
|
||
public ushort Major | ||
{ | ||
get { return _major; } | ||
set | ||
{ | ||
if (_major == value) return; | ||
_major = value; | ||
OnPropertyChanged(); | ||
OnPropertyChanged(nameof(MajorAsString)); | ||
} | ||
} | ||
|
||
public string MinorAsString => Minor.ToString("x4"); | ||
|
||
public ushort Minor | ||
{ | ||
get { return _minor; } | ||
set | ||
{ | ||
if (_minor == value) return; | ||
_minor = value; | ||
OnPropertyChanged(); | ||
OnPropertyChanged(nameof(MinorAsString)); | ||
} | ||
} | ||
|
||
public sbyte TxPower | ||
{ | ||
get { return _txPower; } | ||
set | ||
{ | ||
if (_txPower == value) return; | ||
_txPower = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
private void ParsePayload() | ||
{ | ||
var dataType = Payload[0]; | ||
var dataLength = Payload[1]; | ||
|
||
if (dataType != DataType || dataLength != DataLength) | ||
throw new InvalidOperationException(); | ||
|
||
// need to swap to big-endian | ||
var uuidBytes = new byte[16]; | ||
if (BitConverter.IsLittleEndian) | ||
{ | ||
Array.ConstrainedCopy(Payload, 2, uuidBytes, 0, 16); | ||
Array.Reverse(uuidBytes, 0, 4); | ||
Array.Reverse(uuidBytes, 4, 2); | ||
Array.Reverse(uuidBytes, 6, 2); | ||
} | ||
Uuid = new Guid(uuidBytes); | ||
|
||
var majorBytes = new byte[2]; | ||
Array.ConstrainedCopy(Payload, 18, majorBytes, 0, 2); | ||
if (BitConverter.IsLittleEndian) | ||
Array.Reverse(majorBytes); | ||
Major = BitConverter.ToUInt16(majorBytes,0); | ||
|
||
var minorBytes = new byte[2]; | ||
Array.ConstrainedCopy(Payload, 20, minorBytes, 0, 2); | ||
if (BitConverter.IsLittleEndian) | ||
Array.Reverse(minorBytes); | ||
Minor = BitConverter.ToUInt16(minorBytes, 0); | ||
|
||
TxPower = (sbyte) Payload[22]; | ||
} | ||
|
||
public override void Update(BeaconFrameBase otherFrame) | ||
{ | ||
base.Update(otherFrame); | ||
ParsePayload(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters