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

Detecting a Tile

Fri Nov 23, 2018 12:15 pm

Hi

I've got a Pi that does some occupancy detection so that I can mess with the heating depending on who's in.

So, it's doing this:

Code: Select all

andyResult = bluetooth.lookup_name('xx:xx:xx:xx:xx:xx', timeout=10)
I get the MAC address of the device by running:

Code: Select all

sudo hcitool scan
This works fine BUT...

If I have Bluetooth on my phone switched on all the time, it kills the WiFi speed (a known problem with some phones). So I bought one of these little tiles.

The MAC address appears when I do:

Code: Select all

sudo hcitool lescan
But when I run the code in my Pythin script on the BLE MAC address for the tile, it fails.

I'm guessing that it doesn't scan for BLE.

Is there another way I can do this?

Cheers
Andy

DirkS
Posts: 10447
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Detecting a Tile

Fri Nov 23, 2018 1:41 pm

oldjake wrote:
Fri Nov 23, 2018 12:15 pm
But when I run the code in my Pythin script on the BLE MAC address for the tile, it fails.
What code? Which module(s)?

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

Re: Detecting a Tile

Fri Nov 23, 2018 6:27 pm

I think the module Bluez? It's a while since I installed it as it sort of works so I haven't worried until now.

Code is as my original post:

Code: Select all

andyResult = bluetooth.lookup_name('xx:xx:xx:xx:xx:xx', timeout=10)
It's just that one line. Then after that, it's:

Code: Select all

if (andyResult != None):
            status = "In"
Phone BLE address - works a treat
Tile MAC address - doesn't work

User avatar
Douglas6
Posts: 4950
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Detecting a Tile

Fri Nov 23, 2018 6:42 pm

That looks like code from the PyBluez Python module, which, as you've discovered, does not support BLE. You're going to want to look into Ian Harvey's bluepy module, which does support scanning for BLE devices, if memory serves, by way of a Scanner object.

User avatar
bensimmo
Posts: 4988
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Detecting a Tile

Fri Nov 23, 2018 9:04 pm

Is there anything in BlueDot that can be used ?
https://bluedot.readthedocs.io/

written by one (or two) of the RPF regular python programmers https://github.com/martinohanlon/BlueDot

User avatar
Douglas6
Posts: 4950
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Detecting a Tile

Fri Nov 23, 2018 9:19 pm

No, Blue Dot uses strictly classic SSP communications. A fine program in its own right. You gotta love an app whose only interface is a big blue dot.

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

Re: Detecting a Tile

Fri Nov 23, 2018 9:52 pm

Aha! Bluepy looks good. I've just installed that and looked at the scanner class. That looks like it scans for what's around.

What I can't suss (admittedly, I've only just started looking) is how to do the equivalent of the single line that I use at the moment to do a sort of 'ping' on one address.

The good thing is that I can see my tile and some general info from the command line blescan.

User avatar
Douglas6
Posts: 4950
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Detecting a Tile

Fri Nov 23, 2018 10:13 pm

Might be two lines instead of one:
1. devices = Scanner.scan(timeout)
2. if bdaddr in devices:
(do something)

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

Re: Detecting a Tile

Sat Nov 24, 2018 9:16 am

Thanks, Douglas6

I couldn't quite get your example to work but it pointed me in the right direction. I've fudged something together from the documentation which does work. It's probably overcomplicated in the extreme, and I'm not even going to pretend that I get what's happening at this stage, but it does the job for the moment.

Code: Select all

from bluepy.btle import Scanner, DefaultDelegate
import bluetooth
class ScanDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleDiscovery(self, dev, isNewDev, isNewData):
        if isNewDev:
            print "Discovered device", dev.addr
            if "xx:xx:xx:xx:xx:xx" in dev.addr:
                print "Found Tile"
            else:
                print "Tile not found"
        elif isNewData:
            print "Received new data from", dev.addr

scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)
I have emailed the Tile tech support people and asked about this. They told me that it was impossible... the Tile doesn't work like that. Not sure whether to e-mail them back and tell them to the good news or not.

Cheers
Andy

Return to “Python”