oldjake
Posts: 90
Joined: Fri Aug 03, 2018 12:39 pm

Bluetooth detection

Sun Oct 13, 2019 7:14 am

Hi

I've built a head unit for my car running Open Auto Pro. This mirrors Android Auto and my phone connects to the Pi via Bluetooth. To get round the issue of a clean shutdown without messing with hardware, I'm running a Python script to detect the phone on Bluetooth and run a shutdown if it's not detected. This is my code with some print statements and the shutdown command commented to help debug:

Code: Select all

#!/usr/bin/python2
import bluetooth
import time
from subprocess import call
while True:
        score = 0
        for x in range(15):
                result = bluetooth.lookup_name('xx:xx:xx:xx:xx:xx', timeout=5)
                if (result == None):
                        score = score +1
                        print "Not detected"
                        print score
                        if (score == 2):
                                call("echo 1 > /sys/class/backlight/rpi_backlight/bl_power", shell=True)
                                print "Screen off"
                        time.sleep(10)
                else:
                        score = 0
                        print "Detected"
                        print score
                        call("echo 0 > /sys/class/backlight/rpi_backlight/bl_power", shell=True)
                        print "Screen on again"
                        time.sleep(10)
        if (score == 15):
                #call("sudo shutdown -h 5", shell=True)
                print "Gone away forever"
        else:
                #call("sudo shutdown -c", shell=True)
                print "Back again"
        time.sleep(30)

So, running it from the command line at the moment with my phone in airplane mode and one iteration shows:

Code: Select all

0
Screen on again
Not detected
1
Not detected
2
Screen off
Not detected
3
Not detected
4
Not detected
5
Not detected
6
Not detected
7
Not detected
8
Not detected
9
Not detected
10
Not detected
11
Back again
Not detected
1
Not detected
2
Screen off
Not detected
3
Not detected
4
Not detected
5
Not detected
6
Not detected
You can see that the 11th iteration says "Back again" even though it says "Undetected". So it looks like it come out of the 15 x loop, seen that the score isn't 15 and then started again. This doesn't happen all the time, it's not always at iteration 11 but in the 4 times I've seen it it's been at 11 three times and 12 once.

I think my Python logic is "sound" but I'm wondering if there's a timing issue going on in the background with Bluetooth and Raspian and that's what's causing the problem. I'm going to leave it running. If it's always around the 11th iteration then I'll set my loop to 10 and extend the time period to see if it always happens around the same time rather than with the same iteration. Apart from that, I'm a bit lost for where you start.

Cheers
Andy

I'm not sure why it's doing this?

hippy
Posts: 6104
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Bluetooth detection

Sun Oct 13, 2019 5:44 pm

Dunno with a cursory inspection, but try printing 'x' out as well as 'score'. That may make it clearer what's going on.

oldjake
Posts: 90
Joined: Fri Aug 03, 2018 12:39 pm

Re: Bluetooth detection

Sun Oct 13, 2019 8:30 pm

Done a rework today. I have a bit of an on/off relationship with Python. It's just a hobby so I learn tons and then forget it all.

This is what I now have and it seems to work.

Code: Select all

#!/usr/bin/python2
import bluetooth
import time
from subprocess import call
timeout = 15
scoreOUT = 0
scoreIN = 0
while True:
        result = bluetooth.lookup_name('58:cb:52:79:7f:ba', timeout=5)
        if (result == None):
		scoreOUT = scoreOUT +1
		scoreIN = 0
		if (scoreOUT == 2):
			call("echo 1 > /sys/class/backlight/rpi_backlight/bl_power", shell=True)
		if (scoreOUT == timeout*60/10):
                	call("sudo shutdown -h now", shell=True)
		print "Score: ", scoreOUT
		time.sleep(10)
	else:
		scoreIN = scoreIN +1
		if (scoreIN > 1):
			call("echo 0 > /sys/class/backlight/rpi_backlight/bl_power", shell=True)
			scoreOUT = 0
			scoreIN = 0
		time.sleep(10)
So, it just keeps checking to see if my phone is connected. If it is disconnected for 15 minutes (or thereabouts), it shuts the Pi down. So I can power my Pi from the permanent live in the car without the battery going flat over night and without relying on any additional hardware. The 15 minute delay is just so that I can fill up with petrol without having to wait for the Pi to boot up again when I get back in the car. Turning the display off earlier saves me 400mA and means that I don't leave the car with a big, bright display advertising itself for too long.

I've tested it quite a bit today on the rig I have at home and it worked every time so I'm going to swap it with the one I already have in the car and see if it does the job. Just got to hope that the distance from where I park the car to the house is far enough for the Bluetooth to be out of range!

Return to “Advanced users”