ubuntu bluetooth

Linux : 2019. 9. 18. 14:03
반응형

https://docs.ubuntu.com/core/en/stacks/bluetooth/bluez/docs/bluetooth-on-ubuntu-core

 

Bluetooth on Ubuntu Core | Bluetooth documentation

Ubuntu Core Bluetooth Interfaces Bluetooth on Ubuntu Core is provided by the BlueZ stack which is an official Linux Bluetooth protocol stack. The lower-level part of it comes with the kernel snap while the user-space portion can be installed as a separate

docs.ubuntu.com

https://docs.ubuntu.com/core/en/stacks/bluetooth/bluez/docs/

 

About BlueZ | Bluetooth documentation

About Bluetooth Bluetooth is a standard for wireless communication on short distances. It standarized multiple profiles for different use-cases such as music streaming, serial connections, message exchange, phone calls, and many others. It has been first p

docs.ubuntu.com

$ sudo systemctl enable bluetooth

$sudo systemctl restart bleutooth

$ systemctl status bluetooth
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-09-16 07:36:16 KST; 2 days ago
     Docs: man:bluetoothd(8)
 Main PID: 1232 (bluetoothd)
   Status: "Running"
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/bluetooth.service
           └─1232 /usr/lib/bluetooth/bluetoothd

 9월 16 07:36:16 lionelj-x3900 systemd[1]: Starting Bluetooth service...
 9월 16 07:36:16 lionelj-x3900 bluetoothd[1232]: Bluetooth daemon 5.48
 9월 16 07:36:16 lionelj-x3900 bluetoothd[1232]: Starting SDP server
 9월 16 07:36:16 lionelj-x3900 systemd[1]: Started Bluetooth service.
 9월 16 07:36:16 lionelj-x3900 bluetoothd[1232]: Bluetooth management interface 1.14 initialized
 9월 16 07:36:26 lionelj-x3900 bluetoothd[1232]: Endpoint registered: sender=:1.58 path=/MediaEndpoint/A2DPSource
 9월 16 07:36:26 lionelj-x3900 bluetoothd[1232]: Endpoint registered: sender=:1.58 path=/MediaEndpoint/A2DPSink
$ bluetoothctl 
[NEW] Controller 00:1A:7D:DA:71:11 lionelj-x3900 [default]
[NEW] Device A0:60:90:16:4D:23 Level Box Slim (4D23)
Agent registered
[bluetooth]# 

[bluetooth]# help
Menu main:
Available commands:
-------------------
advertise                                         Advertise Options Submenu
scan                                              Scan Options Submenu
gatt                                              Generic Attribute Submenu
list                                              List available controllers
show [ctrl]                                       Controller information
select                                      Select default controller
devices                                           List available devices
paired-devices                                    List paired devices
system-alias                                Set controller alias
reset-alias                                       Reset controller alias
power <on/off>                                    Set controller power
pairable <on/off>                                 Set controller pairable mode
discoverable <on/off>                             Set controller discoverable mode
agent <on/off/capability>                         Enable/disable agent with given capability
default-agent                                     Set agent as the default one
advertise <on/off/type>                           Enable/disable advertising with given type
set-alias                                  Set device alias
scan <on/off>                                     Scan for devices
info [dev]                                        Device information
pair [dev]                                        Pair with device
trust [dev]                                       Trust device
untrust [dev]                                     Untrust device
block [dev]                                       Block device
unblock [dev]                                     Unblock device
remove                                       Remove device
connect                                      Connect device
disconnect [dev]                                  Disconnect device
menu                                        Select submenu
version                                           Display version
quit                                              Quit program
exit                                              Quit program
help                                              Display help about this program
[bluetooth]# 

[bluetooth]# power on
Changing power on succeeded

[bluetooth]# pair A0:60:90:16:4D:23
Attempting to pair with A0:60:90:16:4D:23
Failed to pair: org.bluez.Error.AlreadyExists

[bluetooth]# trust A0:60:90:16:4D:23
[CHG] Device A0:60:90:16:4D:23 Trusted: yes
Changing A0:60:90:16:4D:23 trust succeeded

[bluetooth]# connect A0:60:90:16:4D:23
Attempting to connect to A0:60:90:16:4D:23
Failed to connect: org.bluez.Error.Failed

지금 디바이스가 없어서 안되는데 원래 연결이 된다.

[bluetooth]# connect A0:60:90:16:4D:23
Attempting to connect to A0:60:90:16:4D:23
Failed to connect: org.bluez.Error.Failed
[CHG] Device A0:60:90:16:4D:23 Connected: yes
[CHG] Device A0:60:90:16:4D:23 UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 0000110d-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 0000110d-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device A0:60:90:16:4D:23 UUIDs: 00001200-0000-1000-8000-00805f9b34fb

[Level Box Slim (4D23)]# disconnect
Attempting to disconnect from A0:60:90:16:4D:23
Successful disconnected
[CHG] Device A0:60:90:16:4D:23 Connected: no
[bluetooth]# quit
Agent unregistered
[DEL] Controller 00:1A:7D:DA:71:11 lionelj-x3900 [default]

됐다.ㅋ

스크립트로 만들면

#!/bin/sh

echo -e 'power on\nconnect A0:60:90:16:4D:23\nquit' | bluetoothctl

 

Bluetooth Interfaces

There are two Bluetooth related interfaces available on Ubuntu Core.

bluez interface that allows accessing the Bluetooth service through D-Bus API

bluetooth-control that can be used to talk to the kernel-side of the Bluetooth stack directly.

bluez는 Bluetooth service를 통한 D-Bus API 이고 bluetooth-control 은 kernel 로 bluetooth stack 에 직접 접근한다.

https://docs.ubuntu.com/core/en/stacks/bluetooth/bluez/docs/reference/enablement/kernel-configuration-options.html

 

Linux kernel configuration options | Bluetooth documentation

Linux Kernel Configuration Options This section lists the Linux kernel configuration options related to the Bluetooth support. It is based on the Linux kernel v4.4. Note that the default Linux kernel for Ubuntu Core has al the necessary bits enabled by def

docs.ubuntu.com

This section lists the Linux kernel configuration options related to the Bluetooth support. It is based on the Linux kernel v4.4.

Note that the default Linux kernel for Ubuntu Core has al the necessary bits enabled by default.

Most of the Bluetooth systems will support Classic mode therefore you need to make sure that the following options are selected.

  [*] Networking support
       Bluetooth subsystem support
        [*]   Bluetooth Classic (BR/EDR) features
         RFCOMM protocol support
    [*]       RFCOMM TTY support
         BNEP protocol support
    [*]       Multicast filter
    [*]       Protocol filter support
         CMTP protocol support
         HIDP protocol support
    [*]     Bluetooth High Speed (HS) features
    [*]   Bluetooth Low Energy (LE) features
         Bluetooth 6LoWPAN support
    [ ]   Bluetooth self testing support
    [*]   Export Bluetooth internals in debugf
Note that this is a general set and it might be further tweaked to match your device use-cases and capabilities. For example, on devices that will not offer networking over Bluetooth, a BNEP can be disabled.

Note that if your Bluetooth controller (chip) supports Bluetooth Low Energy, then leave the BLE related options selected or disable otherwise:

  [*] Networking support
       Bluetooth subsystem support
    [*]   Bluetooth Low Energy (LE) features
         Bluetooth 6LoWPAN support
It is also important to remember about the UHID driver. It is needed for Bluetooth Low Energy (a.k.a. Smart) keyboards and mice.

Device Drivers
  HID support
    {M} HID bus support
    [*]   Battery level reporting for HID devices
    [*]   /dev/hidraw raw HID device support
       User-space I/O driver support for HID subsystem
       Generic HID driver 
Below are the Linux kenrel config options for reference. They are based on the Linux core16 4.4.0-1040-raspi2 kernel which is the official Ubuntu Core Linux kernel for Raspberry Pi 2/3.

CONFIG_NET=y
CONFIG_BT=m
CONFIG_BT_BREDR=y
CONFIG_BT_RFCOMM=m
CONFIG_BT_RFCOMM_TTY=y
CONFIG_BT_BNEP=m
CONFIG_BT_BNEP_MC_FILTER=y
CONFIG_BT_BNEP_PROTO_FILTER=y
CONFIG_BT_CMTP=m
CONFIG_BT_HIDP=m
CONFIG_BT_HS=y
CONFIG_BT_LE=y
CONFIG_BT_6LOWPAN=m
CONFIG_BT_DEBUGFS=y

CONFIG_BT_HCIBCM203X=m
CONFIG_BT_HCIBFUSB=m
CONFIG_BT_HCIBPA10X=m
CONFIG_BT_HCIBTSDIO=m
CONFIG_BT_HCIBTUSB=m
CONFIG_BT_HCIBTUSB_BCM=y
CONFIG_BT_HCIBTUSB_RTL=y
CONFIG_BT_HCIUART=m
CONFIG_BT_HCIUART_3WIRE=y
CONFIG_BT_HCIUART_ATH3K=y
CONFIG_BT_HCIUART_BCM=y
CONFIG_BT_HCIUART_BCSP=y
CONFIG_BT_HCIUART_H4=y
CONFIG_BT_HCIUART_INTEL=y
CONFIG_BT_HCIUART_LL=y
CONFIG_BT_HCIUART_QCA=y
CONFIG_BT_HCIVHCI=m

CONFIG_BT_ATH3K=m
CONFIG_BT_BCM=m
CONFIG_BT_INTEL=m
CONFIG_BT_MRVL=m
CONFIG_BT_MRVL_SDIO=m
CONFIG_BT_QCA=m
CONFIG_BT_RTL=m
CONFIG_BT_WILINK=m
$ sudo btmon 
[sudo] lionelj의 암호: 
Bluetooth monitor ver 5.48
= Note: Linux version 5.0.0-27-generic (x86_64)                                                                            0.344855
= Note: Bluetooth subsystem version 2.22                                                                                   0.344857
= New Index: 00:1A:7D:DA:71:11 (Primary,USB,hci0)                                                                   [hci0] 0.344857
= Open Index: 00:1A:7D:DA:71:11                                                                                     [hci0] 0.344857
= Index Info: 00:1A:7D:DA:71:11 (Cambridge Silicon Radio)                                                           [hci0] 0.344858
@ MGMT Open: bluetoothd (privileged) version 1.14                                                                 {0x0002} 0.344858
@ MGMT Open: bluetoothd (privileged) version 1.14                                                                 {0x0001} 0.344859
@ MGMT Open: btmon (privileged) version 1.14                                                                      {0x0003} 0.344869

 

$ sudo btmgmt 
[mgmt]# help
Available commands:
  version                    Get the MGMT Version
  commands                   List supported commands
  config                     Show configuration info
  info                       Show controller info
  extinfo                    Show extended controller info
  auto-power                 Power all available features
  power <on/off>             Toggle powered state
  discov <yes/no/limited> [timeout]
                             Toggle discoverable state
  connectable <on/off>       Toggle connectable state
  fast-conn <on/off>         Toggle fast connectable state
  bondable <on/off>          Toggle bondable state
  pairable <on/off>          Toggle bondable state
  linksec <on/off>           Toggle link level security
  ssp <on/off>               Toggle SSP mode
  sc <on/off/only>           Toogle SC support
  hs <on/off>                Toggle HS support
  le <on/off>                Toggle LE support
  advertising <on/off>       Toggle LE advertising
  bredr <on/off>             Toggle BR/EDR support
  privacy <on/off>           Toggle privacy support
  class        Set device major/minor class
  disconnect [-t type] 
                             Disconnect device
  con                        List connections
  find [-l|-b] [-L]          Discover nearby devices
  find-service [-u UUID] [-r RSSI_Threshold] [-l|-b]
                             Discover nearby service
  stop-find [-l|-b]          Stop discovery
  name  [shortname]    Set local name
  pair [-c cap] [-t type] 
                             Pair with a remote device
  cancelpair [-t type] 
                             Cancel pairing
  unpair [-t type] 
                             Unpair device
  keys                       Load Link Keys
  ltks                       Load Long Term Keys
  irks [--local ] [--file ]
                             Load Identity Resolving Keys
  block [-t type] 
                             Block Device
  unblock [-t type] 
                             Unblock Device
  add-uuid  
                             Add UUID
  rm-uuid              Remove UUID
  clr-uuids                  Clear UUIDs
  local-oob                  Local OOB data
  remote-oob [-t ] [-r ] [-h ] [-R ] [-H ] 
                             Remote OOB data
  did :::
                             Set Device ID
  static-addr 

      Set static address
  public-addr 
      Set public address
  ext-config <on/off>        External configuration
  debug-keys <on/off>        Toogle debug keys
  conn-info [-t type] 
                             Get connection information
  io-cap                Set IO Capability
  scan-params  
                             Set Scan Parameters
  get-clock [address]        Get Clock Information
  add-device [-a action] [-t type] 

                             Add Device
  del-device [-t type] 

                             Remove Device
  clr-devices                Clear Devices
  bredr-oob                  Local OOB data (BR/EDR)
  le-oob                     Local OOB data (LE)
  advinfo                    Show advertising features
  advsize [options] 
                             Show advertising size info
  add-adv [options] 
                             Add advertising instance
  rm-adv        Remove advertising instance
  clr-adv                    Clear advertising instances
  appearance     Set appearance
  select              Select a different index
  quit                       Exit program
  exit                       Exit program
  help                       List supported commands

[mgmt]# find
Discovery started
hci0 type 7 discovering on
hci0 dev_found: A4:83:E7:64:4F:E1 type LE Public rssi -81 flags 0x0000 
AD flags 0x06 
eir_len 14
hci0 dev_found: 5D:DB:59:16:7F:61 type LE Random rssi -82 flags 0x0000 
AD flags 0x1a 
eir_len 17
hci0 dev_found: 8C:85:90:CC:A7:74 type LE Public rssi -81 flags 0x0000 
AD flags 0x06 
eir_len 14
hci0 dev_found: 8C:85:90:A7:71:9D type LE Public rssi -80 flags 0x0000 
AD flags 0x06 
eir_len 14
hci0 dev_found: 62:CB:8B:CB:4E:38 type LE Random rssi -79 flags 0x0000 
AD flags 0x1a 
eir_len 18
hci0 dev_found: 4A:C0:58:4C:FC:16 type LE Random rssi -82 flags 0x0000 
AD flags 0x1a 
eir_len 14​
 

Custom solutions


● Don’t want/need full bluetoothd for your tiny custom app?
● src/shared folder in bluez.git contains LGPL licenced components
○ Used by bluetoothd and other BlueZ tools
○ Library like C API
○ Easy to integrate
○ MGMT, ATT, GATT, crypto, advertising, ECC, GAP, HFP and more 
○ No API stability guaranteed 

● Ideal for beacons or simple peripheral applications 
○ peripheral/ folder for peripheral example (LGPL) 

● User channel 
○ Gives HCI exclusive access to user space application 
○ Sample in tools/eddystone.c (GPL)

 

odroid bluetooth test 방법

 

몇개 깔아준다.

$ sudo apt-get install bluez-dbg bluez-hcidump bluez-utils bluez-tools 
$ sudo apt-get install bluewho blueman python-bluetooth

$ hcitool dev
Devices:    hci0    00:02:72:CC:F4:CE 

$ hciconfig 
hci0:    Type: BR/EDR  Bus: USB
	BD Address: 00:02:72:CC:F4:CE  ACL MTU: 1021:8  SCO MTU: 64:1
    UP RUNNING PSCAN
    RX bytes:583 acl:0 sco:0 events:33 errors:0
    TX bytes:898 acl:0 sco:0 commands:33 errors:0
    
$ sudo rfkill list all
0: hci0: Bluetooth
	Soft blocked: no
    Hard blocked: no

$ sudo hcidump
HCI sniffer - Bluetooth packet analyzer ver 2.5
device: hci0 snap_len: 1500 filter: 0xffffffff
< HCI Command: Write Class of Device (0x03|0x0024) plen 3
	class 0x700100 
> HCI Event: Command Complete (0x0e) plen 4
	Write Class of Device (0x03|0x0024) ncmd 1
	status 0x00
< HCI Command: Write Extended Inquiry Response (0x03|0x0052) plen 241
	fec 0x00
	Complete local name: ‘c1-1-0’
	TX power level: 0
	Complete service classes: 0x112d 0x1112 0x111f 0x111e 0x110c 0x110e 0x1105
> HCI Event: Command Complete (0x0e) plen 4
	Write Extended Inquiry Response (0x03|0x0052) ncmd 1
	status 0x00
< HCI Command: Write Extended Inquiry Response (0x03|0x0052) plen 241
	fec 0x00
	Complete local name: ‘c1-1-0’
	TX power level: 0
	Complete service classes: 0x112d 0x1112 0x111f 0x111e 0x110c 0x110e ... 
> HCI Event: Command Complete (0x0e) plen 4
	Write Extended Inquiry Response (0x03|0x0052) ncmd 1
	status 0x00
...
 
$ sudo hcitool scan

블루투스 디바이스의 버튼을 누르면

Scanning ...
XX:XX:XX:XX:XX:XX        repoo E6700
$ sudo bluez-sample-agent hci0 XX:XX:XX:XX:XX:XX

커넥트 버튼을 다시 누르면 키를 입력하라고 나온다. 000000을 써본다.

그리고  bluetooth service 를 재시작한다.

$ sudo bluez-test-device trusted xx:xx:xx:xx:xx:xx yes
$ sudo /etc/init.d/bluetooth restart

 

 

반응형
Posted by Real_G