How to initialize Bluetooth in a startup script with Yocto Poky Linux

5k Views Asked by At

I have a script that initializes my bluetooth setup on an Intel Edison. It allows pairing and connecting to this headless machine running Yocto Poky Linux. It was suggested that I put the startup script in /etc/init.d and run update-rc.d myscript.sh defaults. The script ran but it didn't work (generated boot errors saying bluetooth device not found) because Bluetooth had not started yet. I did some reasearch and after removing my links I did update-rc.d myscript.sh defaults 99 which was claimed to run the script last but it did't make any differrence -- it still ran in the same place in the boot sequence. I verified that the links had S99 on them so it seemed like they were set up correctly. There is another post on SO asking a similar question but that was a Ubuntu system where mine is Poky Linux. That solution suggested putting the startup script in a directory that does not exist on my system. There were other suggestions, putting it in rc.local, which I did and got the same result, it runs before Bluetooth is initialized.

Here is my script. My program is called nmea_thread and is run last. Everything else is initializing Bluetooth.

#!/bin/sh
/usr/sbin/rfkill unblock bluetooth
/usr/bin/hciconfig hci0 up
/usr/bin/hciconfig hci0 piscan 
/usr/bin/hciconfig hic0 sspmode 0
/home/root/simpleAgent/simple-agent &
/home/root/nmea_thread 
3

There are 3 best solutions below

0
werewindle On

Often bluetooth is initialized asynchronously, so you can't be sure that your script will be run after hci0 is added. Good solution is to wait for BT initialization in background:

#!/bin/bash

if [ "$1" != "background" ]; then
     $0 background &
else
    #Wait until BT is initialized
    for ((i = 0; i <= 100; i++)) do
      hciconfig hci0 && break
      usleep 100000
    done
    /usr/sbin/rfkill unblock bluetooth
    /usr/bin/hciconfig hci0 up
    /usr/bin/hciconfig hci0 piscan 
    /usr/bin/hciconfig hic0 sspmode 0
    /home/root/simpleAgent/simple-agent &
    /home/root/nmea_thread 
fi
0
SHERIF OMRAN On

hciattach is the correct way. syntax

hciattach /dev/ttyAMA0 bcm43xx 3000000

0
SHERIF OMRAN On

you need to flash the driver first before initializing it. Currently i don't remember how, but thats how i made it with raspberry pi and yocto. Note if you use systemV, you can do call it from a script and it will work Using SystemD, you need to make it in a service and wait. Falshing should be done in the two cases.