#!/usr/bin/python # $Id: telephommande,v 1.4 2007/08/16 16:54:41 matthias Exp $ # Author: Matthias Meulien # Check http://mapage.noos.fr/orontee/index.html """Usage: telephommande [OPTIONS] Control your computer from a SAGEM myX6-2 phone via Bluetooth. Use one of the following to specify a phone device: -a, --address=ADDRESS Connect to the phone at ADDRESS -n, --name=NAME Connect to the phone named NAME Various -d, --debug Print output of subprocesses -h, --help This help message -i, --info Print phone keys usage and exit""" import bluetooth import socket import re import subprocess import getopt import sys commands = {'left dot': 'totem --play-pause', \ 'right dot': 'totem --fullscreen', \ 'up': 'amixer set Master 4dB+', \ 'down': 'amixer set Master 4dB-', \ 'right': 'totem --seek-fwd', \ 'left': 'totem --seek-bwd', \ 'diaresis': 'amixer set Master toggle', \ 'hang-up': 'totem --quit'} key_names = {'91': 'left dot', '32': 'right dot', '94': 'up', '86': 'down', \ '77': 'right', '67':'left', '35': 'diaresis', '69':'hang-up'} def usage(): print __doc__ def lookup(name): # Get addreess of Bluetooth device by name try: devices = bluetooth.discover_devices() for bdaddr in devices: if name == bluetooth.lookup_name( bdaddr ): address = bdaddr print('Device '+name+' found at address '+address) return address except bluetooth.BluetoothError: print("Devices discovery failed") except KeyboardInterrupt: sys.exit() print('Device '+name+' not found') return None def connect(address, port): # Start connection to Bluetooth device global status try: socket.connect((address, port)) name = bluetooth.lookup_name(address) print("Connected to "+name) print("Press Ctrl-C to close connection") status = "connected" socket.send("AT+CMER=2,1,0,0,0\r") # Start key event reporting except bluetooth.BluetoothError: print("Connection to device "+address+" failed") except KeyboardInterrupt: sys.exit() def deconnect(): # Close connection to Bluetooth device socket.send("AT+CMER=2,0,0,0,0\r") # Stop key event reporting socket.close() def listen(): # Read data from socket and search for command to execute r = re.compile('\+CKEV. ([0-9]+),1') while status == "connected": try: data = socket.recv(256) match = r.search(data) if match: key_code = match.group(1) try: name = key_names[key_code] execute(commands[name]) except KeyError: print("Undefined key code :"+key_code) except KeyboardInterrupt: break def execute(string): try: p = subprocess.Popen([string], shell=True, stdout = subprocess.PIPE) except OSError: print("Command failed :"+string) if debug: output = p.stdout.read() print(output) def print_command_dictionary(dictionary): "Print a dictionary on two columns" padding = 2 width_1 = max(len(max(dictionary.keys(), key = len))+padding, 4) width_2 = max(len(max(dictionary.values(), key = len)), 8) # Compute width of columns print("Key".ljust(width_1)+"Command") print("-"*(width_1+width_2)) # Print header keys = dictionary.keys() keys.sort() for key in keys: print(key.ljust(width_1)+dictionary[key]) print "" if __name__ == "__main__": address, debug = False, False opts, args = getopt.gnu_getopt(sys.argv[1:], 'a:dhin:', \ ['address=', 'debug', 'help', 'info', 'name=']) opts.sort() for opt, a in opts: if opt in ('-a', '--address='): address = a elif opt in ('-d', '--debug'): debug = True elif opt in ('-n', '--name='): address = lookup(a) elif opt in ('-h', '--help'): usage() sys.exit() elif opt in ('-i', '--info'): print_command_dictionary(commands) if address: port = 20 socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM) status = "disconnected" connect(address, port) listen() deconnect() else: print('No address found for phone device') print('Try telephommande --help')