def __init__(self, bt_address=None): print "in __init__" try: if (bt_address == None): self.address, self.services = socket.bt_discover() else: self.address, self.services = socket.bt_discover(bt_address) print "Discovery successful" print self.address print self.services except: import traceback traceback.print_exc() appuifw.query( unicode( "Error in bluetooth connection, is the service running"), 'query') appuifw.app.body.text((40, 90), unicode("Please restart!")) lock = e32.Ao_lock() lock.wait() self.data = "" self.sock = 0 self.lock = e32.Ao_lock() self.audio = 0 self.content_handler = appuifw.Content_handler() pykeygrab.init() if len(self.services) > 1: choices = self.services.keys() choices.sort() choice = appuifw.popup_menu( [unicode(self.services[x]) + ": " + x for x in choices], u'Choose port:') self.target = (self.address, self.services[choices[choice]]) else: self.target = (self.address, self.services.values()[0]) self.key_capturer = keycapture.KeyCapturer(self.key_callback) keys = keycapture.all_keys try: keys.remove( keycapture.EKeySelect ) # I haven't figured out how to simulate the select key in the contacts app keys.remove(keycapture.EKeySelect ) # for some reason, EKeySelect is in the list twice # It turns out that this is a bug in the Symbian key_simulation API. I connected a bluetooth keyboard and it exhibited the same problems except: import traceback traceback.print_exc() traceback.print_stack() self.key_capturer.keys = keys
def bt_connect(): global sock # create a bluetooth socket sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM) target=''# here you can give the bt address of the other mobile if you know it if not target: # scan for bluetooth devices address,services=socket.bt_discover() print "Discovered: %s, %s"%(address,services) if len(services)>1: choices=services.keys() choices.sort() # bring up a popup menu and show the available bt devices for selection choice=appuifw.popup_menu([unicode(services[x])+": "+x for x in choices],u'Choose port:') target=(address,services[choices[choice]]) else: target=(address,services.values()[0]) print "Connecting to "+str(target) # connect to the serial port of the PC sock.connect(target) print "OK." # call the text input field function bt_typetext()
def bt_connect(): global sock # create a bluetooth socket sock = socket.socket(socket.AF_BT, socket.SOCK_STREAM) target = '' # here you can give the bt address of the other mobile if you know it if not target: # scan for bluetooth devices address, services = socket.bt_discover() print "Discovered: %s, %s" % (address, services) if len(services) > 1: choices = services.keys() choices.sort() # bring up a popup menu and show the available bt devices for selection choice = appuifw.popup_menu( [unicode(services[x]) + ": " + x for x in choices], u'Choose port:') target = (address, services[choices[choice]]) else: target = (address, services.values()[0]) print "Connecting to " + str(target) # connect to the serial port of the PC sock.connect(target) print "OK." # call the text input field function bt_typetext()
def connect(self): self.sock = socket.socket(socket.AF_BT, socket.SOCK_STREAM) address, services = socket.bt_discover() print "Discovered: %s, %s" % (address, services) target = (address, services.values()[0]) print "Connecting to " + str(target) self.sock.connect(target)
def run(self): # you must create the socket in the same thread you read from it # see the following thread: http://discussion.forum.nokia.com/forum/showthread.php?t=79738 self.running = True if not self.sock: address, services = socket.bt_discover(self.address) self.sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM) print "about to connect to socket" self.sock.connect(self.target) else: print "socket already initialized", self.sock while self.running == True: # await a read event rlist, wlist, elist = select.select( [self.sock], [], [], 5) # test for timeout if [rlist, wlist, elist] == [[], [], []]: print "Five seconds elapsed.\n" else: for sock in rlist: data = sock.recv( 100 ) self.parse_data(data, sock) #self.sock.shutdown(2) self.sock.close() self.sock = 0
def connect(self): self.sock = socket.socket(socket.AF_BT,socket.SOCK_STREAM) address,services = socket.bt_discover() print "Discovered: %s, %s"%(address, services) target = (address, services.values()[0]) print "Connecting to " + str(target) self.sock.connect(target)
def run(self): # you must create the socket in the same thread you read from it # see the following thread: http://discussion.forum.nokia.com/forum/showthread.php?t=79738 self.running = True if not self.sock: address, services = socket.bt_discover(self.address) self.sock = socket.socket(socket.AF_BT, socket.SOCK_STREAM) print "about to connect to socket" self.sock.connect(self.target) else: print "socket already initialized", self.sock while self.running == True: # await a read event rlist, wlist, elist = select.select([self.sock], [], [], 5) # test for timeout if [rlist, wlist, elist] == [[], [], []]: print "Five seconds elapsed.\n" else: for sock in rlist: data = sock.recv(100) self.parse_data(data, sock) #self.sock.shutdown(2) self.sock.close() self.sock = 0
def chat_client(): conn = socket.socket(socket.AF_BT, socket.SOCK_STREAM) address, services = socket.bt_discover() if 'btchat' in services: channel = services[u'btchat'] conn.connect((address, channel)) print "Connected to server!" talk(None, conn) else: appuifw.note(u"Target is not running a btchat server", "error")
def connect(self): self.sock = socket.socket(socket.AF_BT, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) address, services = socket.bt_discover() global connectionDescription connectionDescription = u"Discovered: %s, %s" % (address, services) + u'\n\r' log_panel.set(connectionDescription) target = (address, services.values()[0]) connectionDescription += u"Connecting to " + str(target)+ u'\n\r' log_panel.set(connectionDescription) self.sock.connect(target)
def runBT(self): """Version of the run method that does not use the lightblue library; does not work because "bt_discover" opens up a window, but doesn't return a list.""" allDevices = socket.bt_discover() phones = [] for device in allDevices: foo, isPhone, bar = self.splitclass(device[2]) if isPhone == 2: phones.append(device) for phone in phones: log.write("Looking at phone %s" % str(phone)) services = socket.bt_discover(phone[0]) log.write(str(services)) for service in services: if service[2] is not None and 'FluidNexus' in service[2]: port = service[1] break else: port = None log.write(str(port)) log.write("at end of service search") if port is not None: log.write("sending data!") clientSocket = socket.socket(socket.AF_BT, socket.SOCK_STREAM) clientSocket.connect((phone[0], port)) clientSocket.send('01') time.sleep(1) clientSocket.send('010') time.sleep(1) clientSocket.send('000010') time.sleep(1) clientSocket.send(str(time.time())) time.sleep(1) clientSocket.send(md5.md5('foo').hexdigest()) time.sleep(1) clientSocket.send('aaaaaaaaaa') time.sleep(1) clientSocket.send('aaaaaaaaaa') time.sleep(1) clientSocket.close()
def __init__(self, bt_address = None): print "in __init__" try: if(bt_address == None): self.address,self.services=socket.bt_discover() else: self.address,self.services=socket.bt_discover(bt_address) print "Discovery successful" print self.address print self.services except: import traceback traceback.print_exc() appuifw.query(unicode("Error in bluetooth connection, is the service running"), 'query') appuifw.app.body.text((40, 90), unicode("Please restart!")) lock = e32.Ao_lock() lock.wait() self.data = "" self.sock=0 self.lock = e32.Ao_lock() self.audio=0 self.content_handler = appuifw.Content_handler() pykeygrab.init() if len(self.services)>1: choices=self.services.keys() choices.sort() choice=appuifw.popup_menu([unicode(self.services[x])+": "+x for x in choices],u'Choose port:') self.target=(self.address,self.services[choices[choice]]) else: self.target=(self.address,self.services.values()[0]) self.key_capturer = keycapture.KeyCapturer(self.key_callback) keys = keycapture.all_keys try: keys.remove(keycapture.EKeySelect) # I haven't figured out how to simulate the select key in the contacts app keys.remove(keycapture.EKeySelect) # for some reason, EKeySelect is in the list twice # It turns out that this is a bug in the Symbian key_simulation API. I connected a bluetooth keyboard and it exhibited the same problems except: import traceback traceback.print_exc() traceback.print_stack() self.key_capturer.keys = keys
def connect(address=None): """Form an RFCOMM socket connection to the given address. If address is not given or None, query the user where to connect. The user is given an option to save the discovered host address and port to a configuration file so that connection can be done without discovery in the future. Return value: opened Bluetooth socket or None if the user cancels the connection. """ # Bluetooth connection sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM) if not address: import appuifw CONFIG_FILE=os.path.join(CONFIG_DIR,'syncandrun.txt') try: f=open(CONFIG_FILE,'rt') config=eval(f.read()) f.close() except: config={} address=config.get('default_target','') if address: choice=appuifw.popup_menu([u'Default host', u'Other...'],u'Connect to:') if choice==1: address=None if choice==None: return None # popup menu was cancelled. if not address: print "Discovering..." addr,services=socket.bt_discover() print "Discovered: %s, %s"%(addr,services) port = 5 address=(addr,port) choice=appuifw.query(u'Set as default?','query') if choice: config['default_target']=address # make sure the configuration file exists. if not os.path.isdir(CONFIG_DIR): os.makedirs(CONFIG_DIR) f=open(CONFIG_FILE,'wt') f.write(repr(config)) f.close() print "Connecting to "+str(address)+"...", sock.connect(address) print "OK." return sock
def connect(address=None): """Form an RFCOMM socket connection to the given address. If address is not given or None, query the user where to connect. The user is given an option to save the discovered host address and port to a configuration file so that connection can be done without discovery in the future. Return value: opened Bluetooth socket or None if the user cancels the connection. """ # Bluetooth connection sock = socket.socket(socket.AF_BT, socket.SOCK_STREAM) if not address: import appuifw CONFIG_FILE = os.path.join(CONFIG_DIR, 'syncandrun.txt') try: f = open(CONFIG_FILE, 'rt') config = eval(f.read()) f.close() except: config = {} address = config.get('default_target', '') if address: choice = appuifw.popup_menu([u'Default host', u'Other...'], u'Connect to:') if choice == 1: address = None if choice == None: return None # popup menu was cancelled. if not address: print "Discovering..." addr, services = socket.bt_discover() print "Discovered: %s, %s" % (addr, services) port = 5 address = (addr, port) choice = appuifw.query(u'Set as default?', 'query') if choice: config['default_target'] = address # make sure the configuration file exists. if not os.path.isdir(CONFIG_DIR): os.makedirs(CONFIG_DIR) f = open(CONFIG_FILE, 'wt') f.write(repr(config)) f.close() print "Connecting to " + str(address) + "...", sock.connect(address) print "OK." return sock
def discovered(error, devices, cb_param): if error == 0: tell("devices: " + str(devices)) try: for address, name in devices: services = bt_discover(address) tell("services: " + str(services)) except: tell("service discovery failure") log_exception() else: tell("device discovery failure: error %d" % error) _discoverer.close()
def select_btdevice(): appuifw.app.title = u"Select Device" target='' #('00:20:e0:76:c3:52',1) if not target: address,services=socket.bt_discover() print "Discovered: %s, %s"%(address,services) if len(services)>1: choices=services.keys() choices.sort() choice=appuifw.popup_menu( [unicode(services[x])+": "+x for x in choices],u'Choose port:') target=(address,services[choices[choice]]) else: target=(address,services.values()[0]) return target
def chat_client(): conn = socket.socket(socket.AF_BT, socket.SOCK_STREAM) address, services = socket.bt_discover() print services channel = services["COM11"] conn.connect((address, channel)) print "Connected to server!" conn.send("hello") while 1: img=graphics.screenshot() img.save("d:\\t.png") t=open("d:\\t.png","rb").read() out=zlib.compress(t,zlib.Z_BEST_COMPRESSION) conn.send("010203".decode("hex")+out+"040506".decode("hex")) time.sleep(0)
def bt_connect(): global sock sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM) target='' if not target: address,services=socket.bt_discover() if len(services)>1: import appuifw choices=services.keys() choices.sort() choice=appuifw.popup_menu([unicode(services[x])+": "+x for x in choices],u'Choose port:') target=(address,services[choices[choice]]) else: target=(address,services.values()[0]) sock.connect(target)
def select_btdevice(): appuifw.app.title = u"Select Device" target = '' #('00:20:e0:76:c3:52',1) if not target: address, services = socket.bt_discover() print "Discovered: %s, %s" % (address, services) if len(services) > 1: choices = services.keys() choices.sort() choice = appuifw.popup_menu( [unicode(services[x]) + ": " + x for x in choices], u'Choose port:') target = (address, services[choices[choice]]) else: target = (address, services.values()[0]) return target
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 conecta(self): 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." return self.transformaData(self.sock.recv(1024))
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 bt_connect(): global sock sock = socket.socket(socket.AF_BT, socket.SOCK_STREAM) target = '' if not target: address, services = socket.bt_discover() print "Discovered: %s, %s" % (address, services) if len(services) > 1: choices = services.keys() choices.sort() choice = appuifw.popup_menu( [unicode(services[x]) + ": " + x for x in choices], u'Choose port:') target = (address, services[choices[choice]]) else: target = (address, services.values()[0]) print "Connecting to " + str(target) sock.connect(target) print "OK."
def discover_address(configfile): import appuifw CONFIG_FILE = os.path.join(CONFIG_DIR, configfile) try: f = open(CONFIG_FILE, 'r') config = eval(f.read()) f.close() except: config = {} address = config.get('target', '') if address: choice = appuifw.popup_menu([u'Default host', u'Other...'], u'Connect to:') if choice == 1: address = None if choice == None: return None # popup menu was cancelled. if not address: print "Discovering..." addr, services = socket.bt_discover() print "Discovered: %s, %s" % (addr, services) if len(services) > 1: 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) config['target'] = address # make sure the configuration file exists. if not os.path.isdir(CONFIG_DIR): os.makedirs(CONFIG_DIR) f = open(CONFIG_FILE, 'wt') f.write(repr(config)) f.close() return address
def connect_to_PC(): global sock sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM) target='' if not target: address,services=socket.bt_discover() print "Discovered: %s, %s"%(address,services) if len(services)>1: # import appuifw bt_choices=services.keys() bt_choices.sort() bt_choice=appuifw.popup_menu([unicode(services[x])+": "+x for x in bt_choices],u'Choose port:') pc_target=(address,services[bt_choices[bt_choice]]) else: pc_target=(address,services.values()[0]) print "Connecting to "+str(pc_target) sock.connect(pc_target) print "Connected." bt_msgtransfer()
def sendCoinsBT(self): self.sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM) try: addr,services=socket.bt_discover() except Exception: appuifw.note(u'Bluetooth failed') return if len(services)>0: 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) self.sock.connect(address) appuifw.note(u'Bluetooth connected') self.sock.send('A coin \n') appuifw.note(u'Coin sent')
def discover_address(configfile): import appuifw CONFIG_FILE=os.path.join(CONFIG_DIR,configfile) try: f=open(CONFIG_FILE,'r') config=eval(f.read()) f.close() except: config={} address=config.get('target','') if address: choice=appuifw.popup_menu([u'Default host', u'Other...'],u'Connect to:') if choice==1: address=None if choice==None: return None # popup menu was cancelled. if not address: print "Discovering..." addr,services=socket.bt_discover() print "Discovered: %s, %s"%(addr,services) if len(services)>1: 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) config['target']=address # make sure the configuration file exists. if not os.path.isdir(CONFIG_DIR): os.makedirs(CONFIG_DIR) f=open(CONFIG_FILE,'wt') f.write(repr(config)) f.close() return address
def connectGPS(): global sock, target # create a bluetooth socket and connect to GPS receiver: (if it is a Nokia LD-3W the PIN is: 0000) try: sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM) if target == '': address,services = socket.bt_discover() print "Discovered: %s, %s"%(address, services) target = (address, services.values()[0]) print "Connecting to " + str(target) sock.connect(target) appuifw.note(u"GPS successfully connected!", "info") print "connected!" connectedMenu() # Reading is done separately using a loop. #readData() except: if appuifw.query(u"GPS device problem. Try again?","query") == True: sock.close() connectGPS() else: sock.close()
def sendCoinsBT(self): self.sock = socket.socket(socket.AF_BT, socket.SOCK_STREAM) try: addr, services = socket.bt_discover() except Exception: appuifw.note(u'Bluetooth failed') return if len(services) > 0: 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) self.sock.connect(address) appuifw.note(u'Bluetooth connected') self.sock.send('A coin \n') appuifw.note(u'Coin sent')
def discover_address(config_file, interactive=True): """ discover_address( config_file, interactive = True ) called on the phone side config_file stores the address and port of previous connection if interactive == False that address is used, otherwise the user is prompted to select device and service """ import appuifw CONFIG_DIR = "c:/system/apps/python" CONFIG_FILE = os.path.join(CONFIG_DIR, config_file) try: config = eval(open(CONFIG_FILE, "r").read()) except: config = {} address = config.get("target", "") if address and not interactive: return address if address: choice = appuifw.popup_menu([u"Default host", u"Other..."], u"Connect to:") if choice == 0: return address if choice == None: return None # popup menu was cancelled # address not stored, or want a new host print "Discovering..." addr, services = socket.bt_discover() print "Discovered: %s, %s" % (addr, services) if len(services) > 1: choices = services.keys() choices.sort() def dropendzero(x): # this is to overcome a bug of choice strings # having a terminating zero... if ord(x[-1]) == 0: return unicode(x[:-1]) else: return unicode(x) # l = [(unicode(services[x]), dropendzero(x)) for x in choices] # choice = appuifw.popup_menu( l, u'Choose port (scroll):' ) l = [dropendzero(x) for x in choices] choice = appuifw.popup_menu(l, u"Choose port:") if choice == None: print "no choice" return None port = services[choices[choice]] else: port = services.values()[0] address = (addr, port) config["target"] = address # make sure the configuration file exists if not os.path.isdir(CONFIG_DIR): os.makedirs(CONFIG_DIR) # store the configuration file open(CONFIG_FILE, "wt").write(repr(config)) return address
import socket print("\n\nperforming inquiry...") address, services = socket.bt_discover() print("Address: %s" % address) for name, port in services.items(): print( u"%s : %d" % (name, port) )
break return ''.join(buffer) def raw_input(self, prompt=""): self.write(prompt) return self.readline() def flush(self): pass sock = socket.socket(socket.AF_BT, socket.SOCK_STREAM) # For quicker startup, enter here the address and port to connect to. target = '' #('00:20:e0:76:c3:52',1) if not target: address, services = socket.bt_discover() print "Discovered: %s, %s" % (address, services) if len(services) > 1: import appuifw choices = services.keys() choices.sort() choice = appuifw.popup_menu( [unicode(services[x]) + ": " + x for x in choices], u'Choose port:') target = (address, services[choices[choice]]) else: target = (address, services.values()[0]) print "Connecting to " + str(target) sock.connect(target) socketio = socket_stdio(sock) realio = (sys.stdout, sys.stdin, sys.stderr)
def connect(address=None): """Form an RFCOMM socket connection to the given address. If address is not given or None, query the user where to connect. The user is given an option to save the discovered host address and port to a configuration file so that connection can be done without discovery in the future. Return value: opened Bluetooth socket or None if the user cancels the connection. """ # Bluetooth connection sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM) if not address: import appuifw CONFIG_DIR='c:/system/apps/python' CONFIG_FILE=os.path.join(CONFIG_DIR,'btconsole_conf.txt') try: f=open(CONFIG_FILE,'rt') try: config=eval(f.read()) finally: f.close() except: config={} address=config.get('default_target','') if address: choice=appuifw.popup_menu([u'Default host', u'Other...'],u'Connect to:') if choice==1: address=None if choice==None: return None # popup menu was cancelled. if not address: print "Discovering..." try: addr,services=socket.bt_discover() except socket.error, err: if err[0]==2: # "no such file or directory" appuifw.note(u'No serial ports found.','error') elif err[0]==4: # "interrupted system call" print "Cancelled by user." elif err[0]==13: # "permission denied" print "Discovery failed: permission denied." else: raise return None print "Discovered: %s, %s"%(addr,services) if len(services)>1: 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) choice=appuifw.query(u'Set as default?','query') if choice: config['default_target']=address # make sure the configuration file exists. if not os.path.isdir(CONFIG_DIR): os.makedirs(CONFIG_DIR) f=open(CONFIG_FILE,'wt') f.write(repr(config)) f.close()
def connect(): global sock address, services = socket.bt_discover() channel = choose_service(services) sock = socket.socket(socket.AF_BT, socket.SOCK_STREAM) sock.connect((address, channel))