출처 : https://cafe.naver.com/openiot/101


우분투에서 BT A2dp Sink 쓰기


sudo apt-get install --reinstall pulseaudio pulseaudio-utils pulseaudio-module-bluetooth


sudo pactl load-module module-bluetooth-discover


BT 작업을 하기전 BT카 켜있는지, 블러킹 상태는 아닌지 확인해본다.


$ sudo rfkill list

0: hci0: Bluetooth

Soft blocked: no

Hard blocked: no


혹시 블라킹되어있다면 아래 명령으로 풀어준다.

$ sudo rfkill unblock bluetooth


BT 설정하기


sudo hciconfig

hci0: Type: BR/EDR  Bus: USB

BD Address: 00:1A:7D:DA:71:11  ACL MTU: 310:10  SCO MTU: 64:8

UP RUNNING PSCAN 

RX bytes:30380 acl:10 sco:0 events:209 errors:0

TX bytes:5521 acl:9 sco:0 commands:73 errors:0


BT 장치를 켜준다.

$ sudo hciconfig hci0 up

내 장치가 검색 가능하도록 만들기
$ sudo hciconfig -a hci0 piscan


스캔하기

$ hcitool scan

Scanning ...

0C:1C:20:00:03:7F MINI0004

0C:1C:20:FF:F5:A9 0069

0C:1C:20:FF:F3:73 0186

0C:1C:20:16:F1:84 MINI0573

0C:1C:20:00:00:9F 0080.

80:35:C1:5A:58:8B n/a

CC:4B:73:27:4E:77 jake.jeon


장치의 프로파일/서비스 확인하기 sdptool (SDP 스캔하기)


$ sdptool browse 0C:1C:20:00:03:7F

Browsing 0C:1C:20:00:03:7F ...

Service RecHandle: 0x10000

Service Class ID List:

  "Generic Attribute" (0x1801)

Protocol Descriptor List:

  "L2CAP" (0x0100)

    PSM: 31

  "ATT" (0x0007)

    uint16: 0x0001

    uint16: 0x0005


Service RecHandle: 0x10001

Service Class ID List:

  "Generic Access" (0x1800)

Protocol Descriptor List:

  "L2CAP" (0x0100)

    PSM: 31

  "ATT" (0x0007)

    uint16: 0x0014

    uint16: 0x001e


Service Name: AV Remote Control Target

Service RecHandle: 0x10002

Service Class ID List:

  "AV Remote Target" (0x110c)

Protocol Descriptor List:

  "L2CAP" (0x0100)

    PSM: 23

  "AVCTP" (0x0017)

    uint16: 0x0102

Profile Descriptor List:

  "AV Remote" (0x110e)

    Version: 0x0103


Service Name: Advanced Audio

Service RecHandle: 0x10003

Service Class ID List:

  "Audio Source" (0x110a)

Protocol Descriptor List:

  "L2CAP" (0x0100)

    PSM: 25

  "AVDTP" (0x0019)

    uint16: 0x0102

Profile Descriptor List:

  "Advanced Audio" (0x110d)

    Version: 0x0102


Service Name: Advanced Audio Sink

Service RecHandle: 0x10004

Service Class ID List:

  "Audio Sink" (0x110b)

Protocol Descriptor List:

  "L2CAP" (0x0100)

    PSM: 25

  "AVDTP" (0x0019)

    uint16: 0x0102

Profile Descriptor List:

  "Advanced Audio" (0x110d)

    Version: 0x0102


Service RecHandle: 0x10005

Service Class ID List:

  "AV Remote" (0x110e)

Protocol Descriptor List:

  "L2CAP" (0x0100)

    PSM: 23

  "AVCTP" (0x0017)

    uint16: 0x0102

Profile Descriptor List:

  "AV Remote" (0x110e)

    Version: 0x0103


Service Name: SMS/MMS

Service RecHandle: 0x10007

Service Class ID List:

  "Message Access - MAS" (0x1132)

Protocol Descriptor List:

  "L2CAP" (0x0100)

  "RFCOMM" (0x0003)

    Channel: 2

  "OBEX" (0x0008)

Profile Descriptor List:

  "Message Access" (0x1134)

    Version: 0x0101


Service Name: BluetoothChat

Service RecHandle: 0x10008

Service Class ID List:

  UUID 128: 220da3b2-41f5-11e7-a919-92ebcb67fe33

Protocol Descriptor List:

  "L2CAP" (0x0100)

  "RFCOMM" (0x0003)

    Channel: 3


bluetoothctl 로 조작해보기 블루투스 쉘에 진입


$ bluetoothctl

[NEW] Controller 00:1A:7D:DA:71:11 lionelj-Desktop [default]

[NEW] Device 0C:1C:20:16:F1:84 MINI0573

[NEW] Device F8:E6:1A:EA:8F:A8 Galaxy S8

[NEW] Device 0C:1C:20:FF:F1:75 MINI1188

[bluetooth]# 


BT 제어장치의 정보를 보기

[bluetooth]# show

Controller 00:1A:7D:DA:71:11

Name: lionelj-Desktop

Alias: lionelj-Desktop

Class: 0x1c0104

Powered: yes

Discoverable: yes

Pairable: yes

UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)

UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)

UUID: OBEX File Transfer        (00001106-0000-1000-8000-00805f9b34fb)

UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)

UUID: OBEX Object Push          (00001105-0000-1000-8000-00805f9b34fb)

UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)

UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)

UUID: IrMC Sync                 (00001104-0000-1000-8000-00805f9b34fb)

UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)

UUID: Audio Source              (0000110a-0000-1000-8000-00805f9b34fb)

UUID: Vendor specific           (00005005-0000-1000-8000-0002ee000001)

UUID: Message Notification Se.. (00001133-0000-1000-8000-00805f9b34fb)

UUID: Phonebook Access Server   (0000112f-0000-1000-8000-00805f9b34fb)

UUID: Message Access Server     (00001132-0000-1000-8000-00805f9b34fb)

Modalias: usb:v1D6Bp0246d0525

Discovering: no


다른장치와 페어링 하기위해 기본 에이전트로 등록하기

[bluetooth]# agent on
Agent registered

주변의 다른 장치 검색하기


[bluetooth]# scan on

Discovery started

[CHG] Controller 00:1A:7D:DA:71:11 Discovering: yes

[NEW] Device 8C:85:90:55:FE:02 8C-85-90-55-FE-02

[NEW] Device F0:18:98:55:CE:BA F0-18-98-55-CE-BA

[NEW] Device 8C:85:90:BB:ED:17 8C-85-90-BB-ED-17

[NEW] Device 57:7C:3A:AB:84:32 57-7C-3A-AB-84-32

[NEW] Device 70:8A:1B:E7:F3:B9 70-8A-1B-E7-F3-B9

[NEW] Device 5C:87:53:F4:93:DC 5C-87-53-F4-93-DC

[NEW] Device 7B:9E:36:C3:BE:BB 7B-9E-36-C3-BE-BB

[NEW] Device 50:FA:17:0A:33:92 50-FA-17-0A-33-92

[NEW] Device 0C:1C:20:FF:F3:73 0186

[NEW] Device 77:36:8C:CF:81:94 77-36-8C-CF-81-94

[NEW] Device 52:3B:31:81:60:C3 52-3B-31-81-60-C3

[NEW] Device 42:22:8A:93:DB:CE 42-22-8A-93-DB-CE

[NEW] Device 6A:DD:11:52:74:24 6A-DD-11-52-74-24

[NEW] Device 0C:1C:20:00:00:9F 0080

[NEW] Device 7D:5D:FB:43:FF:58 7D-5D-FB-43-FF-58

[CHG] Device 70:8A:1B:E7:F3:B9 RSSI: -78
[NEW] Device 14:20:5E:05:13:29 14-20-5E-05-13-29
[NEW] Device 42:99:92:1C:C9:09 42-99-92-1C-C9-09
[NEW] Device 7E:1C:1E:2B:86:2F 7E-1C-1E-2B-86-2F
[NEW] Device 48:1A:9F:0D:56:38 48-1A-9F-0D-56-38
[CHG] Device 5C:66:8D:C1:BC:F6 RSSI: -82
[NEW] Device 40:0E:B7:97:81:BB 40-0E-B7-97-81-BB
[NEW] Device E9:C7:A9:D1:EF:A2 WeZON Beacon
[NEW] Device DC:A9:04:75:65:F2 DC-A9-04-75-65-F2
[CHG] Device 0C:1C:20:FF:F3:73 RSSI: -74
[CHG] Device 0C:1C:20:FF:F3:73 Class: 0x2c0414
[CHG] Device 8C:85:90:BB:ED:17 RSSI is nil
[CHG] Device F0:18:98:55:CE:BA RSSI is nil
[CHG] Device 8C:85:90:55:FE:02 RSSI is nil


막 줄줄줄 나옴.

너무 많으니까 일단 스캔 끄기


[bluetooth]# scan off

Discovery stopped

[CHG] Controller 00:1A:7D:DA:71:11 Discovering: no


다른 장치에서 검색 가능하게 하기


[bluetooth]# discoverable on

Changing discoverable on succeeded



페어링 하기

이렇게 하면 된다는데 난 왜 안됨??


[bluetooth]# pair 11:22:33:44:55:77

Attempting to pair with 0C:1C:20:00:03:7F

Failed to pair: org.bluez.Error.AuthenticationCanceled


계속 반복하니 됨


[bluetooth]# pair 0C:1C:20:00:03:7F

Attempting to pair with 0C:1C:20:00:03:7F

[CHG] Device 0C:1C:20:00:03:7F Connected: yes

[MINI0004]# pair 0C:1C:20:00:03:7F

Attempting to pair with 0C:1C:20:00:03:7F

Failed to pair: org.bluez.Error.InProgress

Request confirmation

[agent] Confirm passkey 218316 (yes/no): yes  <==기기에서도 확인해줘야 함.

[CHG] Device 0C:1C:20:00:03:7F Modalias: bluetooth:v000Fp1200d1436

[CHG] Device 0C:1C:20:00:03:7F UUIDs: 0000110a-0000-1000-8000-00805f9b34fb

[CHG] Device 0C:1C:20:00:03:7F UUIDs: 0000110b-0000-1000-8000-00805f9b34fb

[CHG] Device 0C:1C:20:00:03:7F UUIDs: 0000110c-0000-1000-8000-00805f9b34fb

[CHG] Device 0C:1C:20:00:03:7F UUIDs: 0000110e-0000-1000-8000-00805f9b34fb

[CHG] Device 0C:1C:20:00:03:7F UUIDs: 00001132-0000-1000-8000-00805f9b34fb

[CHG] Device 0C:1C:20:00:03:7F UUIDs: 00001200-0000-1000-8000-00805f9b34fb

[CHG] Device 0C:1C:20:00:03:7F UUIDs: 00001800-0000-1000-8000-00805f9b34fb

[CHG] Device 0C:1C:20:00:03:7F UUIDs: 00001801-0000-1000-8000-00805f9b34fb

[CHG] Device 0C:1C:20:00:03:7F Paired: yes

Pairing successful

[CHG] Device 0C:1C:20:00:03:7F Connected: no


장치와 연결하기. - 연결 기능은 해당 서비스/프로파일이 양쪽 기기에 설치되어 있어야 한다.

[bluetooth]# connect 0C:1C:20:00:03:7F
Attempting to connect to 0C:1C:20:00:03:7F
[CHG] Device 0C:1C:20:00:03:7F Connected: yes
Connection successful
[MINI0004]# 

상대 장치의 정보 확인하기

[bluetooth]# info 0C:1C:20:00:03:7F
Device 0C:1C:20:00:03:7F
Name: MINI0004
Alias: MINI0004
Class: 0x2c0414
Icon: audio-card
Paired: yes
Trusted: no
Blocked: no
Connected: no
LegacyPairing: no
UUID: Audio Source              (0000110a-0000-1000-8000-00805f9b34fb)
UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
UUID: Message Access Server     (00001132-0000-1000-8000-00805f9b34fb)
UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
Modalias: bluetooth:v000Fp1200d1436


다음처럼 접속해서 info 해도 나옴.

[bluetooth]#  connect 0C:1C:20:00:03:7F
Attempting to connect to 0C:1C:20:00:03:7F
[CHG] Device 0C:1C:20:00:03:7F Connected: yes
Connection successful
[MINI0004]# info
Device 0C:1C:20:00:03:7F
Name: MINI0004
Alias: MINI0004
Class: 0x2c0414
Icon: audio-card
Paired: yes
Trusted: no
Blocked: no
Connected: yes
LegacyPairing: no
UUID: Audio Source              (0000110a-0000-1000-8000-00805f9b34fb)
UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
UUID: Message Access Server     (00001132-0000-1000-8000-00805f9b34fb)
UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
Modalias: bluetooth:v000Fp1200d1436
[CHG] Device 0C:1C:20:00:03:7F Connected: no

연결 해제하기


[bluetooth]# disconnect 0C:1C:20:00:03:7F

Attempting to disconnect from 0C:1C:20:00:03:7F

Successful disconnected


자기 자신 서비스 보기

이것을 하려면 조금 복잡한 일을 해야한다.

$ vi /lib/systemd/system/bluetooth.service 파일의 아래 --compat 를 주면 local 의 서비스를 확인 할 수 있다.


[Service]

Type=dbus

BusName=org.bluez

ExecStart=/usr/lib/bluetooth/bluetoothd --compat


$ sudo systemctl daemon-reload
$ sudo systemctl restart bluetooth
$ sudo chmod 777 /var/run/sdp
$ sudo sdptool browse local
Browsing FF:FF:FF:00:00:00 ...
Service RecHandle: 0x10000
Service Class ID List:
  "PnP Information" (0x1200)
Profile Descriptor List:
  "PnP Information" (0x1200)
    Version: 0x0103

Browsing FF:FF:FF:00:00:00 ...
Service Search failed: Invalid argument
Service Name: Generic Access Profile
Service Provider: BlueZ
Service RecHandle: 0x10001
Service Class ID List:
  "Generic Access" (0x1800)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 31
  "ATT" (0x0007)
    uint16: 0x0001
    uint16: 0x0005

Service Name: Generic Attribute Profile
Service Provider: BlueZ
Service RecHandle: 0x10002
Service Class ID List:
  "Generic Attribute" (0x1801)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 31
  "ATT" (0x0007)
    uint16: 0x0006
    uint16: 0x0009

Service Name: AVRCP CT
Service RecHandle: 0x10003
Service Class ID List:
  "AV Remote" (0x110e)
  "AV Remote Controller" (0x110f)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 23
  "AVCTP" (0x0017)
    uint16: 0x0103
Profile Descriptor List:
  "AV Remote" (0x110e)
    Version: 0x0106

Service Name: AVRCP TG
Service RecHandle: 0x10004
Service Class ID List:
  "AV Remote Target" (0x110c)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 23
  "AVCTP" (0x0017)
    uint16: 0x0103
Profile Descriptor List:
  "AV Remote" (0x110e)
    Version: 0x0105

Service Name: Message Notification
Service RecHandle: 0x10005
Service Class ID List:
  "Message Access - MNS" (0x1133)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 17
  "OBEX" (0x0008)
Profile Descriptor List:
  "Message Access" (0x1134)
    Version: 0x0102

Browsing FF:FF:FF:00:00:00 ...
Service Search failed: Invalid argument
Service Name: Message Access
Service RecHandle: 0x10006
Service Class ID List:
  "Message Access - MAS" (0x1132)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 16
  "OBEX" (0x0008)
Profile Descriptor List:
  "Message Access" (0x1134)
    Version: 0x0100

Browsing FF:FF:FF:00:00:00 ...
Service Search failed: Invalid argument
Service Name: Phone Book Access
Service RecHandle: 0x10007
Service Class ID List:
  "Phonebook Access - PSE" (0x112f)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 15
  "OBEX" (0x0008)
Profile Descriptor List:
  "Phonebook Access" (0x1130)
    Version: 0x0101

Browsing FF:FF:FF:00:00:00 ...
Service Search failed: Invalid argument
Service Name: Synchronization
Service RecHandle: 0x10008
Service Class ID List:
  "IrMC Sync" (0x1104)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 14
  "OBEX" (0x0008)
Profile Descriptor List:
  "IrMC Sync" (0x1104)
    Version: 0x0100

Service Name: File Transfer
Service RecHandle: 0x10009
Service Class ID List:
  "OBEX File Transfer" (0x1106)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 10
  "OBEX" (0x0008)
Profile Descriptor List:
  "OBEX File Transfer" (0x1106)
    Version: 0x0102

Browsing FF:FF:FF:00:00:00 ...
Service Search failed: Invalid argument
Service Name: Object Push
Service RecHandle: 0x1000a
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 9
  "OBEX" (0x0008)
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0102

Browsing FF:FF:FF:00:00:00 ...
Service Search failed: No data available
Service Name: Nokia OBEX PC Suite Services
Service RecHandle: 0x1000b
Service Class ID List:
  UUID 128: 00005005-0000-1000-8000-0002ee000001
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 24
  "OBEX" (0x0008)
Profile Descriptor List:
  "" (0x00005005-0000-1000-8000-0002ee000001)
    Version: 0x0100

Service Name: Audio Source
Service RecHandle: 0x1000c
Service Class ID List:
  "Audio Source" (0x110a)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 25
  "AVDTP" (0x0019)
    uint16: 0x0103
Profile Descriptor List:
  "Advanced Audio" (0x110d)
    Version: 0x0103

Service Name: Audio Sink
Service RecHandle: 0x1000d
Service Class ID List:
  "Audio Sink" (0x110b)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 25
  "AVDTP" (0x0019)
    uint16: 0x0103
Profile Descriptor List:
  "Advanced Audio" (0x110d)
    Version: 0x0103

Service Name: Headset Voice gateway
Service RecHandle: 0x1000e
Service Class ID List:
  "Headset Audio Gateway" (0x1112)
  "Generic Audio" (0x1203)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 12
Profile Descriptor List:
  "Headset" (0x1108)
    Version: 0x0102


BT 테스트 스크립트


이걸 깔면

sudo apt-get install bluez-tools bluez-test-scripts


아래 디렉토리에 테스트 스크립트가 생성된다.

/usr/share/doc/bluez-tests/examples


$ ls

bluezutils.py             pbap-client.gz      test-adapter          test-manager

dbusdef.py                sap_client.py.gz    test-alert.gz         test-nap

example-advertisement.gz  service-did.xml     test-cyclingspeed.gz  test-network

example-gatt-client.gz    service-ftp.xml     test-device.gz        test-profile

example-gatt-server.gz    service-opp.xml     test-discovery        test-proximity

ftp-client.gz             service-record.dtd  test-gatt-profile     test-sap-server

list-devices              service-spp.xml     test-health-sink      test-thermometer

map-client.gz             simple-agent.gz     test-health.gz

monitor-bluetooth         simple-endpoint.gz  test-heartrate

opp-client                simple-player.gz    test-hfp.gz



SPP 서비스 추가 등록하고 rfcomm 연결을 대기


$ sudo sdptool add sp

Serial Port service registered


$sudo rfcomm listen hci0
Waiting for connection on channel 1

아래처럼 켜줌.

$ bluetoothctl   <== BT컨트롤
[NEW] Controller 00:1A:7D:DA:71:11 lionelj-Desktop [default]
[NEW] Device 0C:1C:20:16:F1:84 MINI0573
[NEW] Device F8:E6:1A:EA:8F:A8 Galaxy S8
[NEW] Device 0C:1C:20:FF:F1:75 MINI1188
[NEW] Device 0C:1C:20:00:03:7F MINI0004
[bluetooth]# agent on <== 에이전트 등록
Agent registered
[bluetooth]# discoverable on <== 찾을 수 있도록
Changing discoverable on succeeded
[CHG] Device 0C:1C:20:00:03:7F Connected: yes
[CHG] Device 0C:1C:20:00:03:7F Connected: no

[bluetooth]# default-agent <== default-agent 으로 등록

Default agent request successful

[CHG] Device F8:E6:1A:EA:8F:A8 Connected: yes

Request confirmation

[agent] Confirm passkey 095343 (yes/no): yes <== 접속시도할때 PIN code 확인하는게 나옴.

[CHG] Device F8:E6:1A:EA:8F:A8 Connected: no


접속이 성공하면 아까 띄워놨던 rfcomm0 에서 읽을 수 있다.


$ sudo rfcomm listen hci0

Waiting for connection on channel 1

Connection from 0C:1C:20:00:03:7F to /dev/rfcomm0

Press CTRL-C for hangup


$ sudo cat /dev/rfcomm0
hello

미니컴으로 열어서 양방향 채팅이 가능하다.



BT mac으로 ping 날리기

$ sudo l2ping 0C:1C:20:00:03:7F

[sudo] password for lionelj: 

Ping: 0C:1C:20:00:03:7F from 00:1A:7D:DA:71:11 (data size 44) ...

0 bytes from 0C:1C:20:00:03:7F id 0 time 354.49ms

0 bytes from 0C:1C:20:00:03:7F id 1 time 20.52ms

0 bytes from 0C:1C:20:00:03:7F id 2 time 22.59ms

0 bytes from 0C:1C:20:00:03:7F id 3 time 22.48ms

0 bytes from 0C:1C:20:00:03:7F id 4 time 24.68ms

^C5 sent, 5 received, 0% loss


rfcomm 으로 연결하기


$ sudo rfcomm connect <bluetooth host device> <Target bluetooth device MAC> <channel>


hci0 로 채널 2에 연결하려면

sudo rfcomm connect hci0 83:23:26:15:54:46 2




Posted by Real_G