> For the complete documentation index, see [llms.txt](https://amobile-solutions.gitbook.io/yocto-developer-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://amobile-solutions.gitbook.io/yocto-developer-guide/test-and-use-the-interface/bluetooth.md).

# Bluetooth

{% hint style="info" %}
The following instructions utilize the SD510 for demonstration purposes. These procedures are applicable to other development boards.
{% endhint %}

## Bluetooth boot messages

During boot the following Bluetooth kernel messages are received:

```
root@sd510:~# dmesg | grep -i Bluetooth
[    5.050007] Bluetooth: Core ver 2.22
[    5.050085] NET: Registered PF_BLUETOOTH protocol family
[    5.050090] Bluetooth: HCI device and connection manager initialized
[    5.050101] Bluetooth: HCI socket layer initialized
[    5.050107] Bluetooth: L2CAP socket layer initialized
[    5.050117] Bluetooth: SCO socket layer initialized
[   11.202967] Bluetooth: hci0: Firmware already downloaded
[   11.210825] Bluetooth: hci0: Device setup in 10813 usecs
```

## Device identification

After the File system is up 'hciconfig' should show the connected bluetooth module.

```
root@sd510:~# hciconfig hci0 up
root@sd510:~# hciconfig
hci0:   Type: Primary  Bus: SDIO
        BD Address: 00:00:46:76:63:01  ACL MTU: 1021:6  SCO MTU: 244:4
        UP RUNNING
        RX bytes:2180 acl:0 sco:0 events:107 errors:0
        TX bytes:1814 acl:0 sco:0 commands:107 errors:0
```

The hcitool can be used to show the Bluetooth MAC address of the Bluetooth module with the respective hci connection.

```
root@sd510:~# hcitool dev
Devices:
        hci0    00:00:46:76:63:01
```

Type help to show commands.

```
root@sd510:~# hcitool --help
hcitool - HCI Tool ver 5.65
Usage:
        hcitool [options] <command> [command parameters]
Options:
        --help  Display help
        -i dev  HCI device
Commands:
        dev     Display local devices
        inq     Inquire remote devices
        scan    Scan for remote devices
        name    Get name from remote device
        info    Get information from remote device
        spinq   Start periodic inquiry
        epinq   Exit periodic inquiry
        cmd     Submit arbitrary HCI commands
        con     Display active connections
        cc      Create connection to remote device
        dc      Disconnect from remote device
        sr      Switch central/peripheral role
        cpt     Change connection packet type
        rssi    Display connection RSSI
        lq      Display link quality
        tpl     Display transmit power level
        afh     Display AFH channel map
        lp      Set/display link policy settings
        lst     Set/display link supervision timeout
        auth    Request authentication
        enc     Set connection encryption
        key     Change connection link key
        clkoff  Read clock offset
        clock   Read local or remote clock
        lescan  Start LE scan
        leinfo  Get LE remote information
        lealadd Add device to LE Accept List
        lealrm  Remove device from LE Accept List
        lealsz  Read size of LE Accept List
        lealclr Clear LE Accept List
        lewladd Deprecated. Use lealadd instead.
        lewlrm  Deprecated. Use lealrm instead.
        lewlsz  Deprecated. Use lealsz instead.
        lewlclr Deprecated. Use lealclr instead.
        lerladd Add device to LE Resolving List
        lerlrm  Remove device from LE Resolving List
        lerlclr Clear LE Resolving List
        lerlsz  Read size of LE Resolving List
        lerlon  Enable LE Address Resolution
        lerloff Disable LE Address Resolution
        lecc    Create a LE Connection
        ledc    Disconnect a LE Connection
        lecup   LE Connection Update

For more information on the usage of each command use:
        hcitool <command> --help
```

## Managing bluetooth

You can manage bluetooth with bluetoothctl, type 'bluetoothctl' and enter the interface. Type help to show commands.\
For connecting a device do the following:<br>

```
# bluetoothctl
[bluetooth]# power on
[bluetooth]# agent off
[bluetooth]# agent on
[bluetooth]# default-agent
[bluetooth]# pairable on

Push the connect button in the device

[bluetooth]# scan on

Copy mac address

[bluetooth]# scan off
[bluetooth]# pair <mac address>

Approve pairing on Device if required

[bluetooth]# trust <mac address>
[bluetooth]# connect <mac address>
[bluetooth]# quit
```

For example when connecting a Logitech bluetooth mouse the following is done:

```
root@sd510:~# bluetoothctl
Agent registered
[CHG] Controller 00:00:46:76:63:01 Pairable: yes
[bluetooth]# scan on
Discovery started
[CHG] Controller 00:00:46:76:63:01 Discovering: yes
[NEW] Device 6C:72:63:B6:8C:40 6C-72-63-B6-8C-40
[NEW] Device 42:EA:42:6F:31:B5 42-EA-42-6F-31-B5
[NEW] Device 74:14:1F:B8:6B:45 74-14-1F-B8-6B-45
...
[NEW] Device 5A:B4:BD:0E:7D:32 5A-B4-BD-0E-7D-32
[NEW] Device E7:1B:4D:3F:84:FB MX Anywhere 2S
...
[CHG] Device A0:A4:C5:22:1F:63 RSSI: -77
[bluetooth]# pair E7:1B:4D:3F:84:FB
Attempting to pair with E7:1B:4D:3F:84:FB
[CHG] Device E7:1B:4D:3F:84:FB Connected: yes
[CHG] Device E7:1B:4D:3F:84:FB Bonded: yes
[CHG] Device E7:1B:4D:3F:84:FB UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device E7:1B:4D:3F:84:FB UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device E7:1B:4D:3F:84:FB UUIDs: 0000180a-0000-1000-8000-00805f9b34fb
[CHG] Device E7:1B:4D:3F:84:FB UUIDs: 0000180f-0000-1000-8000-00805f9b34fb
[CHG] Device E7:1B:4D:3F:84:FB UUIDs: 00001812-0000-1000-8000-00805f9b34fb
[CHG] Device E7:1B:4D:3F:84:FB UUIDs: 00010000-0000-1000-8000-011f2000046d
[CHG] Device E7:1B:4D:3F:84:FB ServicesResolved: yes
[CHG] Device E7:1B:4D:3F:84:FB Paired: yes
[NEW] Primary Service (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service0008
        00001801-0000-1000-8000-00805f9b34fb
        Generic Attribute Profile
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service0008/char0009
        00002a05-0000-1000-8000-00805f9b34fb
        Service Changed
[NEW] Descriptor (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service0008/char0009/desc000b
        00002902-0000-1000-8000-00805f9b34fb
        Client Characteristic Configuration
[NEW] Primary Service (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service000c
        0000180a-0000-1000-8000-00805f9b34fb
        Device Information
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service000c/char000d
        00002a29-0000-1000-8000-00805f9b34fb
        Manufacturer Name String
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service000c/char000f
        00002a24-0000-1000-8000-00805f9b34fb
        Model Number String
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service000c/char0011
        00002a25-0000-1000-8000-00805f9b34fb
        Serial Number String
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service000c/char0013
        00002a27-0000-1000-8000-00805f9b34fb
        Hardware Revision String
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service000c/char0015
        00002a26-0000-1000-8000-00805f9b34fb
        Firmware Revision String
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service000c/char0017
        00002a28-0000-1000-8000-00805f9b34fb
        Software Revision String
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service000c/char0019
        00002a50-0000-1000-8000-00805f9b34fb
        PnP ID
[NEW] Primary Service (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service001b
        0000180f-0000-1000-8000-00805f9b34fb
        Battery Service
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service001b/char001c
        00002a19-0000-1000-8000-00805f9b34fb
        Battery Level
[NEW] Descriptor (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service001b/char001c/desc001e
        00002902-0000-1000-8000-00805f9b34fb
        Client Characteristic Configuration
[NEW] Primary Service (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service003f
        00010000-0000-1000-8000-011f2000046d
        Vendor specific
[NEW] Characteristic (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service003f/char0040
        00010001-0000-1000-8000-011f2000046d
        Vendor specific
[NEW] Descriptor (Handle 0x0000)
        /org/bluez/hci0/dev_E7_1B_4D_3F_84_FB/service003f/char0040/desc0042
        00002902-0000-1000-8000-00805f9b34fb
        Client Characteristic Configuration
Pairing successful
[bluetooth]#
[bluetooth]# quit
```

Using evtest to Inspect HID Events

```
root@sd510:/dev/input# evtest
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0:      mtk-pmic-keys
/dev/input/event1:      mt8370-evk HDMI Jack
/dev/input/event2:      MX Anywhere 2S Keyboard
/dev/input/event3:      MX Anywhere 2S Mouse
```
