forked from nicokaiser/rpi-audio-receiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-bluetooth.sh
executable file
·215 lines (166 loc) · 5.84 KB
/
install-bluetooth.sh
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/sh
echo "Installing Bluetooth Audio (BlueALSA)"
apt install -y --no-install-recommends alsa-base alsa-utils bluealsa bluez python-gobject python-dbus vorbis-tools sound-theme-freedesktop
# Bluetooth settings
cat <<'EOF' > /etc/bluetooth/main.conf
[General]
Class = 0x200414
DiscoverableTimeout = 0
[Policy]
AutoEnable=true
EOF
service bluetooth start
hciconfig hci0 piscan
hciconfig hci0 sspmode 1
# Bluetooth agent
cat <<'EOF' > /opt/local/bin/bluetooth-agent
#!/usr/bin/python
# Automatically authenticating bluez agent.
from __future__ import absolute_import, print_function, unicode_literals
from gi.repository import GObject
import sys
import dbus
import dbus.service
import dbus.mainloop.glib
BUS_NAME = 'org.bluez'
AGENT_INTERFACE = 'org.bluez.Agent1'
AGENT_PATH = "/test/agent"
bus = None
device_obj = None
dev_path = None
def set_trusted(path):
props = dbus.Interface(bus.get_object("org.bluez", path), "org.freedesktop.DBus.Properties")
props.Set("org.bluez.Device1", "Trusted", True)
class Agent(dbus.service.Object):
exit_on_release = True
def set_exit_on_release(self, exit_on_release):
self.exit_on_release = exit_on_release
@dbus.service.method(AGENT_INTERFACE, in_signature="", out_signature="")
def Release(self):
print("Release")
if self.exit_on_release:
mainloop.quit()
@dbus.service.method(AGENT_INTERFACE, in_signature="os", out_signature="")
def AuthorizeService(self, device, uuid):
print("AuthorizeService (%s, %s)" % (device, uuid))
set_trusted(device)
return
@dbus.service.method(AGENT_INTERFACE, in_signature="o", out_signature="s")
def RequestPinCode(self, device):
print("RequestPinCode (%s)" % (device))
set_trusted(device)
return "0000"
@dbus.service.method(AGENT_INTERFACE, in_signature="o", out_signature="u")
def RequestPasskey(self, device):
print("RequestPasskey (%s)" % (device))
set_trusted(device)
return dbus.UInt32("0000")
@dbus.service.method(AGENT_INTERFACE, in_signature="ouq", out_signature="")
def DisplayPasskey(self, device, passkey, entered):
print("DisplayPasskey (%s, %06u entered %u)" % (device, passkey, entered))
@dbus.service.method(AGENT_INTERFACE, in_signature="os", out_signature="")
def DisplayPinCode(self, device, pincode):
print("DisplayPinCode (%s, %s)" % (device, pincode))
@dbus.service.method(AGENT_INTERFACE, in_signature="ou", out_signature="")
def RequestConfirmation(self, device, passkey):
print("RequestConfirmation (%s, %06d)" % (device, passkey))
set_trusted(device)
return
@dbus.service.method(AGENT_INTERFACE, in_signature="o", out_signature="")
def RequestAuthorization(self, device):
print("RequestAuthorization (%s)" % (device))
set_trusted(device)
return
@dbus.service.method(AGENT_INTERFACE, in_signature="", out_signature="")
def Cancel(self):
print("Cancel")
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
capability = "NoInputNoOutput"
path = "/test/agent"
agent = Agent(bus, path)
mainloop = GObject.MainLoop()
obj = bus.get_object(BUS_NAME, "/org/bluez");
manager = dbus.Interface(obj, "org.bluez.AgentManager1")
manager.RegisterAgent(path, capability)
print("Agent registered")
manager.RequestDefaultAgent(path)
mainloop.run()
EOF
chmod 755 /opt/local/bin/bluetooth-agent
cat <<'EOF' > /etc/systemd/system/bluetooth-agent.service
[Unit]
Description=Bluetooth Agent
Requires=bluetooth.service
After=bluetooth.target bluetooth.service
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
ExecStart=/opt/local/bin/bluetooth-agent
EOF
systemctl enable bluetooth-agent.service
# ALSA settings
sed -i.orig 's/^options snd-usb-audio index=-2$/#options snd-usb-audio index=-2/' /lib/modprobe.d/aliases.conf
# BlueALSA
mkdir -p /etc/systemd/system/bluealsa.service.d
cat <<'EOF' > /etc/systemd/system/bluealsa.service.d/override.conf
[Service]
ExecStart=
ExecStart=/usr/bin/bluealsa --disable-hfp --disable-hsp
ExecStartPre=/bin/sleep 1
EOF
cat <<'EOF' > /etc/systemd/system/bluealsa-aplay.service
[Unit]
Description=BlueALSA player
Requires=bluealsa.service
After=bluealsa.service
Wants=bluetooth.target sound.target
[Service]
Type=simple
User=root
ExecStartPre=/bin/sleep 2
ExecStart=/usr/bin/bluealsa-aplay --pcm-buffer-time=250000 00:00:00:00:00:00
[Install]
WantedBy=graphical.target
EOF
systemctl daemon-reload
systemctl enable bluealsa-aplay
echo 'ACTION=="add", KERNEL=="hci0", RUN+="/bin/systemctl start bluealsa-aplay.service"' > /etc/udev/rules.d/61-bluealsa-aplay.rules
# Bluetooth udev script
cat <<'EOF' > /opt/local/bin/bluetooth-udev
#!/bin/bash
name=$(sed 's/\"//g' <<< $NAME)
if [[ ! $name =~ ^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$ ]]; then exit 0; fi
action=$(expr "$ACTION" : "\([a-zA-Z]\+\).*")
if [ "$action" = "add" ]; then
echo -e 'discoverable off\nexit\n' | bluetoothctl
ogg123 -q /usr/share/sounds/freedesktop/stereo/device-added.oga
# disconnect wifi to prevent dropouts
# ifconfig wlan0 down &
fi
if [ "$action" = "remove" ]; then
ogg123 -q /usr/share/sounds/freedesktop/stereo/device-removed.oga
# reenable wifi
# ifconfig wlan0 up &
echo -e 'discoverable on\nexit\n' | bluetoothctl
fi
EOF
chmod 755 /opt/local/bin/bluetooth-udev
cat <<'EOF' > /etc/udev/rules.d/99-bluetooth-udev.rules
SUBSYSTEM=="input", GROUP="input", MODE="0660"
KERNEL=="input[0-9]*", RUN+="/opt/local/bin/bluetooth-udev"
EOF
# Startup sound
cat <<'EOF' > /etc/systemd/system/startup-sound.service
[Unit]
Description=Startup sound
After=multi-user.target
[Service]
Type=simple
ExecStart=/usr/bin/ogg123 -q /usr/share/sounds/freedesktop/stereo/service-login.oga
[Install]
WantedBy=multi-user.target
EOF
systemctl enable startup-sound.service