Sample Bluetooth RFCOMM Client App in Python

I recently wrote a bluetooth rfcomm client app in C. However before I started writing the code in C, I prototyped it using python. The python code obviously is 50 times shorter than the C code and was very easy to write. Surprisingly it also feels snappier than its C counterpart. May be I screwed up the C code somewhere.

Functionality:

  1. Scan for all discoverable bluetooth devices
  2. Connect to the SDP servers of each one of these and search for a particular service identified by a unique UUID
  3. The first possible match is blasted with dummy data
  4. If the bluetooth device stops advertising the server, the app will wait till the bluetooth device’s sdp server is unreachable
  5. Go to the next device in list if any, or go back to step 1
  6. Ctrl – C to exit
#!/usr/bin/python

import bluetooth
import threading
import time
import random
import sys

#address="00:17:E8:B2:50:28"
#uuid="66841278-c3d1-11df-ab31-001de000a901" 
#service_matches = bluetooth.find_service( uuid = uuid, address = address )

uuid="66841278-c3d1-11df-ab31-001de000a901" 
while True:
  try:
    print "Searching ..."
    try: service_matches
    except NameError:
      service_matches = bluetooth.find_service( uuid = uuid)
    else:
      if not service_matches:
        print ("without address")
        service_matches = bluetooth.find_service( uuid = uuid)
      else:
        print ("with address")
        service_matches_with_addr = bluetooth.find_service( uuid = uuid, address = host )
        if service_matches_with_addr:
          service_matches = service_matches_with_addr
        else:
          continue

    if service_matches:
      first_match = service_matches[0]
      port = first_match["port"]
      name = first_match["name"]
      host = first_match["host"]
      print "connecting to \"%s\" on %s" % (name, host)
      sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
      sock.connect((host, port))

      print "Happy Spamming"

      while True:
        try:
          string = 'test' + random.choice('abcdefghij') + '\n'
          try:
            sock.send(string)
            time.sleep(0.5)
          except:
            print "Android no longer interested in my spam, socket not valid, going back to searching"
            break
        except KeyboardInterrupt:
          print "Done with Spamming, Press Ctrl-C again if you wish to quit, otherwise I'll keep searching"
          break

  except KeyboardInterrupt:
    print "Phew! Done Searching"
    sys.exit()

if service_matches:
  first_match = service_matches[0]
  port = first_match["port"]
  name = first_match["name"]
  host = first_match["host"]

  sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
  sock.connect((host, port))

  print "Happy Spamming"

  while True:
    try:
      string = 'test' + random.choice('abcdefghij') + '\n'
      sock.send(string)
      time.sleep(0.5)
    except KeyboardInterrupt:
      print "Done with Spamming"
      sys.exit()


sock.close()

About Pratik Sinha

Linux Nerd, Socialist, Atheist, Adventuristic, Nature Lover, Geeky.

1 comment

Leave a Reply