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

 
Developer Updates  
Free Website Content 
eWeek
 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: 4 stars4 stars4 stars4 stars4 stars / 8
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Virtual Tradeshows by Ziff Davis Enterprise - A Unique Opportunity to Connect with IT Experts, Access Information, and Gain Insight on today's Technology

    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....


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this discussion I have tried to introduce the basics of Bluetooth programming...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway