Some half-baked code from Larry
From MoDe
Here is some code to connect to and read from a bluetooth gps device. One should first do scan to find the device via name and then get its bluetooth id, but that makes it harder to read.
import socket
class gpsget:
def __init__(self):
self.buffer = ""
self.sock = ""
def find_gps_device(self):
# hard coded bluetooth addresses
target='00:0c:a5:00:b9:b5',1
target = '00:0b:0d:15:5a:7f',1
target = '00:08:1b:8c:8f:06',1
# this should discover and return a list of BT devices with names
return target
def connect_gps(self,target):
try:
self.sock = socket.socket( socket.AF_BT, socket.SOCK_STREAM )
self.sock.connect(target)
return True
except:
return False
def gps_get_fix(self):
(r, buffer) = (False, "") # initialization
c = self.sock.recv(1) # get one byte from gps
for iii in range(1000): # receive up to a '$' or 1000 bytes
if (c != '$'):
c = self.sock.recv(1)
else: break
for iii in range(1000): # receive up to a '\r' or 1000 bytes
if (c != '\r'):
buffer += c
c = self.sock.recv(1)
else: break
self.buffer += buffer
if (buffer[0:6]=="$GPGGA"):
try: (GPGGA,utcTime,lat,ns,lon,ew,posfix,sats,hdop,alt,altunits,sep,sepunits,age,sid)=buffer.split(",")
if int(posfix) == True:
r = (lat, lon )
except:
r = False
return r
if __name__ == '__main__':
g = gpsget()
target = g.find_gps_device()
r = g.connect_gps(target)
if not r:
print 'cannot connect to gps device'
for i in range(10):
fix = g.gps_get_fix()
if fix:
print "latitude: %s, longitude: %s" % fix
else:
print "could not get fix"
print 'done'
