src/extras/examples/simplebtconsole.py
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 # A simple interactive console over Bluetooth.
       
     2 
       
     3 # Copyright (c) 2005 Nokia Corporation
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #     http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 
       
    18 import socket
       
    19 
       
    20 class socket_stdio:
       
    21     def __init__(self,sock):
       
    22         self.socket=sock
       
    23     def read(self,n=1):
       
    24         return self.socket.recv(n)
       
    25     def write(self,str):
       
    26         return self.socket.send(str.replace('\n','\r\n'))
       
    27     def readline(self,n=None):
       
    28         buffer=[]
       
    29         while 1:
       
    30             ch=self.read(1)
       
    31             if ch == '\n' or ch == '\r':   # return
       
    32                 buffer.append('\n')
       
    33                 self.write('\n')
       
    34                 break
       
    35             if ch == '\177' or ch == '\010': # backspace
       
    36                 self.write('\010 \010') # erase character from the screen
       
    37                 del buffer[-1:] # and from the buffer
       
    38             else:
       
    39                 self.write(ch)
       
    40                 buffer.append(ch)
       
    41             if n and len(buffer)>=n:
       
    42                 break
       
    43         return ''.join(buffer)
       
    44     def raw_input(self,prompt=""):
       
    45         self.write(prompt)
       
    46         return self.readline()
       
    47     def flush(self):
       
    48         pass
       
    49 
       
    50 sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
       
    51 # For quicker startup, enter here the address and port to connect to.
       
    52 target='' #('00:20:e0:76:c3:52',1)
       
    53 if not target:
       
    54     address,services=socket.bt_discover()
       
    55     print "Discovered: %s, %s"%(address,services)
       
    56     if len(services)>1:
       
    57         import appuifw
       
    58         choices=services.keys()
       
    59         choices.sort()
       
    60         choice=appuifw.popup_menu(
       
    61             [unicode(services[x])+": "+x for x in choices],u'Choose port:')
       
    62         target=(address,services[choices[choice]])
       
    63     else:
       
    64         target=(address,services.values()[0])        
       
    65 print "Connecting to "+str(target)
       
    66 sock.connect(target)
       
    67 socketio=socket_stdio(sock)
       
    68 realio=(sys.stdout,sys.stdin,sys.stderr)
       
    69 (sys.stdout,sys.stdin,sys.stderr)=(socketio,socketio,socketio)
       
    70 import code
       
    71 try:
       
    72   code.interact()
       
    73 finally:
       
    74   (sys.stdout,sys.stdin,sys.stderr)=realio
       
    75   sock.close()