Skip to content

Instantly share code, notes, and snippets.

@neutralvibes
Last active May 26, 2022 16:59
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 neutralvibes/0a91b81873720f146361e526e32c8dab to your computer and use it in GitHub Desktop.
Save neutralvibes/0a91b81873720f146361e526e32c8dab to your computer and use it in GitHub Desktop.
Parse a bluetooth class integer in javascript

Parse a bluetooth class in javascript

Translates a bluetooth class integer into its component parts

/**
 * Parses a bluetooth class integer into major class and service list
 * 
 */
function parseBlueClass (classInt) {
  const mc_list = [
    "Miscellaneous",
    "Computer",
    "Phone",
    "LAN/Network Access Point",
    "Audio/Video",
    "Peripheral",
    "Imaging"
  ]
  const s_list = [
    [16, "positioning"],
    [17, "networking"],
    [18, "rendering"],
    [19, "capturing"],
    [20, "object transfer"],
    [21, "audio"],
    [22, "telephony"],
    [23, "information"]
  ]
  let mi = (classInt >> 8) & 0xf
  let major = (mi < 7) ? mc_list[mi] : 'Unknown'

  let services = []
  s_list.forEach(s => {
    if (classInt & (1 << (s[0] - 1)))
      services.push(s[1])
  })

  return {major, services}
}

Usage

parseBlueClass(5898764)

Returns

{
    "major": "Phone",
    "services": [
        "rendering",
        "object transfer",
        "audio",
        "information"
    ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment