Python
  Home arrow Python arrow Page 4 - Bluetooth Programming using Python
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Bluetooth Programming using Python
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2007-09-10


    Table of Contents:
  • Bluetooth Programming using Python
  • Bluetooth and Python: the Whys and Wherefores
  • Bluetooth Programming, Step by Step
  • Bluetooth Programming in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Bluetooth Programming using Python - Bluetooth Programming in the Real World
    ( Page 4 of 4 )

    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
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT