开机自动连接蓝牙speaker

转自:https://raspberrypi.stackexchange.com/questions/53408/automatically-connect-trusted-bluetooth-speaker
Here is a command line solution:

First, let's scan, pair, trust your device with "bluetoothctl". To do that, run this at the command line, your terminal:

bluetoothctl -a
You should get a different command prompt like:

[bluetooth]
With your BT speaker on, type this:

scan on
In a few moments, you should see the BT devices available. Next to the device will be it's MAC address, like: 00:AA:22:BB:33. Now type this:

info
Exclude the greater than and less than characters. What your looking for is some kind of previous association with your BT speaker. You will know that there was a previous association because bluetoothctl will show information about your BT device. Some of this information will be about the device being paired and trusted. This is good.

If bluetoothctl complains about there is no device, then we need to set that up at this moment. To do that, type this:

pair
You should see a success message about your device pairing successfully. Now let's trust our new BT device. Type this:

trust
Again, you should see a success message about trusting. Let me pre-warn you. Your BT device might connect then again it might not. Never fear, we don't want it to connect. Go ahead and let's exit "bluetoothctl". To do that, type:

quit
Now you will be brought back to the command line prompt. In a previous post I suggested for you to create a scripts directory in your home directory. If you haven't, go ahead and do that now. Type this at the command prompt:

mkdir -p ~/scripts
Press enter and now let's create our autopair bash script. Type this:

nano ~/scripts/autopair
Enter this code into the script:

#!/bin/bash
bluetoothctl << EOF
connect [enter your MAC add]
EOF

Exclude the brackets!

Now press CTRL + x at the same time, and now press enter to save the script. We need to make it executable. To do that, type this:

chmod +x ~/scripts/autopair
I'm assuming that you don't use external analog speakers plug into the 3.5 mm jack. If this is true, let's disable alsa. To do that, let's edit a file in the /boot directory called config.txt. To do that, type this in your terminal:

sudo nano /boot/config.txt
Page down to the bottom of the file and look for two lines that read:

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

Place a (pound sign #) in front of the line that reads:

dtparam=audio=on
To look like:

#dtparam=audio=on
Press CTRL + x and then press Enter to save your file.

I'm assuming you have pulseaudio installed? If not, go ahead and run this command from the command line:

sudo apt-get update && sudo apt-get install pulseaudio -y
This will get you a very important component to making bluetooth work! Now let's edit our .bashrc file in our home directory. Type this:

nano ~/.bashrc
Page down to the bottom and add this line:

pulseaudio --start
Press CTRL + x and now press Enter to save your file.

OK! We need to enter over into the Python world. I have wrote a Python program that will watch for the bluetooth device. In short, it will activate the connection between RPi and your bluetooth speaker, once your bluetooth speaker is turned on. And vice versa. Let's create a directory called python in your home directory To do that, type this:

mkdir -p ~/python
Now let's create the python program file. To do that, type this:

nano ~/python/on.py
Inside that file, we need to copy and paste the following:

#!/usr/bin/python
#
# Monitor removal of bluetooth reciever
import os
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()

Now press CTRL + x and then press Enter to save the Python program file. Now we need to make this file executable. To do that, type this:

chmod +x ~/python/on.py
Finally, let's add this to our .bashrc script in our home directory:

nano ~/.bashrc
Page down to the bottom of the file and add these two lines:

wait
~/python/on.py

Now press CTRL + x and then press Enter to save. Turn your bluetooth speaker on and reboot your Raspberry Pi.

Good Luck!

-nitrolinux

你可能感兴趣的:(开机自动连接蓝牙speaker)