Python parsing Bluetooth RFCOMM for 9 bytes

Barry Dick barryd.it at gmail.com
Fri Dec 14 06:25:53 CET 2012


> Is the following more like how you want it?
> 
> 
> 
> data = ""
> 
> try:
> 
>      while True:
> 
>          try:
> 
>              more = client_socket.recv(9)
> 
>          except bluetooth.BluetoothError, b:
> 
>              print "Bluetooth Error: ", b
> 
>          else:
> 
>              data += more
> 
> 
> 
>              while len(data) >= 9:
> 
>                  print "Data:  [%s]" % binascii.hexlify(data[ : 9])
> 
>                  data = data[9 : ]
> 
> except KeyboardInterrupt:
> 
>      #print "Closing socket...",
> 
>      client_socket.close()
> 
>      #print "done."
> 
> 
> 
> You could, of course, decide to recv more than 9 bytes at a time. It
> 
> could return less than you asked for (but it should block until at
> 
> least 1 byte is available), but it will never return more than you
> 
> asked for.

Thank you for your interest and quick reply. 

Its a great start, seeing as I'm a beginner with python, I was actually hoping to see an example of http://code.activestate.com/recipes/408859/ as it appears to be exactly what I need, but I haven't got a clue how to implement it. Basically as each byte perhaps gets read or 9 bytes at a time, that way I can seek out as much real data as possible

Data:  [0000002c025b025321]
Data:  [00010048000602505e]
Data:  [0002004a000a025552]
Data:  [0003004b0006025752]
Data:  [00046698000702569e]

(notice the header data[9:1] will always flow in this pattern 0 -> 4 and then back again, so I figure that might as well be my start)


And skip the errors...

Data:  [0002004b000a026343]
Data:  [00]
Data:  [03004f0006025b4a00]
Data:  [046698000802569d00]
Data:  [00003002830257f100]



More information about the Python-list mailing list