×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    Send key event commands to other symbian Device over bluetooth

    Hi it is possible to write python script that will be able to send key code event commands to other symbian and python enabled device via bluetooth? Like Remote control of other device (Phone to Phone interface) I cant find this kind of script on this forum.I found chat scripts and remote RC car scripts but I need is kind the script that if i pressed (Up arrow, 5 key or green key) on First symbian device (6120c) that will send that commands to other symbian device (N82) and move menu UP ,pres 5 key or Dial number.
    Thanks and waiting for reply.

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    166

    Re: Send key event commands to other symbian Device over bluetooth

    try the keypress module
    it can simulate keypresses.

    send from one device to an other a var.
    the device that reads it has just to perform such an simulated keypress that's it

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    Re: Send key event commands to other symbian Device over bluetooth

    Thanks for reply AJ____92 .OK i find the socket and keypress module in wiki but! how to make it together work?? On S60 3rd FP1 platform?
    I try to edit the rfcomm_Server.py and rfcomm_client.py script but no success

    any help with this?? i am just beginner in Python scripting

    Here is the Server side "rfcomm_Server.py" edited by me

    Code:
    # this file lets 2 phones exchange data via RFCOMM
    # this file is the server side
    # the corresponding client side file is called rfcomm_client.py
    
    from socket import *
    import appuifw
    import key_codes
    
    
    server_socket = socket(AF_BT, SOCK_STREAM)
    p = bt_rfcomm_get_available_server_channel(server_socket)
    server_socket.bind(("", p))
    print "bind done"
    server_socket.listen(1)
    bt_advertise_service( u"RFCOMM", server_socket, True, RFCOMM)
    set_security(server_socket, AUTH)
    print "I am listening"
    
    # Note: Don't call .send or .recv on the server_socket!
    # Use the sock object returned by server_socket.accept().
    (sock,peer_addr) = server_socket.accept()
    print "Connection from %s"%peer_addr
    test = appuifw.query(u"Type words", "text", u"")
    sock.send(test+'\n')
    
    #----code i insert----
    
    import keypress
    from key_codes import *
    
    
    test2 = keypress.simulate_key(EKey5, EScancode5)
    
    
    sock.send(test2+'\n')
    
    print "send key command"
    
    #----End------
    
         
    
    print "sending done"
    
    
    # Allow time for data to be sent to work around a bug in the socket
    # module.
    Lock=e32.ao_lock()
    Lock.wait()
    sock.close()

    and Client side "rfcomm_Server.py" code Not edited

    Code:
    # this file lets 2 phones exchange data via RFCOMM
    # this file is the client side
    # the corresponding server side file is called rfcomm_server.py
    
    import socket
    import appuifw
    import e32
    
    class BTReader:
        def connect(self):
            self.sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
            addr,services=socket.bt_discover()
            print "Discovered: %s, %s"%(addr,services)
            if len(services)>0:
                import appuifw
                choices=services.keys()
                choices.sort()
                choice=appuifw.popup_menu([unicode(services[x])+": "+x
                                           for x in choices],u'Choose port:')
                port=services[choices[choice]]
            else:
                port=services[services.keys()[0]]
            address=(addr,port)
            print "Connecting to "+str(address)+"...",
            self.sock.connect(address)
            print "OK." 
        def readline(self):
            line=[]
            while 1:
                ch=self.sock.recv(1)
                if(ch=='\n'):
                    break
                line.append(ch)
            return ''.join(line)
        def close(self):
            self.sock.close()
    
    bt=BTReader()
    bt.connect()
    print "Received: "+bt.readline()
    bt.close()

    I try to run script but yes!! it make Error because i am just beginer that don't know how to make this work

    Error
    Code:
    In plat senc_import rise import error("No Module Named"+name)
    ImportError: No module named keypress
    Is here someone that can give me a little Help with this script??

    Please reply

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    166

    Re: Send key event commands to other symbian Device over bluetooth

    i made a simple script in just one night

    i used the BT-Chat sample from here
    and modified it a lil bit

    only problem was that somehow always the shift key is down ...
    and keypress only works fine in "symbian" not in any other app (atleast for me )

    Code:
    import appuifw
    import sys
    import e32
    try:
        # http://discussion.forum.nokia.com/forum/showthread.php?p=575213
        # Try to import 'btsocket' as 'socket' - ignored on versions < 1.9.x
        sys.modules['socket'] = __import__('btsocket')
    except ImportError:
        pass
    import socket
    
    from key_codes import *
    from key_modifiers import * 
    import keypress
    
    class Keyboard(object):
        global keyboard_on
        keyboard_on = 1
        def __init__(self,onevent=lambda:None):
            self._keyboard_state={}
            self._downs={}
            self._onevent=onevent
        def handle_event(self,event):
            if event['type'] == appuifw.EEventKeyDown:
                code=event['scancode']
                if not self.is_down(code):
                    self._downs[cod#e]=self._downs.get(code,0)+1      #please remove the #
                self._keyboard_state[cod#e]=1         #please remove the # otherwise  wont work ...
            elif event['type'] == appuifw.EEventKeyUp:
                self._keyboard_state[event['scancode']]=0
            self._onevent()
        def is_down(self,scancode):
            return self._keyboard_state.get(scancode,0)
        def pressed(self,scancode):
            if self._downs.get(scancode,0):
                self._downs[scancode]-=1
                return True
            return False
    
    keyboard=Keyboard()
    
    
    def con_server():
        server = socket.socket(socket.AF_BT, socket.SOCK_STREAM)
        channel = socket.bt_rfcomm_get_available_server_channel(server)
        server.bind(("", channel))
        server.listen(1)
        socket.bt_advertise_service(u"remote_ctr", server, True, socket.RFCOMM)
        socket.set_security(server, socket.AUTH | socket.AUTHOR)
        print "Waiting for client..."
        conn, client_addr = server.accept()
        print "Client connected!"
        server_get_key(conn)
        
    def con_client():
        conn = socket.socket(socket.AF_BT, socket.SOCK_STREAM)
        address, services = socket.bt_discover()
        if 'remote_ctr' in services:
            channel = services[u'remote_ctr']
            conn.connect((address, channel))
            print "Connected to server!"
            client_get_key(conn)
        else:
            appuifw.note(u"Target is not running a remote_ctr server", "error")
    
    
    def server_get_key(conn):
        fd = conn.makefile("rw", 0)
        while True:
            msg = fd.readline()
            if msg:
                print "Received: " + msg
    
                msg = msg[:msg.find(".")]
                
                if msg == "lsk":
                    keypress.simulate_key(EKeyLeftSoftkey,EKeyLeftSoftkey)
                if msg == "yes":
                    keypress.simulate_key(EKeyYes,EKeyYes)
                if msg == "menu":
                    keypress.simulate_key(EKeyMenu,EKeyMenu)
                if msg == "star":
                    keypress.simulate_key(EKeyStar,EKeyStar)
                if msg == "left":
                    keypress.simulate_key(EKeyLeftArrow,EKeyLeftArrow)
                if msg == "up":
                    keypress.simulate_key(EKeyUpArrow,EKeyUpArrow)
                if msg == "select":
                    keypress.simulate_key(EKeySelect,EKeySelect)
                if msg == "right":
                    keypress.simulate_key(EKeyRightArrow,EKeyRightArrow)
                if msg == "down":
                    keypress.simulate_key(EKeyDownArrow,EKeyDownArrow)
                if msg == "rsk":
                    keypress.simulate_key(EKeyRightSoftkey,EKeyRightSoftkey)
                if msg == "no":
                    keypress.simulate_key(EKeyNo,EKeyNo)
                if msg == "backspace":
                    keypress.simulate_key(EKeyBackspace,EKeyBackspace)
                if msg == "edit":
                    keypress.simulate_key(EKeyEdit,EKeyEdit)
                if msg == "hash":
                    keypress.simulate_key(EKeyHash,EKeyHash)
                if msg == "1":
                    keypress.simulate_key(EKey1,EKey1)
                if msg == "2":
                    keypress.simulate_key(EKey2,EKey2)
                if msg == "3":
                    keypress.simulate_key(EKey3,EKey3)
                if msg == "4":
                    keypress.simulate_key(EKey4,EKey4)
                if msg == "5":
                    keypress.simulate_key(EKey5,EKey5)
                if msg == "6":
                    keypress.simulate_key(EKey6,EKey6)
                if msg == "7":
                    keypress.simulate_key(EKey7,EKey7)
                if msg == "8":
                    keypress.simulate_key(EKey8,EKey8)
                if msg == "9":
                    keypress.simulate_key(EKey9,EKey9)
                if msg == "0":
                    keypress.simulate_key(EKey0,EKey0)
                         
           
    
    def client_get_key(conn):
        fd = conn.makefile("rw", 0)
        msg = "?"
        send = 1
        while True:
            if keyboard.pressed(EScancodeLeftSoftkey):
                msg = "lsk."
                send = 1
            if keyboard.pressed(EScancodeYes):
                msg = "yes."
                send = 1
            if keyboard.pressed(EScancodeMenu):
                msg = "menu."
                send = 1
            if keyboard.pressed(EScancodeStar):
                #msg = "*."
                msg = "menu."
                send = 1
            if keyboard.pressed(EScancodeLeftArrow):
                msg = "left."
                send = 1
            if keyboard.pressed(EScancodeUpArrow):
                msg = "up."
                send = 1
            if keyboard.pressed(EScancodeSelect):
                msg = "select."
                send = 1
            if keyboard.pressed(EScancodeRightArrow):
                msg = "right."
                send = 1
            if keyboard.pressed(EScancodeDownArrow):
                msg = "down."
                send = 1
            if keyboard.pressed(EScancodeRightSoftkey):
                msg = "rsk."
                send = 1
            if keyboard.pressed(EScancodeNo):
                msg = "no."
                send = 1
            if keyboard.pressed(EScancodeBackspace):
                msg = "backspace."
                send = 1
            if keyboard.pressed(EScancodeEdit):
                msg = "edit."
                send = 1
            if keyboard.pressed(EScancodeHash):
                #msg = "hash."
                msg = "rsk."
                send = 1           
            if keyboard.pressed(EScancode1):
                msg = "1."
                send = 1
            if keyboard.pressed(EScancode2):
                msg = "2."
                send = 1
            if keyboard.pressed(EScancode3):
                msg = "3."
                send = 1
            if keyboard.pressed(EScancode4):
                msg = "4."
                send = 1
            if keyboard.pressed(EScancode5):
                msg = "5."
                send = 1
            if keyboard.pressed(EScancode6):
                msg = "6."
                send = 1
            if keyboard.pressed(EScancode7):
                msg = "7."
                send = 1
            if keyboard.pressed(EScancode8):
                msg = "8."
                send = 1
            if keyboard.pressed(EScancode9):
                msg = "9."
                send = 1
            if keyboard.pressed(EScancode0):
                msg = "0."
                send = 1
    
            if send == 1:
                print >> fd, msg
                send = 0
            e32.ao_sleep(0.1)
            
            
        
    
    
        
    index = appuifw.popup_menu([u"New server", u"Connect to server"],
    u"Control mode")
    if index != None:
        if index:
            appuifw.app.body=c=appuifw.Canvas(event_callback=keyboard.handle_event)
            con_client()
        else:
            con_server()
    i hope it works
    the sever receives the events and simulates a keypress.
    Last edited by AJ____92; 2010-01-16 at 16:54. Reason: Added the code.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    Re: Send key event commands to other symbian Device over bluetooth

    Thanks AJ____92 Very sorry for my long response i lost my acount info i must reset my pass to be here again
    And very thanks for working script that i requested.Now i study your code and finaly i little understand it. And i read some tutorials about pys60 also
    But!! somethin strange is happening .If i try run script in N82 as server and try connect 6120c as client the python shell crashed!!! Script only work if 6120c is server and N82 is client (i think is the firmware problem beaucause even c++ apps crached with this settings ) So... Its possible to re-write this script that server (6120c) control client (N82) and either client (N82) control server (6120c)
    And about that control of application in foreground. I thing that trick with this will be in the send_raw_event() But I must ask samir (author of NOKMOTE) about this.He know how to control APP in foreground.
    With NOKMOTE you can control all apps even AnotherGuest's Liero s60 port :-)
    or i try ask y.a.k he updated the keypress module to version 1.0.6
    Thanks and please reply

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    166

    Re: Send key event commands to other symbian Device over bluetooth

    well i had the same problem with my N82 and N73 so i gave up :P
    wlan is my new target :P

    but i do not own a 2nd wlan device axcept my laptop so i can not test anything that goes phone 2 phone

    but i guess there is almost anything possible with BT, you can send accelerometer data which you can use on your 2nd device and the keypress ( sometimes i had problems in non python apps like xplore, if you have a solution for that please reply to me xD )

Similar Threads

  1. Replies: 1
    Last Post: 2010-02-12, 14:20
  2. Replies: 0
    Last Post: 2009-11-22, 19:03
  3. Bluetooth serviceDiscover problem Device Not Reachable
    By kashifqazi in forum Bluetooth Technology
    Replies: 1
    Last Post: 2006-10-13, 12:31
  4. send text serially over bluetooth
    By Amilael in forum Symbian Networking & Messaging (Closed)
    Replies: 3
    Last Post: 2006-10-04, 02:10
  5. Send key event
    By sverre80 in forum Symbian
    Replies: 0
    Last Post: 2005-11-09, 20:59

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×