Nous allons voir comment détecter la présence d'un smartphone en utilisant les fonctionnalités bluetooth de la raspberry Pi
Avant de commencer la programmation Python, il nous faut récupérer l'adresse MAC de notre périphérique Bluetooth.
sudo apt-get install bluez
On a alors l'outils hcitool qui va nous permettre de récupérer l'adresse MAC de notre périphérique.
Nous allons associé un smartphone avec la raspberry PI, pour cela mettre le smartphone en appairage bluetooth et lancer la commande
hcitool scan
Vous devez alors obtenir une ligne du style :
Scanning ...
84:B5:41:XX:XX:XX <Marque Du Smartphone>
Cette ligne correspond à l'adresse MAC du périphérique bluetooth. Copiez-la, nous en aurons besoin plus loin.
J'ai rencontré beaucoup de problème lors de la gestion du bluetooth en python, c'est pourquoi je préconise l'utilisation de python3. Nous allons donc voir comment installer l'environnement et les librairies pour cette version de python.
sudo apt-get install python3-pip
sudo apt-get install libbluetooth-dev
sudo apt-get install python-dev
sudo pip3 install PyBluez
#!/usr/bin/python3
# -*- coding:utf-8 -*-'
''This programm test the bluetooth device'''
import bluetooth
bluetoothDevices = [{"name":"Test2", "mac":"84:B5:41:0B:EE:2E"}]
if __name__ == '__main__':
for bluetoothDevice in bluetoothDevices:
# Ask for the name of the bluetooth' mac adress for 5 seconds
result = bluetooth.lookup_name(bluetoothDevice['mac'], timeout=5)
# the bluetooth is alive
if (result is None):
print("Device %s detected" % bluetoothDevice['name'])