How to Connect multi-devices at same time ?

Help
xu long
2008-05-27
2013-04-25
  • xu long

    xu long - 2008-05-27

    hi,
    A bluetooth adapter host could connect maximum 7 different devices,e.g.,I use a bluetooth adapter(CSR chip set),send file via Obex service to device A and at the same time receive file from device B.but how to do it with lightblue?

    best regards!

     
    • Cristian Orellana

      Hi, I guess you can do that with threads. One thread for the obexServer that receives files and another for the sending class.

       
    • xu long

      xu long - 2008-05-28

      thank you crorella,I tried,but it seems doesn't work,my code is below:

      [code]
      #!/usr/bin/env   python 
      #   coding:utf8

      import bluetooth
      import lightblue
      import threading
      import time
      import random

      def get_Local_info():
          local_addr=lightblue.gethostaddr()
          local_name=lightblue.finddevicename(local_addr)
          local_class=lightblue.gethostclass()
          print "local device information:"
          print "    addr: "+str(local_addr)
          print "    name: "+str(local_name)
          print "    class: "+str(local_class)+"\n"

      def recvfile(channel_NO):
          s=lightblue.socket()
          print "Register socket"
          local_addr=lightblue.gethostaddr()
          s.bind((local_addr,channel_NO))
          print "Bind socket %d" %channel_NO
          lightblue.advertise("My OBEX Service",s,lightblue.OBEX)
          print "Advertise service"
          file_Name=str(channel_NO)+".txt"
          lightblue.obex.recvfile(s,file_Name)
          print "file %d received"%channel_NO
          s.close()
          print "Scoket close\n"

      class PrintThread(threading.Thread):
          def __init__(self, threadName,channel_NO):
              threading.Thread.__init__(self, name = threadName)
              self.sleepTime = random.randint(1,3)
              self.channel_NO = channel_NO
              print "Name: %s; sleep: %d" % (self.getName(), self.sleepTime)

          def run(self):
              print "%s going to sleep for %s second(s)"\               % (self.getName(), self.sleepTime)
              print "%s say : hello baby,I'm here! " %self.getName()
              recvfile(self.channel_NO)
              time.sleep(self.sleepTime)
              print self.getName(), 'done sleeping'

      num=2
      threadList=[]
      for i in range(1,num+1):
          thread = PrintThread('thread'+str(i),i)
          threadList.append(thread)

      print '\nStarting threads'

      for i in threadList:
          i.start()

      print 'All threads started\n'

      for i in threadList:
          i.join()

      print 'All threads stoped\n'

      [code]

      I run it in ubuntu8.04 desktop,after thread 1 received file from NOKIA5300,thread2 could't receive file anymore! what's the matter?

       
      • Cristian Orellana

        What I'm doing is perform a stopadvertise(s) just after the start of the incoming transfer (for that I took the accept() out of recvfile()), this is because if a second device does a scan it will associate OBEX-PUSH service with the first available channel(that is the one who is already being used), thus, closing the advertise lowers the probability the second devices tries to connect in that channel.

         

Log in to post a comment.