Speaker

Bluetooth Speaker

Raspberry Pi : Connect Bluetooth Speaker #

Connecting Raspberry Pi to Mi Pocket Speaker 2

[bluetooth]# power on
Changing power on succeeded
[bluetooth]# agent on
Agent registered
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:79:F6:B7 Discovering: yes
[bluetooth]# default-agent
Default agent request successful
[CHG] Device 00:EC:0A:57:F2:65 LegacyPairing: yes
[bluetooth]# list
Controller B8:27:EB:79:F6:B7 raspberrypi [default]
[bluetooth]# devices
Device 5C:72:FE:B7:35:FD 5C-72-FE-B7-35-FD
Device 28:F0:76:0A:B9:7B 28-F0-76-0A-B9-7B
Device 00:EC:0A:57:F2:65 Redmi
[NEW] Device 74:A3:4A:15:DD:71 Mi Pocket Speaker 2
[bluetooth]# pair 74:A3:4A:15:DD:71
Attempting to pair with 74:A3:4A:15:DD:71
[CHG] Device 74:A3:4A:15:DD:71 Connected: yes
[CHG] Device 74:A3:4A:15:DD:71 UUIDs:
        00001108-0000-1000-8000-00805f9b34fb
        0000110b-0000-1000-8000-00805f9b34fb
        0000110c-0000-1000-8000-00805f9b34fb
        0000110e-0000-1000-8000-00805f9b34fb
        0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device 74:A3:4A:15:DD:71 Paired: yes
Pairing successful
[CHG] Device 74:A3:4A:15:DD:71 Connected: no
[bluetooth]# info 74:A3:4A:15:DD:71
Device 74:A3:4A:15:DD:71
        Name: Mi Pocket Speaker 2
        Alias: Mi Pocket Speaker 2
        Class: 0x240404
        Icon: audio-card
        Paired: yes
        Trusted: no
        Blocked: no
        Connected: no
        LegacyPairing: yes
        UUID: Headset                   (00001108-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: Handsfree                 (0000111e-0000-1000-8000-00805f9b34fb)
[bluetooth]# trust 74:A3:4A:15:DD:71
[CHG] Device 74:A3:4A:15:DD:71 Trusted: yes
Changing 74:A3:4A:15:DD:71 trust succeeded
[bluetooth]# info 74:A3:4A:15:DD:71
Device 74:A3:4A:15:DD:71
        Name: Mi Pocket Speaker 2
        Alias: Mi Pocket Speaker 2
        Class: 0x240404
        Icon: audio-card
        Paired: yes
        Trusted: yes
        Blocked: no
        Connected: no
        LegacyPairing: yes
        UUID: Headset                   (00001108-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: Handsfree                 (0000111e-0000-1000-8000-00805f9b34fb)
[bluetooth]# quit

pi@raspberrypi:~ $ sudo apt-get update && sudo apt-get install pulseaudio -y
pi@raspberrypi:~ $ mkdir -p scripts
pi@raspberrypi:~ $ vim scripts/autopair
#!/bin/bash
bluetoothctl << EOF
connect [enter your MAC add]
EOF
pi@raspberrypi:~ $ chmod +x !$
chmod +x scripts/autopair
pi@raspberrypi:~ $ sudo vim /boot/config.txt
#dtparam=audio=on
pi@raspberrypi:~ $ vim ~/scripts/on.py
#!/usr/bin/python

Monitor removal of bluetooth reciever #

import sys
import subprocess
import time
def blue_it():
    status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)
    while status == 0:
        print("Bluetooth UP")
        print(status)
        time.sleep(15)
        status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)
    else:
        waiting()
def waiting():
    subprocess.call('killall -9 pulseaudio', shell=True)
    time.sleep(3)
    subprocess.call('pulseaudio --start', shell=True)
    time.sleep(2)
    status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)
    while status == 2:
        print("Bluetooth DOWN")
        print(status)
        subprocess.call('~/scripts/autopair', shell=True)
        time.sleep(15)
        status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)
    else:
        blue_it()
blue_it()
pi@raspberrypi:~ $chmod +x ~/scripts/on.py
pi@raspberrypi:~ $vim ~/.bashrc
pulseaudio --start
wait
~/python/on.py

Reboot #

If the connection fails, remove the device and pair-trust-connect