우분투에서 cli로 BT A2dp Sink 쓰기 (hcitool, bluetoothctl, sdptool, rfkill, l2ping)
출처 : 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
스캔하기
$ 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]# 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]# 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
[bluetooth]# scan off
[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]# 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
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
[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 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
hfp MIC 녹음 해보려고 했는데 안되네
sudo arecord -f S16_LE -D bluealsa:HCI=hci,DEV=00:1A:7D:DA:71:11,PROFILE=sco record.wav
'Linux > Linux 일반' 카테고리의 다른 글
bluetooth - python (0) | 2018.11.26 |
---|---|
Shell script fails: Syntax error: “(” unexpected (0) | 2018.07.06 |
DBus (0) | 2018.03.27 |