Hello
Just got a Rii i8+ mini keyboard from santa and I was able to get it working via Bluetooth on the Pi3 using the GUI to pair the device. However the device switches its self off after several minutes of inactivity and wont reconnect automatically. Also bluetooth seems to be disabled on reboot.
Im not sure why it is setup like that but I have written a couple of scripts which you can run at startup which will reconnect the bluetooth device if it looses connection.
So i have added the following script to /etc/rc.local to run on startup.
sudo nano /etc/rc.local
/home/osmc/scripts/cronfiveseconds.sh &
exit 0
And the script “cronfiveseconds.sh”:
#!/bin/sh
SHELL=/bin/sh
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/sbin:/usr/sbin:/usr/osmc/bin:/opt/vc/bin:/home/osmc/scripts
SLEEP=5
/home/osmc/scripts/bluetoothkeyboard.sh
sleep $SLEEP
/home/osmc/scripts/bluetoothkeyboard.sh
sleep $SLEEP
/home/osmc/scripts/bluetoothkeyboard.sh
sleep $SLEEP
/home/osmc/scripts/bluetoothkeyboard.sh
sleep $SLEEP
# echo and restart
So this script launches in background mode and will repeat evey 5 seconds. And the script to pair the bluetooth device:
/home/osmc/scripts/bluetoothkeyboard.sh
#!/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/sbin:/usr/sbin:/usr/osmc/bin:/opt/vc/bin:/home/osmc/scripts
sudo rfkill unblock 1
sudo hciconfig hci0 up
text="$(echo -e "info CC:C5:0A:21:C9:37\nquit" | bluetoothctl | grep "Connected" | cut --only-delimited --delimiter=' ' --fields=2)"
if [ "$text" = "no" ]; then
echo "no" > /dev/null
echo -e "connect CC:C5:0A:21:C9:37\nquit" | bluetoothctl
else
echo "yes" > /dev/null
fi
So i have added the PATH to my scripts because I initially had trouble launching the script via cron (probably was running and then being terminated rather than running in the background).
rfkill unblock 1
This part is necessarry to be able to bring up bluetoothctl. There is a softblock on the bluetooth, I think probably until it is enabled in the GUI. Bluetoothctl was reporting the bluetooth service was blocked by RFKill which was confusing as rfkill was not installed so:
sudo apt-get install rfkill
So once the soft block on the bluetooth (hci0) has been disabled it can then be started
sudo hciconfig hci0 up
Using bluetoothctl we get the connection status of the device, if the status is “no” it connects the bluetooth device using bluetoothctl, otherwise it does nothing.
Script is set to run every 5 seconds so that the keyboard can be quickly reconnected with the bluetooth button.
Hopefully this helps other folk trying to work with bluetooth because you really shouldnt have to launch the gui everytime you need to reconnect an already paired device.