×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    need help in connect bluetooth module

    I am doing a project that use bluetooth on mobile phone to connect to bluetooth module attached on development board. Code below able to discover bluetooth devices but not able to connect to the bluetooth module. Anyone can help me to find out what is going wrong in my code? thank you very much


    Code:
    import appuifw
    # import the module socket
    import socket
    import e32
    
    # function that handles the bluetooth connection:
    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."
        return sock
    
    def recieve1():
        global sock
        data=sock.recv(1)
        if data=="1":
            appuifw.note(u"LED on","info")
        elif data=="0":
    	appuifw.note(u"LED off","info")
    
    def recieve2():
        global sock
        data=sock.recv(2)
        if data=="1":
            appuifw.note(u"Light on","info")
        elif data=="0":
    	appuifw.note(u"Light off","info")
    
    
    def bt_send_data1():
        global sock
        sock.send("1")
        receive1()
    
    def bt_send_data2():
        global sock
        sock.send("0")
        receive1()
    
    def bt_send_data3():
        global sock
        sock.send("1")
        receive2()
    
    def bt_send_data4():
        global sock
        sock.send("0")
        receive2()
    
    
    #def quit():
    #    print"socket closed"
    #    sock.close()
    #    app_lock.signal()
    
    #app_lock=e32.Ao_lock()
    
    appuifw.app.menu=[(u"LED on",bt_send_data1),
    		  (u"LED off",bt_send_data2),
                      (u"Light on",bt_send_data3),
    		  (u"Light off",bt_send_data4)]
    		  
    #app_lock.wait()
    
    
    def exit_key_handler():
        script_lock.signal()
        appuifw.app.set_exit()
    
    appuifw.app.title = u"Smart home"
    
    script_lock = e32.Ao_lock()
    
    appuifw.app.exit_key_handler = exit_key_handler()
    
    # call the function that handles the bluetooth connection
    bt_connect()
    
    script_lock.wait()

  2. #2
    Regular Contributor
    Join Date
    Nov 2008
    Posts
    73

    Re: need help in connect bluetooth module

    Quote Originally Posted by snowfish View Post
    I am doing a project that use bluetooth on mobile phone to connect to bluetooth module attached on development board. Code below able to discover bluetooth devices but not able to connect to the bluetooth module. Anyone can help me to find out what is going wrong in my code? thank you very much


    Code:
    import appuifw
    # import the module socket
    import socket
    import e32
    
    # function that handles the bluetooth connection:
    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."
        return sock
    
    def recieve1():
        global sock
        data=sock.recv(1)
        if data=="1":
            appuifw.note(u"LED on","info")
        elif data=="0":
    	appuifw.note(u"LED off","info")
    
    def recieve2():
        global sock
        data=sock.recv(2)
        if data=="1":
            appuifw.note(u"Light on","info")
        elif data=="0":
    	appuifw.note(u"Light off","info")
    
    
    def bt_send_data1():
        global sock
        sock.send("1")
        receive1()
    
    def bt_send_data2():
        global sock
        sock.send("0")
        receive1()
    
    def bt_send_data3():
        global sock
        sock.send("1")
        receive2()
    
    def bt_send_data4():
        global sock
        sock.send("0")
        receive2()
    
    
    #def quit():
    #    print"socket closed"
    #    sock.close()
    #    app_lock.signal()
    
    #app_lock=e32.Ao_lock()
    
    appuifw.app.menu=[(u"LED on",bt_send_data1),
    		  (u"LED off",bt_send_data2),
                      (u"Light on",bt_send_data3),
    		  (u"Light off",bt_send_data4)]
    		  
    #app_lock.wait()
    
    
    def exit_key_handler():
        script_lock.signal()
        appuifw.app.set_exit()
    
    appuifw.app.title = u"Smart home"
    
    script_lock = e32.Ao_lock()
    
    appuifw.app.exit_key_handler = exit_key_handler()
    
    # call the function that handles the bluetooth connection
    bt_connect()
    
    script_lock.wait()
    --------------------------------------------
    I also care about it! I have seen this:

    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()

Similar Threads

  1. Using Python and Bluetooth to connect with a Nonin Medical Sensor
    By icinnamon in forum Bluetooth Technology
    Replies: 0
    Last Post: 2007-07-11, 00:33
  2. Connect to internet through bluetooth
    By g00011431 in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2007-03-17, 09:50
  3. Bluetooth connect without userconfirmation.
    By AdrianDK in forum Bluetooth Technology
    Replies: 0
    Last Post: 2006-10-30, 10:24
  4. Connect to Bluetooth GPS and headset?
    By trafcam in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2005-11-24, 09:11
  5. How to detect the bluetooth connect?
    By baker75 in forum Bluetooth Technology
    Replies: 2
    Last Post: 2005-11-02, 01:12

Posting Permissions

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