Installer une communication bluetooth sur Raspberry

Mise en oeuvre du Bluetooth sur Raspberry

Installation des paquets :

sudo apt-get install -y bluetooth bluez-utils blueman python-bluez bluez bluez-hcidump
sudo apt-get install python-pip python-dev
sudo pip install evdev

ou (à tester)

sudo apt-get install --no-install-recommends bluetooth # Installe uniquement les paquets indispensables

Vérification de l’installation

sudo service bluetooth status

Vérification de la prise en charge du dongle par le driver

sudo lsusb

Communication avec une wiimote

La wiimote utilise le bluetooth comme couche physique de communication, il est donc possible de mapper celle-ci sur un raspberry en utilisant la bibliothèque CWiid.
Installation du paquet :

sudo apt-get install python-cwiid

Démarrage du scan bluetooth

sudo hcitool scan # Retourne 00:1A:E9:34:D2:33 Nintendo RVL-CNT-01

Example de script Python ( Presser les boutons ‘-’ et ‘+’ de la wiimote pour quitter )

#!/usr/bin/python
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#
# wii_remote_1.py
# Connect a Nintendo Wii Remote via Bluetooth
# and  read the button states in Python.
#
# Project URL :
# http://www.raspberrypi-spy.co.uk/?p=1101
#
# Author : Matt Hawkins
# Date   : 30/01/2013
 
# -----------------------
# Import required Python libraries
# -----------------------
import cwiid
import time
 
button_delay = 0.1
 
print 'Press 1 + 2 on your Wii Remote now ...'
time.sleep(1)
 
# Connect to the Wii Remote. If it times out
# then quit.
try:
  wii=cwiid.Wiimote()
except RuntimeError:
  print "Error opening wiimote connection"
  quit()
 
print 'Wii Remote connected...\n'
print 'Press some buttons!\n'
print 'Press PLUS and MINUS together to disconnect and quit.\n'
 
wii.rpt_mode = cwiid.RPT_BTN
 
while True:
 
  buttons = wii.state['buttons']
 
  # If Plus and Minus buttons pressed
  # together then rumble and quit.
  if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
    print '\nClosing connection ...'
    wii.rumble = 1
    time.sleep(1)
    wii.rumble = 0
    exit(wii)
 
  # Check if other buttons are pressed by
  # doing a bitwise AND of the buttons number
  # and the predefined constant for that button.
  if (buttons & cwiid.BTN_LEFT):
    print 'Left pressed'
    time.sleep(button_delay)
 
  if(buttons & cwiid.BTN_RIGHT):
    print 'Right pressed'
    time.sleep(button_delay)
 
  if (buttons & cwiid.BTN_UP):
    print 'Up pressed'
    time.sleep(button_delay)
 
  if (buttons & cwiid.BTN_DOWN):
    print 'Down pressed'
    time.sleep(button_delay)
 
  if (buttons & cwiid.BTN_1):
    print 'Button 1 pressed'
    time.sleep(button_delay)
 
  if (buttons & cwiid.BTN_2):
    print 'Button 2 pressed'
    time.sleep(button_delay)
 
  if (buttons & cwiid.BTN_A):
    print 'Button A pressed'
    time.sleep(button_delay)
 
  if (buttons & cwiid.BTN_B):
    print 'Button B pressed'
    time.sleep(button_delay)
 
  if (buttons & cwiid.BTN_HOME):
    print 'Home Button pressed'
    time.sleep(button_delay)
 
  if (buttons & cwiid.BTN_MINUS):
    print 'Minus Button pressed'
    time.sleep(button_delay)
 
  if (buttons & cwiid.BTN_PLUS):
    print 'Plus Button pressed'
    time.sleep(button_delay)

So, what do you think ?

You must be logged in to post a comment.