btshell_api.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. API for btshell app
  2. -------------------
  3. "btshell" is one of the sample applications that come with Mynewt. It is a shell application which provides a basic
  4. interface to the host-side of the BLE stack. "btshell" includes all the possible roles (Central/Peripheral) and they may
  5. be run simultaneously. You can run btshell on a board and issue commands that make it behave as a central or a peripheral
  6. with different peers.
  7. **btshell** is a new application that uses shell subsystem introduced in Mynewt 1.1 and has updated commands and
  8. parameters names. Thanks to support for tab completion commands names are more descriptive and self-explanatory
  9. without requiring extensive typing.
  10. Highlighted below are some of the ways you can use the API to establish connections and discover services and
  11. characteristics from peer devices. For descriptions of the full API, go to the next sections on
  12. :doc:`btshell_GAP` and :doc:`btshell_GATT`.
  13. .. contents::
  14. :local:
  15. :depth: 2
  16. .. toctree::
  17. :hidden:
  18. :titlesonly:
  19. GAP <btshell_GAP>
  20. GATT <btshell_GATT>
  21. btshell_advdata
  22. Set device address.
  23. ~~~~~~~~~~~~~~~~~~~
  24. On startup, btshell has the following identity address configuration:
  25. - Public address: None
  26. - Random address: None
  27. The below ``set`` commands can be used to change the address configuration:
  28. ::
  29. set addr_type=public addr=<device-address>
  30. set addr_type=random addr=<device-address>
  31. For example:
  32. ::
  33. set addr_type=public addr=01:02:03:04:05:06
  34. set addr_type=random addr=c1:aa:bb:cc:dd:ee
  35. The address configuration can be viewed with the ``show-addr`` command, as follows:
  36. ::
  37. show-addr
  38. public_id_addr=01:02:03:04:05:06 random_id_addr=c1:aa:bb:cc:dd:ee
  39. Initiate a direct connection to a device
  40. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. In this case, your board is acting as a central and initiating a connection with another BLE device. The example
  42. assumes you know the address of the peer, either by scanning for available peers or because you have set up the peer
  43. yourself.
  44. .. code-block:: none
  45. :emphasize-lines: 1
  46. connect peer_addr=d4:f5:13:53:d2:43
  47. connection established; handle=1 our_ota_addr_type=0 our_ota_addr=0a:0b:0c:0d:0e:0f out_id_addr_type=0 our_id_addr=0a:0b:0c:0d:0e:0f peer_addr_type=0 peer_addr=43:d2:53:13:f5:d4 conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
  48. The ``handle=1`` in the output indicates that it is connection-1.
  49. Configure advertisements to include device name
  50. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  51. In this case, your board is acting as a peripheral.
  52. With Extended Advertising enabled (should be executed after advertise-configure):
  53. ::
  54. advertise-set-adv-data name=<your-device-name>
  55. With Extended Advertising disabled:
  56. ::
  57. set-adv-data name=<your-device-name>
  58. Begin sending undirected general advertisements
  59. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. In this case, your board is acting as a peripheral.
  61. With Extended Advertising enabled:
  62. ::
  63. advertise-configure connectable=1 legacy=1 scannable=1
  64. advertise-start
  65. With Extended Advertising disabled:
  66. ::
  67. advertise conn=und discov=gen
  68. Show established connections.
  69. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  70. ::
  71. gatt-show-conn
  72. Discover and display peer's services, characteristics, and descriptors.
  73. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  74. This is how you discover and then display the services of the peer you established earlier across connection-1.
  75. .. code-block:: none
  76. :emphasize-lines: 1,2
  77. gatt-discover-full conn=1
  78. gatt-show
  79. [ts=132425ssb, mod=64 level=2] CONNECTION: handle=1 addr=d4:f5:13:53:d2:43
  80. [ts=132428ssb, mod=64 level=2] start=1 end=5 uuid=0x1800
  81. [ts=132433ssb, mod=64 level=2] start=6 end=16 uuid=0x1808
  82. [ts=132437ssb, mod=64 level=2] start=17 end=31 uuid=0x180a
  83. [ts=132441ssb, mod=64 level=2] start=32 end=65535 uuid=00000000-0000-1000-1000000000000000
  84. Read an attribute belonging to the peer
  85. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  86. ::
  87. gatt-read conn=1 attr=21
  88. Write to an attribute belonging to the peer
  89. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. ::
  91. gatt-write conn=1 attr=3 value=0x01:0x02:0x03
  92. Perform a passive scan
  93. ~~~~~~~~~~~~~~~~~~~~~~
  94. This is how you tell your board to listen to all advertisements around it. The duration is specified in ms.
  95. ::
  96. scan duration=1000 passive=1 filter=no_wl