You are not logged in.

#1 2010-04-26 19:13:38

Busata
Member
From: Belgium
Registered: 2010-04-04
Posts: 30
Website

Control laptop with bluetooth without java!

Hai, was cleaning up my subdirectory mess, and bumped into a mini mini project from some weeks ago, I was trying to remote control my laptop with my cheap cellphone (it costed 19 euros, included was 12 euros call credit!). As I couldn't get any of the java clients working on it, I quickly wrote a python script that parsed hcidump. It requires the bluetooth daemon, and filesharing. The shared folder contains subfolders which represent commands smile. Basically you connect with the phone to the share, browsing a folder = executing a command.

I post the script just under the "it worked for me" ,  it might give others an idea for a work around when java stuff on your phone doesn't work. Be aware that hcidump requires root privileges, so you need to run it as root, so don't count on it being safe smile

.
#!/usr/bin/env python
#remote.py

""" Author: Dries De Smet
"""

import subprocess
import shlex

cmd = ['hcidump','-a','rfcomm']

def launchLocalCommand(command):
    command = shlex.split(command)
    return subprocess.Popen(command)

commands = {}
commands["ToggleMute"]="amixer set Master toggle"
commands["volumeDown"]="amixer set Master 5- unmute"
commands["volumeUp"]="amixer set Master 5+ unmute"
commands["Next"]="mpc next"
commands["Previous"]="mpc prev"
commands["TogglePlay"]="mpc toggle"
commands["Shutdown"]="shutdown -h now"
commands["Restart"]="shutdown -r now"

process = subprocess.Popen(cmd,shell=False,stdout=subprocess.PIPE)

append = False
rfcommMsg = ""

while 1:
    line = process.stdout.readline().split("\n")[0]
    if not line:
        break
    else:
        if "> RFCOMM" in line:
            append = True
            continue
        
        if "< RFCOMM" in line:
            append = False
            msg = rfcommMsg.replace(" ","").replace(".","").replace("#"," ").strip().rstrip()
            
            if msg in commands.keys():
                launchLocalCommand(commands[msg])
                print msg
            
            
            
            rfcommMsg = ""
            continue
        
        if append is True:
            rfcommMsg = ''.join((rfcommMsg,line))
        
        
            

process.close()

Offline

#2 2010-04-27 06:14:22

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Control laptop with bluetooth without java!

Very nice script, Busata.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#3 2010-04-28 18:56:09

riivo
Member
Registered: 2008-08-25
Posts: 112

Re: Control laptop with bluetooth without java!

What could be the reason for getting

~ > ./remote.py
Traceback (most recent call last):
  File "./remote.py", line 26, in <module>
    process = subprocess.Popen(cmd,shell=False,stdout=subprocess.PIPE)
  File "/usr/lib/python2.6/subprocess.py", line 633, in __init__
    errread, errwrite)
  File "/usr/lib/python2.6/subprocess.py", line 1139, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

Edit: never mind. I was missing bluez-hcidump.

Last edited by riivo (2010-04-28 19:52:14)

Offline

Board footer

Powered by FluxBB