Skip to content

Instantly share code, notes, and snippets.

@sundeepgupta
Last active October 4, 2015 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sundeepgupta/feb4ec5282a9489fed74 to your computer and use it in GitHub Desktop.
Save sundeepgupta/feb4ec5282a9489fed74 to your computer and use it in GitHub Desktop.
Algorithm to generate the Major and Minor values for the iBeacon spec.
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