HowTo - Reenable and pair to a Bluetooth Device automatically

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.

Never use rfkill on OSMC.

You can use connmanctl to enable or disable wireless technologies. If you also install rfkill you will create conflicts on your system as ConnMan and rfkill will fight each other for control over these devices.

Your tutorial shouldn’t be necessary at all – it looks like you just haven’t enabled BT via ConnMan / My OSMC.

I used the gui to connect to the bluetooth device, but once it disconnects it never reconnects. This is literally the only way I could get it to reconnect over ssh as the whole point of the thing is to be able to reconnect automatically.

If rfkill list shows this:

osmc@osmc2:~$ rfkill list
0: phy0: Wireless LAN
        Soft blocked: yes
        Hard blocked: no
1: hci0: Bluetooth
        Soft blocked: yes
        Hard blocked: no

How are you supposed to “power on” or “connect” in bluetoothctl because it kept on telling me access was denied or not available or whatever.

How do you unblock it with connman??

connmanctl enable bluetooth.

Hello

So how come when I enabled it in the gui it then doesnt enable it permanently, seems a bit of a design flaw?

Im just going to turn off my script and reboot to see if it works using “connmanctl enable bluetooth”

BT pairing via the GUI is experimental. We have plans to improve this. If you think you’ve found a bug, please post a log.

The command line method will work reliably, unless there is a problem with your adapter or device. This has been covered in the Wiki

No offence but it took me several hours last night to figure out how to get it working to automatically reconnect without manually launching the script and while i did see something that mentioned that the bluetooth support was experimental I saw absolutely nothing that mentioned connman and on the wiki page:

It is not mentioned at all, probably worth explicitly stating that connman will be needed to re-enable bluetooth after reboot.

That’s not quite what I said.

connmanctl should work reliably, and there may be instances where the GUI does not. However, so far we haven’t had reports of this issue in huge numbers or we would have addressed them. And currently we have no log to see what (if anything) needs addressing.

Well i had an issue with an update today (another post) and uploaded my logs here:

http://paste.osmc.io/ekowaroded

Your log shows segfaulting during upgrade.

You have a bad power supply, or PSU. Check your peripherals and reinstall.

Ok so:

connmanctl enable bluetooth

Does work but upon reboot if i try and bring the bluetooth up without re-enabling it:

sudo hciconfig hci0 up

I get the message:

Can't init device hci0: Operation not possible due to RF-kill (132)

You can see why i then decided to install rfkill, as googling suggested it was soft blocked and the way to undo this was with rfkill, didnt see anything in my googling which suggested this could be done with connman.

So it will need to be re-enables with connman and every reboot?
And i should purge rfkill?

complete reinstall or just the latest update?

You need to reinstall, and you need to fix your hardware.

I’m not sure why you would do this.

All of this should be unnecessary. I think you have a problem with your peripherals.

sudo hciconfig hci0 up

Is necessarry because every time i reboot the bluetooth does not appear to be started. I need to enable it, then it needs brought up, before I can even think about connecting to a bluetooth device.

It may be a power issue, but i have never used a usb charger than hasnt given me power warnings. The current power supply is an official samsung charger, if that doesnt provide enough power nothing will.

That’s your problem there.

The Samsung charger is designed to charge phones, not provide a stable 5V, 2.5A power supply. And as we’ve seen from Samsung, they need some of their phones to charge slowly…

You need an official power supply. You can get an official Pi one, or one that we sell.

Almost all issues regarding stability, update issues and performance problems revolve around two things. A bad SD card or a bad power supply.

You need to fix your hardware first before looking at anything else.

The problem here is that it almost certainly is a power issue. When you don’t tell us about the warnings and ignore them, you waste time that can be spent better elsewhere, and you make it harder to get to the bottom of the problem.

So what my bluetooth is not up on reboot because there isnt enough power?

Ok so using a better samsung power supply (for an s6 which is supposed to charge quickly so should fully powered) the bluetooth comes up without having to re-enable it but it still doesnt reconnect automatically.

Which if i am having to run a script to reconnect the device, making sure it is up before hand is no biggie, even if I am running on an under powered usb.

This is the first time i have seen anywhere explicitly stated about OSMC that the os will disable peripherals on startup if it detects there is not enough power.
Again maybe something to state in the wiki, ive seen plenty of “make sure your power supply is up to scratch posts” but nothing which explicitly states why it might make any difference.

An explicit statement that an underpowered pi may lead to peripherals and services not starting properly may actually have encouraged me to by a proper power supply.

This means that you had an issue with the other power supply.

The Galaxy S6 power supply still doesn’t meet the requirements for the Raspberry Pi 3. I would strongly recommend getting a better power supply.

I did not say that.

I suspect the board is not receiving enough power at boot when power domains are being brought up. This could create timing problems uploading the BT firmware via SDIO. The fact is, if the board doesn’t have enough power, behaviour will be unpredictable.

The importance of a good power supply is thoroughly documented in the Raspberry Pi Wiki: Frequently Asked Questions - Raspberry Pi - OSMC.

Until you are using a proper power supply, I can’t help you any further.

This is going to give you many, many problems down the line. At some point you have to value your time (several hours), over the cost of a power supply.

I bought a dedicated power supply for this pi (pi3) which still gave me power warnings. Admittedly it probably wasnt from the pi shop or whatever, it was probably from amazon, but it was listed as being specifically for the pi though. And my other pi is running off a powered usb hub with nothing else plugged into it, if that doesnt provide enough power then I dont know what does.

What i am saying is that i have used several different power supplies, some phone charges, others not. And none of them seemed to be good enough, yet I have not really had significant problems:

Random freezes or glitches
Unexpected network dropouts
Randomly seeing external drives unmount and remount (caused by dips in power)
Repeat remote presses

I have had it freeze maybe occasionally but generally its in an addon and i would usually chalk it up to the addon being the proplem and reboot the pi. But nowhere does the wiki mention that peripherals may fail to start or may stop. Not explicitly.
Maybe to someone like you who built OSMC this sort of effect is obvious but as someone not inexperienced with computers but not a linux expert, i never would have thought that the os wouldnt load a peripheral device because of power issues.
And especially not something onboard like the bluetooth on the pi3.

So my geberal thinking was i am not going to go out and buy more power supplies, which also may be under powered, until I know why I should. Its not like i have a voltmeter to test it with or anything.

But this is the first time that specific effects of an under powered pi have been described to me. Im just used to seeing “get a new power supply” posts which if you dont know why you should arent going to convince anyone to actually do it. And im not one to throw good money after bad.

Im not trying to pick a fight, im just saying that up until now anything i have read about the power and osmc has really not been that useful or informative about the actual issues it may cause.

It may have been for an older version of the Raspberry Pi.

I don’t believe the USB specification permits a power output high enough to power your Pi from a USB powered hub. The power will also depend on the specification of the hub.

If you buy from us, or buy the official Pi model, you will get no warnings and things will work.

The Wiki explains the symptoms you are experiencing:

Peripherals can be hard drives, USB dongles, Bluetooth adapters. Bluetooth adapters send and receive network traffic, so I believe it’s covered under:

But I am not going to nitpick here. I’ve already told you how to resolve the problem, or at the very least, what needs to be done before we can proceed further.

Then you should really have tried this first. If it’s mentioned often enough, why would you think it wouldn’t apply to your issue?

The Wiki can be edited by other users. I have only written that section in an effort to summarise the most common issues. It would be difficult to provide an exhaustive list of potential problems that can occur as a result of using an inadequate power supply. You can suggest edits to the Wiki if you would like to improve it.