1.. _bluetooth_shell: 2 3Bluetooth Shell 4############### 5 6The Bluetooth Shell is an application based on the :ref:`shell_api` module. It offer a collection of 7commands made to easily interact with the Bluetooth stack. 8 9Bluetooth Shell Setup and Usage 10******************************* 11 12First you need to build and flash your board with the Bluetooth shell. For how to do that, see the 13:ref:`getting_started`. The Bluetooth shell itself is located in 14:zephyr_file:`tests/bluetooth/shell/`. 15 16When it's done, connect to the CLI using your favorite serial terminal application. You should see 17the following prompt: 18 19.. code-block:: console 20 21 uart:~$ 22 23For more details on general usage of the shell, see :ref:`shell_api`. 24 25The first step is enabling Bluetooth. To do so, use the :code:`bt init` command. The following 26message is printed to confirm Bluetooth has been initialized. 27 28.. code-block:: console 29 30 uart:~$ bt init 31 Bluetooth initialized 32 Settings Loaded 33 [00:02:26.771,148] <inf> fs_nvs: nvs_mount: 8 Sectors of 4096 bytes 34 [00:02:26.771,148] <inf> fs_nvs: nvs_mount: alloc wra: 0, fe8 35 [00:02:26.771,179] <inf> fs_nvs: nvs_mount: data wra: 0, 0 36 [00:02:26.777,984] <inf> bt_hci_core: hci_vs_init: HW Platform: Nordic Semiconductor (0x0002) 37 [00:02:26.778,015] <inf> bt_hci_core: hci_vs_init: HW Variant: nRF52x (0x0002) 38 [00:02:26.778,045] <inf> bt_hci_core: hci_vs_init: Firmware: Standard Bluetooth controller (0x00) Version 3.2 Build 99 39 [00:02:26.778,656] <inf> bt_hci_core: bt_init: No ID address. App must call settings_load() 40 [00:02:26.794,738] <inf> bt_hci_core: bt_dev_show_info: Identity: EB:BF:36:26:42:09 (random) 41 [00:02:26.794,769] <inf> bt_hci_core: bt_dev_show_info: HCI: version 5.3 (0x0c) revision 0x0000, manufacturer 0x05f1 42 [00:02:26.794,799] <inf> bt_hci_core: bt_dev_show_info: LMP: version 5.3 (0x0c) subver 0xffff 43 44 45Identities 46********** 47 48Identities are a Zephyr host concept, allowing a single physical device to behave like multiple 49logical Bluetooth devices. 50 51The shell allows the creation of multiple identities, to a maximum that is set by the Kconfig symbol 52:kconfig:option:`CONFIG_BT_ID_MAX`. To create a new identity, use :code:`bt id-create` command. You 53can then use it by selecting it with its ID :code:`bt id-select <id>`. Finally, you can list all the 54available identities with :code:`id-show`. 55 56Scan for devices 57**************** 58 59Start scanning by using the :code:`bt scan on` command. Depending on the environment you're in, you 60may see a lot of lines printed on the shell. To stop the scan, run :code:`bt scan off`, the 61scrolling should stop. 62 63Here is an example of what you can expect: 64 65.. code-block:: console 66 67 uart:~$ bt scan on 68 Bluetooth active scan enabled 69 [DEVICE]: CB:01:1A:2D:6E:AE (random), AD evt type 0, RSSI -78 C:1 S:1 D:0 SR:0 E:0 Prim: LE 1M, Secn: No packets, Interval: 0x0000 (0 us), SID: 0xff 70 [DEVICE]: 20:C2:EE:59:85:5B (random), AD evt type 3, RSSI -62 C:0 S:0 D:0 SR:0 E:0 Prim: LE 1M, Secn: No packets, Interval: 0x0000 (0 us), SID: 0xff 71 [DEVICE]: E3:72:76:87:2F:E8 (random), AD evt type 3, RSSI -74 C:0 S:0 D:0 SR:0 E:0 Prim: LE 1M, Secn: No packets, Interval: 0x0000 (0 us), SID: 0xff 72 [DEVICE]: 1E:19:25:8A:CB:84 (random), AD evt type 3, RSSI -67 C:0 S:0 D:0 SR:0 E:0 Prim: LE 1M, Secn: No packets, Interval: 0x0000 (0 us), SID: 0xff 73 [DEVICE]: 26:42:F3:D5:A0:86 (random), AD evt type 3, RSSI -73 C:0 S:0 D:0 SR:0 E:0 Prim: LE 1M, Secn: No packets, Interval: 0x0000 (0 us), SID: 0xff 74 [DEVICE]: 0C:61:D1:B9:5D:9E (random), AD evt type 3, RSSI -87 C:0 S:0 D:0 SR:0 E:0 Prim: LE 1M, Secn: No packets, Interval: 0x0000 (0 us), SID: 0xff 75 [DEVICE]: 20:C2:EE:59:85:5B (random), AD evt type 3, RSSI -66 C:0 S:0 D:0 SR:0 E:0 Prim: LE 1M, Secn: No packets, Interval: 0x0000 (0 us), SID: 0xff 76 [DEVICE]: 25:3F:7A:EE:0F:55 (random), AD evt type 3, RSSI -83 C:0 S:0 D:0 SR:0 E:0 Prim: LE 1M, Secn: No packets, Interval: 0x0000 (0 us), SID: 0xff 77 uart:~$ bt scan off 78 Scan successfully stopped 79 80As you can see, this can lead to a high number of results. To reduce that number and easily find a 81specific device, you can enable scan filters. There are four types of filters: by name, by RSSI, by 82address and by periodic advertising interval. To apply a filter, use the :code:`bt scan-set-filter` 83command followed by the type of filters. You can add multiple filters by using the commands again. 84 85For example, if you want to look only for devices with the name *test shell*: 86 87.. code-block:: console 88 89 uart:~$ bt scan-filter-set name "test shell" 90 91Or if you want to look for devices at a very close range: 92 93.. code-block:: console 94 95 uart:~$ bt scan-filter-set rssi -40 96 RSSI cutoff set at -40 dB 97 98Finally, if you want to remove all filters: 99 100.. code-block:: console 101 102 uart:~$ bt scan-filter-clear all 103 104You can use the command :code:`bt scan on` to create an *active* scanner, meaning that the scanner 105will ask the advertisers for more information by sending a *scan request* packet. Alternatively, you 106can create a *passive scanner* by using the :code:`bt scan passive` command, so the scanner will not 107ask the advertiser for more information. 108 109Connecting to a device 110********************** 111 112To connect to a device, you need to know its address and type of address and use the 113:code:`bt connect` command with the address and the type as arguments. 114 115Here is an example: 116 117.. code-block:: console 118 119 uart:~$ bt connect 52:84:F6:BD:CE:48 random 120 Connection pending 121 Connected: 52:84:F6:BD:CE:48 (random) 122 Remote LMP version 5.3 (0x0c) subversion 0xffff manufacturer 0x05f1 123 LE Features: 0x000000000001412f 124 LE PHY updated: TX PHY LE 2M, RX PHY LE 2M 125 LE conn param req: int (0x0018, 0x0028) lat 0 to 42 126 LE conn param updated: int 0x0028 lat 0 to 42 127 128You can list the active connections of the shell using the :code:`bt connections` command. The shell 129maximum number of connections is defined by :kconfig:option:`CONFIG_BT_MAX_CONN`. You can disconnect 130from a connection with the 131:code:`bt disconnect <address: XX:XX:XX:XX:XX:XX> <type: (public|random)>` command. 132 133.. note:: 134 135 If you were scanning just before, you can connect to the last scanned device by 136 simply running the :code:`bt connect` command. 137 138 Alternatively, you can use the :code:`bt connect-name <name>` command to automatically 139 enable scanning with a name filter and connect to the first match. 140 141Advertising 142*********** 143 144Begin advertising by using the :code:`bt advertise on` command. This will use the default parameters 145and advertise a resolvable private address with the name of the device. You can choose to use the 146identity address instead by running the :code:`bt advertise on identity` command. To stop 147advertising use the :code:`bt advertise off` command. 148 149To enable more advanced features of advertising, you should create an advertiser using the 150:code:`bt adv-create` command. Parameters for the advertiser can be passed either at the creation of 151it or by using the :code:`bt adv-param` command. To begin advertising with this newly created 152advertiser, use the :code:`bt adv-start` command, and then the :code:`bt adv-stop` command to stop 153advertising. 154 155When using the custom advertisers, you can choose if it will be connectable or scannable. This leads 156to four options: :code:`conn-scan`, :code:`conn-nscan`, :code:`nconn-scan` and :code:`nconn-nscan`. 157Those parameters are mandatory when creating an advertiser or updating its parameters. 158 159For example, if you want to create a connectable and scannable advertiser and start it: 160 161.. code-block:: console 162 163 uart:~$ bt adv-create conn-scan 164 Created adv id: 0, adv: 0x200022f0 165 uart:~$ bt adv-start 166 Advertiser[0] 0x200022f0 set started 167 168You may notice that with this, the custom advertiser does not advertise the device name; you need to 169enable it. Continuing from the previous example: 170 171.. code-block:: console 172 173 uart:~$ bt adv-stop 174 Advertiser set stopped 175 uart:~$ bt adv-param conn-scan name 176 uart:~$ bt adv-start 177 Advertiser[0] 0x200022f0 set started 178 179You should now see the name of the device in the advertising data. You can also set the advertising 180data manually by using the :code:`bt adv-data` command. The following example shows how to set the 181advertiser name with it: 182 183.. code-block:: console 184 185 uart:~$ bt adv-create conn-scan 186 Created adv id: 0, adv: 0x20002348 187 uart:~$ bt adv-data 1009426C7565746F6F74682D5368656C6C 188 uart:~$ bt adv-start 189 Advertiser[0] 0x20002348 set started 190 191The data must be formatted according to the Bluetooth Core Specification (see version 5.3, vol. 3, 192part C, 11). In this example, the first octet is the size of the data (the data and one octet for 193the data type), the second one is the type of data, ``0x09`` is the Complete Local Name and the 194remaining data are the name in ASCII. So, on the other device you should see the name 195*Bluetooth-Shell*. 196 197When advertising, if others devices use an *active* scanner, you may receive *scan request* packets. 198To visualize those packets, you can add :code:`scan-reports` to the parameters of your advertiser. 199 200Directed Advertising 201==================== 202 203It is possible to use directed advertising on the shell if you want to reconnect to a device. The 204following example demonstrates how to create a directed advertiser with the address specified right 205after the parameter :code:`directed`. The :code:`low` parameter indicates that we want to use the 206low duty cycle mode, and the :code:`dir-rpa` parameter is required if the remote device is 207privacy-enabled and supports address resolution of the target address in directed advertisement. 208 209.. code-block:: console 210 211 uart:~$ bt adv-create conn-scan directed D7:54:03:CE:F3:B4 random low dir-rpa 212 Created adv id: 0, adv: 0x20002348 213 214After that, you can start the advertiser and then the target device will be able to reconnect. 215 216Extended Advertising 217==================== 218 219Let's now have a look at some extended advertising features. To enable extended advertising, use the 220`ext-adv` parameter. 221 222.. code-block:: console 223 224 uart:~$ bt adv-create conn-nscan ext-adv name-ad 225 Created adv id: 0, adv: 0x200022f0 226 uart:~$ bt adv-start 227 Advertiser[0] 0x200022f0 set started 228 229This will create an extended advertiser, that is connectable and non-scannable. 230 231Filter Accept List 232****************** 233 234It's possible to create a list of allowed addresses that can be used to 235connect to those addresses automatically. Here is how to do it: 236 237.. code-block:: console 238 239 uart:~$ bt fal-add 47:38:76:EA:29:36 random 240 uart:~$ bt fal-add 66:C8:80:2A:05:73 random 241 uart:~$ bt fal-connect on 242 243The shell will then connect to the first available device. In the example, if both devices are 244advertising at the same time, we will connect to the first address added to the list. 245 246The Filter Accept List can also be used for scanning or advertising by using the option :code:`fal`. 247For example, if we want to scan for a bunch of selected addresses, we can set up a Filter Accept 248List: 249 250.. code-block:: console 251 252 uart:~$ bt fal-add 65:4B:9E:83:AF:73 random 253 uart:~$ bt fal-add 73:72:82:B4:8F:B9 random 254 uart:~$ bt fal-add 5D:85:50:1C:72:64 random 255 uart:~$ bt scan on fal 256 257You should see only those three addresses reported by the scanner. 258 259Enabling security 260***************** 261 262When connected to a device, you can enable multiple levels of security, here is the list for 263Bluetooth LE: 264 265* **1** No encryption and no authentication; 266* **2** Encryption and no authentication; 267* **3** Encryption and authentication; 268* **4** Bluetooth LE Secure Connection. 269 270To enable security, use the :code:`bt security <level>` command. For levels requiring authentication 271(level 3 and above), you must first set the authentication method. To do it, you can use the 272:code:`bt auth all` command. After that, when you will set the security level, you will be asked to 273confirm the passkey on both devices. On the shell side, do it with the command 274:code:`bt auth-passkey-confirm`. 275 276Pairing 277======= 278 279Enabling authentication requires the devices to be bondable. By default the shell is bondable. You 280can make the shell not bondable using :code:`bt bondable off`. You can list all the devices you are 281paired with using the command :code:`bt bonds`. 282 283The maximum number of paired devices is set using :kconfig:option:`CONFIG_BT_MAX_PAIRED`. You can 284remove a paired device using :code:`bt clear <address: XX:XX:XX:XX:XX:XX> <type: (public|random)>` 285or remove all paired devices with the command :code:`bt clear all`. 286 287GATT 288**** 289 290The following examples assume that you have two devices already connected. 291 292To perform service discovery on the client side, use the :code:`gatt discover` command. This should 293print all the services that are available on the GATT server. 294 295On the server side, you can register pre-defined test services using the :code:`gatt register` 296command. When done, you should see the newly added services on the client side when running the 297discovery command. 298 299You can now subscribe to those new services on the client side. Here is an example on how to 300subscribe to the test service: 301 302.. code-block:: console 303 304 uart:~$ gatt subscribe 26 25 305 Subscribed 306 307The server can now notify the client with the command :code:`gatt notify`. 308 309Another option available through the GATT command is initiating the MTU exchange. To do it, use the 310:code:`gatt exchange-mtu` command. To update the shell maximum MTU, you need to update Kconfig 311symbols in the configuration file of the shell. For more details, see 312:ref:`bluetooth_mtu_update_sample`. 313 314L2CAP 315***** 316 317The :code:`l2cap` command exposes parts of the L2CAP API. The following example shows how to 318register a LE PSM, connect to it from another device and send 3 packets of 14 octets each. 319 320The example assumes that the two devices are already connected. 321 322On device A, register the LE PSM: 323 324.. code-block:: console 325 326 uart:~$ l2cap register 29 327 L2CAP psm 41 sec_level 1 registered 328 329On device B, connect to the registered LE PSM and send data: 330 331.. code-block:: console 332 333 uart:~$ l2cap connect 29 334 Chan sec: 1 335 L2CAP connection pending 336 Channel 0x20000210 connected 337 Channel 0x20000210 status 1 338 uart:~$ l2cap send 3 14 339 Rem 2 340 Rem 1 341 Rem 0 342 Outgoing data channel 0x20000210 transmitted 343 Outgoing data channel 0x20000210 transmitted 344 Outgoing data channel 0x20000210 transmitted 345 346On device A, you should have received the data: 347 348.. code-block:: console 349 350 Incoming conn 0x20002398 351 Channel 0x20000210 status 1 352 Channel 0x20000210 connected 353 Channel 0x20000210 requires buffer 354 Incoming data channel 0x20000210 len 14 355 00000000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff |........ ...... | 356 Channel 0x20000210 requires buffer 357 Incoming data channel 0x20000210 len 14 358 00000000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff |........ ...... | 359 Channel 0x20000210 requires buffer 360 Incoming data channel 0x20000210 len 14 361 00000000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff |........ ...... | 362 363Logging 364******* 365 366You can configure the logging level per module at runtime. This depends on the maximum logging level 367that is compiled in. To configure, use the :code:`log` command. Here are some examples: 368 369* List the available modules and their current logging level 370 371.. code-block:: console 372 373 uart:~$ log status 374 375* Disable logging for *bt_hci_core* 376 377.. code-block:: console 378 379 uart:~$ log disable bt_hci_core 380 381* Enable error logs for *bt_att* and *bt_smp* 382 383.. code-block:: console 384 385 uart:~$ log enable err bt_att bt_smp 386 387* Disable logging for all modules 388 389.. code-block:: console 390 391 uart:~$ log disable 392 393* Enable warning logs for all modules 394 395.. code-block:: console 396 397 uart:~$ log enable wrn 398