[RaspberryPi3] Bluetooth : bouton pour rendre le raspberry détectable

Bluetooth : bouton pour rendre le raspberry détectable [RaspberryPi3] - Python - Programmation

Marsh Posté le 24-05-2017 à 09:46:13    

Bonjour,
 
Je cherche en ce moment sur le web en parti, comment faire un script et un montage (avec une résistance, une LED et un bouton) pour que quand j'appuie sur le bouton, la LED clignote et le Raspberry devient detéctable en bluetooth. Une fois appairé le LED reste fixe.
 
J'ai trouvé une piste ici :

Code :
  1. import bluetooth, time
  2. import Adafruit_BBIO.GPIO as GPIO
  3. search_time = 10
  4. led_pin = "P8_7"
  5. # You can hardcode the desired device ID here as a string to skip the discovery stage
  6. addr = None
  7. print("Welcome to the Bluetooth Detection Demo! \nMake sure your desired Bluetooth-capable device is turned on and discoverable." )
  8. if addr == None:
  9.     try:
  10.         input("When you are ready to begin, press the Enter key to continue..." )
  11.     except SyntaxError:
  12.         pass
  13.     print("Searching for devices..." )
  14.     nearby_devices = bluetooth.discover_devices(duration=search_time, flush_cache=True, lookup_names=True)
  15.     if len(nearby_devices) > 0:
  16.         print("Found %d devices!" % len(nearby_devices))
  17.     else:
  18.         print("No devices found! Please check your Bluetooth device and restart the demo!" )
  19.         exit(0)
  20.     i = 0 # Just an incrementer for labeling the list entries
  21.     # Print out a list of all the discovered Bluetooth Devices
  22.     for addr, name in nearby_devices:
  23.         print("%s. %s - %s" % (i, addr, name))
  24.         i =+ 1
  25.     device_num = input("Please specify the number of the device you want to track: " )
  26.     # extract out the useful info on the desired device for use later
  27.     addr, name = nearby_devices[device_num][0], nearby_devices[device_num][1]
  28. print("The script will now scan for the device %s." % (addr))
  29. print("Feel free to move near and far away from the BeagleBone to see the state change on the LED.\nUse Ctrl+c to exit..." )
  30. GPIO.setup(led_pin, GPIO.OUT)
  31. while True:
  32.     # Try to gather information from the desired device.
  33.     # We're using two different metrics (readable name and data services)
  34.     # to reduce false negatives.
  35.     state = bluetooth.lookup_name(addr, timeout=20)
  36.     services = bluetooth.find_service(address=addr)
  37.     # Flip the LED pin on or off depending on whether the device is nearby
  38.     if state == None and services == []:
  39.         print("No device detected in range..." )
  40.         GPIO.output(led_pin, GPIO.LOW)
  41.     else:
  42.         print("Device detected!" )
  43.         GPIO.output(led_pin, GPIO.HIGH)
  44.     # Arbitrary wait time
  45.     time.sleep(1)


 
 
Vous avez une idée ?

Reply

Marsh Posté le 24-05-2017 à 09:46:13   

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed