-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinstall-and-configure-pinless-bluetooth-pairing.yaml
180 lines (158 loc) · 6.14 KB
/
install-and-configure-pinless-bluetooth-pairing.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
---
- name: Install and configure pinless bluetooth pairing
hosts: all
gather_facts: true
vars:
bt_agent_service_location: /etc/systemd/system/bt-agent.service
bt_agent_service_script_location: /usr/local/bin/remove-paired-devices.sh
bt_agent_service_name: bt-agent.service
bluetooth_service_location: /lib/systemd/system/bluetooth.service
bluetooth_conf_location: /etc/bluetooth/main.conf
bluetooth_service_name: bluetooth.service
pulseaudio_system_service_location: /etc/systemd/system/pulseaudio.service
pulseaudio_system_service_name: pulseaudio.service
tasks:
# https://www.raspberrypi.org/forums/viewtopic.php?t=235519
- name: Required dependencies
become: true
apt:
update_cache: yes
pkg:
- bluez
- bluez-tools
- pulseaudio-module-bluetooth
- gnome-bluetooth
state: latest
- name: Enable bluetooth
become: true
shell: rfkill unblock bluetooth
- name: "Change bluetooth service to enable volume control"
block:
- name: "Service file edit: Disable avrcp plugin to control volume"
become: true
lineinfile:
path: "{{ bluetooth_service_location }}"
regexp: '^ExecStart=(.*)$'
line: 'ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=avrcp'
backrefs: yes
- name: Reload systemd service
include_tasks: tasks-reload-systemd-service.yaml
vars:
service_name: "{{ bluetooth_service_name }}"
become: True
- name: "Add {{ ansible_user }} to audio group"
become: true
ansible.builtin.user:
name: "{{ ansible_user }}"
groups: audio
append: yes
- name: Setup NoInputNoOutput bt-agent for pinless pairing
block:
- name: "Remove {{ pulseaudio_system_service_location }}"
become: true
file:
path: "{{ pulseaudio_system_service_location }}"
state: absent
- name: "Create {{ pulseaudio_system_service_location }}"
become: true
file:
path: "{{ pulseaudio_system_service_location }}"
state: touch
- name: "Populate {{ pulseaudio_system_service_location }} file"
become: True
blockinfile:
path: "{{ pulseaudio_system_service_location }}"
block: |
[Unit]
Description=PulseAudio Daemon
[Service]
ExecStart=/usr/bin/pulseaudio --daemonize=no --log-target=journal
Restart=on-failure
Type=simple
User={{ ansible_user }}
[Install]
WantedBy=multi-user.target
- name: Reload systemd service
include_tasks: tasks-reload-systemd-service.yaml
vars:
service_name: "{{ pulseaudio_system_service_name }}"
become: True
- name: Setup NoInputNoOutput bt-agent for pinless pairing
block:
- name: "Remove {{ bt_agent_service_script_location }}"
become: true
file:
path: "{{ bt_agent_service_location }}"
state: absent
- name: "Copy script to {{ bt_agent_service_script_location }}"
become: true
copy:
src: helpers/remove-paired-devices.sh
dest: "{{ bt_agent_service_script_location }}"
mode: "0755"
- name: "Remove {{ bt_agent_service_location }}"
become: true
file:
path: "{{ bt_agent_service_location }}"
state: absent
- name: "Create {{ bt_agent_service_location }}"
become: true
file:
path: "{{ bt_agent_service_location }}"
state: touch
- name: "Populate {{ bt_agent_service_location }} file"
become: True
blockinfile:
path: "{{ bt_agent_service_location }}"
block: |
[Unit]
Description=Bluetooth Auth Agent
After=bluetooth.service
PartOf=bluetooth.service
StartLimitInterval=0
[Service]
Type=simple
ExecStartPre=/usr/bin/bluetoothctl power yes
ExecStartPre=/usr/bin/bluetoothctl discoverable yes
ExecStartPre=/usr/bin/bluetoothctl discoverable-timeout 0
ExecStartPre=/usr/bin/bluetoothctl pairable yes
ExecStartPre=/usr/bin/amixer set Master 100
ExecStartPre={{ bt_agent_service_script_location }}
ExecStart=/usr/bin/bt-agent -c NoInputNoOutput
Restart=on-failure
RestartSec=5
[Install]
WantedBy=bluetooth.target
- name: Reload systemd service
include_tasks: tasks-reload-systemd-service.yaml
vars:
service_name: "{{ bt_agent_service_name }}"
become: True
- name: "Setup alsa-restore for the {{ ansible_user }}"
# https://askubuntu.com/questions/132440/headphone-jack-not-working
# https://bbs.archlinux.org/viewtopic.php?id=147206
# alsa-restore only works on root as its a system service,
# to make it work for {{ ansible_user }}, a systemd service on the
# user level needs to be created. This way a restart will
# fix audio issues
become: True
block:
- name: Copy system alsa-restore service
copy:
remote_src: true
src: /lib/systemd/system/alsa-restore.service
dest: /etc/systemd/system/alsa-restore.service
- name: "Edit service file to run as {{ ansible_user }}"
lineinfile:
path: "{{ loc_user_service }}"
insertafter: '^\[Service\]$'
line: "User={{ ansible_user }}"
- name: Reload systemd service
include_tasks: tasks-reload-systemd-service.yaml
vars:
service_name: "{{ service_file_name }}"
become: True
vars:
loc_original_service: /lib/systemd/system/alsa-restore.service
loc_user_service: /etc/systemd/system/alsa-restore.service
service_file_name: alsa-restore.service