Changing bluetooth MAC address:
Source: https://scribles.net/changing-a-bluetooth-device-address-on-raspberry-pi/
Download the bdaddr. I cannot find the source of this file. It may be not safe to run.
You need to install the build-essential, wget, libbluetooth-dev package using apt install.


$ wget -U firefox http://www.petrilopia.net/wordpress/wp-content/uploads/bdaddrtar.bz2


--2019-08-12 09:47:23--  http://www.petrilopia.net/wordpress/wp-content/uploads/bdaddrtar.bz2
Resolving www.petrilopia.net (www.petrilopia.net)... 94.199.56.47
Connecting to www.petrilopia.net (www.petrilopia.net)|94.199.56.47|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3901 (3.8K) [application/x-bzip2]
Saving to: ‘bdaddrtar.bz2’

bdaddrtar.bz2                   100%[====================================================>]   3.81K  14.5KB/s    in 0.3s

2019-08-12 09:47:24 (14.5 KB/s) - ‘bdaddrtar.bz2’ saved [3901/3901]

$ tar xvf bdaddrtar.bz2
bdaddr/
bdaddr/bdaddr.c
bdaddr/oui.h
bdaddr/oui.c
bdaddr/Makefile

$ cd bdaddr/
$ ls
bdaddr.c  Makefile  oui.c  oui.h

$ make
gcc -c bdaddr.c
bdaddr.c:35:10: fatal error: bluetooth/bluetooth.h: No such file or directory
 #include 
          ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:11: bdaddr.o] Error 1


The error was caused by not installing the libbluetooth-dev package.

$ sudo apt install libbluetooth-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  libbluetooth-dev
0 upgraded, 1 newly installed, 0 to remove and 4 not upgraded.
Need to get 190 kB of archives.
After this operation, 524 kB of additional disk space will be used.
Get:1 http://archive.raspberrypi.org/debian buster/main armhf libbluetooth-dev armhf 5.50-1+rpt1 [190 kB]
Fetched 190 kB in 3s (66.1 kB/s)
Selecting previously unselected package libbluetooth-dev:armhf.
(Reading database ... 74026 files and directories currently installed.)
Preparing to unpack .../libbluetooth-dev_5.50-1+rpt1_armhf.deb ...
Unpacking libbluetooth-dev:armhf (5.50-1+rpt1) ...
Setting up libbluetooth-dev:armhf (5.50-1+rpt1) ...


$ make
gcc -c bdaddr.c
gcc -c oui.c
gcc -o bdaddr bdaddr.o oui.o -lbluetooth

$ bluetoothctl
[bluetooth] power on
[bluetooth] quit

$ ./bdaddr -i hci0
Manufacturer:   Cypress Semiconductor Corporation (305)
Device address: B8:27:XX:XX:XX:XX

Upon trying to change the MAC address:
$ sudo ./bdaddr -i hci0 -r 47:XX:XX:XX:XX:XX -t
Manufacturer:   Cypress Semiconductor Corporation (305)
Device address: B8:27:XX:XX:XX:XX

Unsupported manufacturer

I will try to find other ways to change the MAC address but I will stop trying for now.


$ sudo systemctl status bluetooth
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-08-12 08:58:14 PST; 4min 41s ago
     Docs: man:bluetoothd(8)
 Main PID: 1202 (bluetoothd)
   Status: "Running"
    Tasks: 1 (limit: 2319)
   Memory: 2.0M
   CGroup: /system.slice/bluetooth.service
           └─1202 /usr/lib/bluetooth/bluetoothd

Aug 12 08:58:10 systemd[1]: Starting Bluetooth service...
Aug 12 08:58:12  bluetoothd[1202]: Bluetooth daemon 5.50
Aug 12 08:58:14  systemd[1]: Started Bluetooth service.
Aug 12 08:58:14  bluetoothd[1202]: Starting SDP server
Aug 12 08:58:15  bluetoothd[1202]: Bluetooth management interface 1.14 initia
Aug 12 08:58:15  bluetoothd[1202]: Sap driver initialization failed.
Aug 12 08:58:15  bluetoothd[1202]: sap-server: Operation not permitted (1)
Aug 12 08:58:15  bluetoothd[1202]: Failed to set privacy: Rejected (0x0b)

If you have some problem with SAP error on the Pi, you can edit the systemd service and replace ExecStart with this:


$ sudo nano /etc/systemd/system/bluetooth.target.wants/bluetooth.service
ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap

Using Pi 3B+ as a Poor Man's Bluetooth Speaker

We will use the pulseadio here not ALSA.
Install pulseaduio packages and bluetooth packages:


$ sudo apt-get install pulseaudio pulseaudio-module-bluetooth pi-bluetooth bluetooth  bluez-tools

Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  bluez-cups bluez-obexd
The following NEW packages will be installed:
  bluetooth
0 upgraded, 1 newly installed, 0 to remove and 4 not upgraded.
Need to get 0 B/43.0 kB of archives.
After this operation, 70.7 kB of additional disk space will be used.
Selecting previously unselected package bluetooth.
(Reading database ... 74020 files and directories currently installed.)
Preparing to unpack .../bluetooth_5.50-1+rpt1_all.deb ...
Unpacking bluetooth (5.50-1+rpt1) ...
Setting up bluetooth (5.50-1+rpt1) ..

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
 bluez-obexd libical3
The following NEW packages will be installed:
 bluez-obexd bluez-tools libical3
0 upgraded, 3 newly installed, 0 to remove and 4 not upgraded.
Need to get 515 kB of archives.
After this operation, 2,343 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.raspberrypi.org/debian buster/main armhf bluez-obexd armhf 5.50-1+rpt1 [180 kB]
Get:2 http://raspbian.raspberrypi.org/raspbian buster/main armhf libical3 armhf 3.0.4-3 [227 kB]
Get:3 http://raspbian.raspberrypi.org/raspbian buster/main armhf bluez-tools armhf 2.0~20170911.0.7cb788c-2 [108 kB]
Fetched 515 kB in 6s (91.3 kB/s)
Selecting previously unselected package libical3:armhf.
(Reading database ... 74075 files and directories currently installed.)
Preparing to unpack .../libical3_3.0.4-3_armhf.deb ...
Unpacking libical3:armhf (3.0.4-3) ...
Selecting previously unselected package bluez-obexd.
Preparing to unpack .../bluez-obexd_5.50-1+rpt1_armhf.deb ...
Unpacking bluez-obexd (5.50-1+rpt1) ...
Selecting previously unselected package bluez-tools.
Preparing to unpack .../bluez-tools_2.0~20170911.0.7cb788c-2_armhf.deb ...
Unpacking bluez-tools (2.0~20170911.0.7cb788c-2) ...
Setting up bluez-tools (2.0~20170911.0.7cb788c-2) ...
Setting up libical3:armhf (3.0.4-3) ...
Setting up bluez-obexd (5.50-1+rpt1) ...
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for libc-bin (2.28-10+rpi1) ...

Add user to the lp group:


$ sudo usermod -a -G lp user
$ newgrp lp

Verify:


$ groups | grep lp

user adm lp

Changing the bluetooth visible name:
Edit the file:


$ sudo nano /etc/machine-info

PRETTY_HOSTNAME=PiSpeaker

Configure bluetooth:


$ sudo nano /etc/bluetooth/main.conf

Class = 0x00041C
DiscoverableTimeout = 0

Configure bluetooth for audio:


$ sudo nano /etc/bluetooth/audio.conf

[General]:
Enable=Source,Sink,Media,Socket

Restart bluetooth:


sudo systemctl restart bluetooth
</code

Configure pulseaudio:
More info: https://wiki.debian.org/PulseAudio


$ sudo nano /etc/pulse/daemon.conf

resample-method = trivial

Test Pi's bluetooth, you need to pair with it:


$ bluetoothctl

[bluetooth]# power on
Changing power on succeeded

[bluetooth]#  discoverable on
Changing discoverable on succeeded
[CHG] Controller B8:XX:XX:XX:XX:XX Discoverable: yes

[bluetooth]#  pairable on
Changing pairable on succeeded

[bluetooth]# agent on
Agent is already registered

[bluetooth]# default-agent
Default agent request successful
[NEW] Device BX:XX:XX:XX:XX:XX Android
[CHG] Device BX:XX:XX:XX:XX:XX Paired: yes

[bluetooth]# trust BX:XX:XX:XX:XX:XX
CHG] Device  BX:XX:XX:XX:XX:XX Trusted: yes
Changing BX:XX:XX:XX:XX:XX trust succeeded

[bluetooth]# quit
/pre>

Start pulseaudio manually:

$  pulseaudio --start

Please read the references for other configuration options. (No Authentication)

Adding PIN for pairing (configuration 5):


$ sudo nano /etc/bluetooth/pin.conf

*	123456


$ sudo chmod 600 /etc/bluetooth/pin.conf

$ sudo nano /etc/systemd/system/bt-agent.service


[Unit]
Description=Bluetooth Auth Agent
After=bluetooth.service
PartOf=bluetooth.service

[Service]
Type=simple
ExecStart=/usr/bin/bt-agent -c NoInputNoOutput -p /etc/bluetooth/pin.conf
ExecStartPost=/bin/sleep 1
ExecStartPost=/bin/hciconfig hci0 sspmode 0

[Install]
WantedBy=bluetooth.target


$ sudo systemctl daemon-reload
$ sudo systemctl restart bt-agent

$ sudo systemctl status bt-agent

● bt-agent.service - Bluetooth Auth Agent
   Loaded: loaded (/etc/systemd/system/bt-agent.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-08-12 11:35:53 PST; 29min ago
 Main PID: 1345 (bt-agent)
    Tasks: 3 (limit: 2319)
   Memory: 1.7M
   CGroup: /system.slice/bt-agent.service
           └─1345 /usr/bin/bt-agent -c NoInputNoOutput -p /etc/bluetooth/pin.conf

Aug 12 10:35:52 - systemd[1]: Starting Bluetooth Auth Agent...
Aug 12 10:35:53 - systemd[1]: Started Bluetooth Auth Agent.
Aug 12 10:35:56 - bt-agent[1345]: Agent registered
Aug 12 10:35:56 - bt-agent[1345]: Default agent requested


I seem to have problem with the systemctl pulseaudio:


$ systemctl --user enable pulseaudio
Failed to connect to bus: No such file or directory

I just created a new systemd service file and enable it:
This will run pulseaudio with user privileges not root. Alternatively, you can just execute pulseaudio --start in cron or rc.local.


$ sudo nano /etc/systemd/system/pulseaudio.service

[Unit]
Description=PulseAudio Daemon
After=bluetooth.service
PartOf=bluetooth.service

[Install]
WantedBy=multi-user.target

[Service]
Type=simple
PrivateTmp=true
ExecStart=/bin/su  user -c '/usr/bin/pulseaudio --start'
TimeoutSec=45
Restart=on-failure
RestartSec=30

Enable and start on boot:

Verify if pulse audio is running:


$ ps -ef|grep PulseAudio

user       12528     1 11 10:58 ?        00:00:39 /usr/bin/pulseaudio --start

Enjoy!

References:
https://raspberrypi.stackexchange.com/questions/47708/setup-raspberry-pi-3-as-bluetooth-speaker
https://www.raspberrypi.org/forums/viewtopic.php?t=235519