×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    1

    Programming Bluetooth aplication by Python

    Hello to everybody,

    I want to know does it possible to make a bluetooth application
    on a phone which is developped on Python language ?

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7
    Download and review Python for Series 60 Documentation bundle at http://forum.nokia.com/main/0,6566,0...ml&fileID=6534

    At "Programming with Python for Series 60 Platform" there is section dedicated to the topic - Bluetooth Sockets. There is example of services discovery and reference to Python's Bluetooth console and Bluetooth socket stdio.

    May be some other examples would be useful. ;-)

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    Groningen, Netherlands
    Posts
    10

    example BT program

    Hi,

    Here is a sample BT program that reads a position from a BT GPS and displays it.

    import socket

    class BTReader:

    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 readposition(self):
    try:
    while 1:
    buffer=""
    ch=self.sock.recv(1)
    while(ch!='$'):
    ch=self.sock.recv(1)
    while 1:
    if (ch=='\r'):
    break
    buffer+=ch
    ch=self.sock.recv(1)
    if (buffer[0:6]=="$GPGGA"):
    (GPGGA,hhmmssss,l1ddmmmmmm,l1,l2dddmmmmmm,l2,q,xx,pp,ab,M,cd,M,xx,nnnn)=buffer.split(",")
    return (l1+l1ddmmmmmm, l2+l2dddmmmmmm)
    except Error:
    return None

    def close(self):
    self.sock.close()

    bt=BTReader()
    bt.connect()
    print bt.readposition()
    print bt.readposition()
    print bt.readposition()
    bt.close()

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    124
    I am aware that the above is oversimplification, but yet I would like to point out that:

    GPGGA is not enough to read GPS position, with all receivers, since not all receiver use it.

    I think you should be able decode at least :
    GPGGA
    GPRMC
    GPGLL
    messages. Possible others, I have only practical xp on subject.

    One other thing is that __some__ receivers and sending some of the above messages without cordinates, if they do not hear satellites. You should not crash on it.


    Just because your code work with one NMEA receiver does not meat that it works with them all.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Location
    Groningen, Netherlands
    Posts
    10
    You' re right, but I didn't pretend it to be very robust always working code. For example, in the same GPGGA string the validness etc is also stored but not used in the code.

    My BT GPS unit only sends GPGSA, GPGSV, GPGGA and GPRMC strings, which again shows that different unit use different parts of the standard!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    1

    Connecting to Bluez

    I have been trying to make a socket connection over bluetooth to a Bluez enabled GNU/Linux box, without much luck.

    Is there any more information on the Python bluetooth interface, than in the 3 pdf's? I would like more examples :)

  7. #7
    Super Contributor
    Join Date
    Dec 2004
    Posts
    643

    Re: Connecting to Bluez

    Originally posted by sverrejoh
    I have been trying to make a socket connection over bluetooth to a Bluez enabled GNU/Linux box, without much luck.

    Is there any more information on the Python bluetooth interface, than in the 3 pdf's? I would like more examples
    There are instructions in the postneo wiki for using Bluetooth on various platforms. See http://www.postneo.com/postwiki/moin...honForSeries60

  8. #8
    Registered User
    Join Date
    Feb 2005
    Location
    Belgium (Europe)
    Posts
    1,352

    Re: Programming Bluetooth aplication by Python

    Is someone can check out if indentation is OK ???

    Code:
    import socket
    
    class BTReader:
    
    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 readposition(self):
      try:
      while 1:
        buffer=""
        ch=self.sock.recv(1)
        while(ch!='$'):
          ch=self.sock.recv(1)
          while 1:
            if (ch=='\r'):
              break
            buffer+=ch
            ch=self.sock.recv(1)
            if (buffer[0:6]=="$GPGGA"):
              (GPGGA,hhmmssss,l1ddmmmmmm,l1,l2dddmmmmmm,l2,q,xx,pp,ab,M,cd,M,xx,nnnn)=buffer.split(",")
              return (l1+l1ddmmmmmm, l2+l2dddmmmmmm)
      except Error:
              return None
    
    def close(self):
      self.sock.close()
    
    bt=BTReader()
    bt.connect()
    print bt.readposition()
    print bt.readposition()
    print bt.readposition()
    bt.close()
    Thanks
    Cyke64
    pys60 1.4.5 and 2.0.0, pygame, PyS60 CE on E90 and 5800 !

    Find my pys60 extension modules on cyke64.googlepages.com

  9. #9
    Regular Contributor
    Join Date
    Nov 2006
    Location
    Pune, india
    Posts
    186

    Re: Programming Bluetooth aplication by Python

    Quote Originally Posted by cyke64
    Is someone can check out if indentation is OK ???

    Code:
    import socket
    
    class BTReader:
    
    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 readposition(self):
      try:
      while 1:
        buffer=""
        ch=self.sock.recv(1)
        while(ch!='$'):
          ch=self.sock.recv(1)
          while 1:
            if (ch=='\r'):
              break
            buffer+=ch
            ch=self.sock.recv(1)
            if (buffer[0:6]=="$GPGGA"):
              (GPGGA,hhmmssss,l1ddmmmmmm,l1,l2dddmmmmmm,l2,q,xx,pp,ab,M,cd,M,xx,nnnn)=buffer.split(",")
              return (l1+l1ddmmmmmm, l2+l2dddmmmmmm)
      except Error:
              return None
    
    def close(self):
      self.sock.close()
    
    bt=BTReader()
    bt.connect()
    print bt.readposition()
    print bt.readposition()
    print bt.readposition()
    bt.close()
    Thanks
    Cyke64

    yes there is an error in indentation after try.
    you should give a space before while

  10. #10
    Registered User
    Join Date
    Feb 2005
    Location
    Belgium (Europe)
    Posts
    1,352

    Re: Programming Bluetooth aplication by Python

    Thanks Sagars

    Cyke64
    pys60 1.4.5 and 2.0.0, pygame, PyS60 CE on E90 and 5800 !

    Find my pys60 extension modules on cyke64.googlepages.com

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    13

    Re: Programming Bluetooth aplication by Python

    can this be done more simply with pybluez??

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Re: Programming Bluetooth aplication by Python

    Quote Originally Posted by cyke64 View Post
    Is someone can check out if indentation is OK ???

    Code:
    import socket
    
    class BTReader:
    
    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 readposition(self):
      try:
      while 1:
        buffer=""
        ch=self.sock.recv(1)
        while(ch!='$'):
          ch=self.sock.recv(1)
          while 1:
            if (ch=='\r'):
              break
            buffer+=ch
            ch=self.sock.recv(1)
            if (buffer[0:6]=="$GPGGA"):
              (GPGGA,hhmmssss,l1ddmmmmmm,l1,l2dddmmmmmm,l2,q,xx,pp,ab,M,cd,M,xx,nnnn)=buffer.split(",")
              return (l1+l1ddmmmmmm, l2+l2dddmmmmmm)
      except Error:
              return None
    
    def close(self):
      self.sock.close()
    
    bt=BTReader()
    bt.connect()
    print bt.readposition()
    print bt.readposition()
    print bt.readposition()
    bt.close()
    Thanks
    Cyke64

    Indentation is still not correct but the example is overly complicated anyway. There is no need to read char by char and parse line ends, you can easily use socket.makefile() and use readline()

Posting Permissions

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