Home arrow Python arrow Page 4 - Bluetooth Programming using Python

Bluetooth Programming in the Real World - Python

Bluetooth is a way to connect devices wirelessly. This technology has a variety of uses. Python is an easy to learn scripting language that has been growing in popularity. The two can work well together, as will be explained in this article.

TABLE OF CONTENTS:
  1. Bluetooth Programming using Python
  2. Bluetooth and Python: the Whys and Wherefores
  3. Bluetooth Programming, Step by Step
  4. Bluetooth Programming in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 16
September 10, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The first rule of any program that targets the real world is to provide modularity. In our case, we can provide modularity by wrapping the device discoverycode in a class. The name of the class will be Devices. Lets start with the imports:

from bluetooth import *

Next comes the class with the constructor. The constructor takes the name of the device whose address has to be found.

from bluetooth import *

class Devices:
   def __init__( self, target_device_name):
      self.target_device=target_device_name
      self.target_device_address= None

Next let us define the method that will perform the method lookup. If the target device is found, it will set the address to the variable self.target_device_address. To find the address, it iterates over the list of addresses returned by the discover_devices() method and passes each address to the lookup_name method.

from bluetooth import *

class Devices:
   def __init__( self, target_device_name):
      self.target_device=target_device_name
      self.target_device_address= None

   def check_devices(self):
      discovered_devices=discover_devices()
     
for address in discovered_devices:
  
      if self.target_device==lookup_name(address):
  
      self.target_device_address=address
        break

Next comes the method that checks whether the address of the target device is found or not. If found it will return the address; otherwise it will return None.

from bluetooth import *

class Devices:
   def __init__( self, target_device_name):
      self.target_device=target_device_name
      self.target_device_address= None

   def check_devices(self):
      discovered_devices=discover_devices()
     
for address in discovered_devices:
 
      if self.target_device==lookup_name(address):
  
      self.target_device_address=address
 
      break

   def device_found(self):
      self.check_devices()

       if self.target_device_address is not None:
        return self.target_device_address
     
else:
 
      return None

That completes our class. Now let us test it by calling it from another module. The module will first ask the user to enter the name of the device to be discovered. Then it will create an object of the Device class and call the check_device method. The returned result will be displayed to the user. Here is the code:

user_device= raw_input("Enter the device to be
discovered:")
device = Devices(user_device)
addr = device.device_found()

if addr is not None:
      print "The address for the device is :". Adder
else:
      print "The device could not be discovered"

That completes our discussion on the basics of Bluetooth programming using Python. The next step is to create and discover services. That will be the topic of my next discussion. Till then....



 
 
>>> More Python Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: