bluetooth.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2024 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from .. import PebbleCommander, exceptions, parsers
  15. @PebbleCommander.command()
  16. def bt_airplane_mode(cmdr, enter=True):
  17. """ Enter or exit airplane mode.
  18. `enter` should either be a boolean, "enter", or "exit".
  19. """
  20. if parsers.str2bool(enter, also_true=["enter"], also_false=["exit"]):
  21. enter = "enter"
  22. else:
  23. enter = "exit"
  24. ret = cmdr.send_prompt_command("bt airplane mode %s" % enter)
  25. if ret:
  26. raise exceptions.PromptResponseError(ret)
  27. @PebbleCommander.command()
  28. def bt_prefs_wipe(cmdr):
  29. """ Wipe bluetooth preferences.
  30. """
  31. ret = cmdr.send_prompt_command("bt prefs wipe")
  32. if ret:
  33. raise exceptions.PromptResponseError(ret)
  34. @PebbleCommander.command()
  35. def bt_mac(cmdr):
  36. """ Get the bluetooth MAC address.
  37. """
  38. ret = cmdr.send_prompt_command("bt mac")
  39. if not ret[0].startswith("0x"):
  40. raise exceptions.PromptResponseError(ret)
  41. retstr = ret[0][2:]
  42. return [':'.join(retstr[i:i+2] for i in range(0, len(retstr), 2))]
  43. @PebbleCommander.command()
  44. def bt_set_addr(cmdr, new_mac=None):
  45. """ Set the bluetooth MAC address.
  46. Don't specify `new_mac` to revert to default.
  47. `new_mac` should be of the normal 6 hex octets split with colons.
  48. """
  49. if not new_mac:
  50. new_mac = "00:00:00:00:00:00"
  51. mac = parsers.str2mac(new_mac)
  52. macstr = ''.join(["%02X" % byte for byte in mac])
  53. ret = cmdr.send_prompt_command("bt set addr %s" % macstr)
  54. if ret[0] != new_mac:
  55. raise exceptions.PromptResponseError(ret)
  56. @PebbleCommander.command()
  57. def bt_set_name(cmdr, new_name=None):
  58. """ Set the bluetooth name.
  59. """
  60. if not new_name:
  61. new_name = ""
  62. # Note: the only reason for this is because prompt sucks
  63. # This can probably be removed when prompt goes away
  64. if ' ' in new_name:
  65. raise exceptions.ParameterError("bluetooth name must not have spaces")
  66. ret = cmdr.send_prompt_command("bt set name %s" % new_name)
  67. if ret:
  68. raise exceptions.PromptResponseError(ret)