Last active
October 4, 2015 17:49
-
-
Save sundeepgupta/feb4ec5282a9489fed74 to your computer and use it in GitHub Desktop.
Algorithm to generate the Major and Minor values for the iBeacon spec.
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
class BeaconIdGenerator | |
MAX_MAJOR_MINOR = 65536 | |
MAX_VALUE = MAX_MAJOR_MINOR**2 - 1 | |
def self.perform(value) | |
raise "Value passed in must be an integer." unless value.is_a? Integer | |
raise "Value passed in must be at least 0." if value < 0 | |
raise "Value passed in must be at most #{MAX_VALUE}" if value > MAX_VALUE | |
major = (value/MAX_MAJOR_MINOR).floor | |
minor = value % MAX_MAJOR_MINOR | |
{ major: major, minor: minor } | |
end | |
end | |
__END__ | |
iBeacon major and minor values are 16-bit unsigned integers thus range from 0 through 65,535. | |
Therefore there are 4,294,967,296 possible combinations. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment