Python
  Home arrow Python arrow Page 5 - Bluetooth Programming in Python: Netwo...
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 
 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 in Python: Network Programming using RFCOMM
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-12-12

    Table of Contents:
  • Bluetooth Programming in Python: Network Programming using RFCOMM
  • Developing Applications for RFCOMM, Step by Step
  • Listening for requests
  • Developing the Client
  • RFCOMM 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 Interact with IT Experts, Access Information, and Gain Insight on Today’s Trends in Technology Learn more

    Bluetooth Programming in Python: Network Programming using RFCOMM - RFCOMM in the Real World
    (Page 5 of 5 )

    The server will service only one client at a time. It is neither multithreaded nor multi-process based. Let us start. First comes the imports.

    from bluetooth import *

    Then comes the class that will contain the server functionalities. Its constructor will take the port number on which the server has to listen. It will also call the create_server function.

    from bluetooth import *


    class rfcomm_server:

    def __init__(self,port):

    self.port=port

    self.create_server()

    Next is the create_server method that will create a RFCOMM-based socket and make it listen on the passed port. It then calls the start_server method.

    from bluetooth import *


    class rfcomm_server:

    def __init__(self,port):

    self.port=port

    self.create_server()


    def create_server(self):

    self.server_socket=

    Bluetooth.BluetoothSocket(Bluetooth.RFCOMM)

     

    self.server_socket.bind(("",self.port))

    self.start_server()

    Next is the start_server method. This method makes the server listen on the port and then starts the connection. After that it asks the user for the file and and then transfers it.

    from bluetooth import *


    class rfcomm_server:

    def __init__(self,port):

    self.port=port

    self.create_server()


    def create_server(self):

    self.server_socket=

    Bluetooth.BluetoothSocket(Bluetooth.RFCOMM)

     

    self.server_socket.bind(("",self.port))

    self.start_server()


    def start_server(self):

    while true:

    self.server_socket.listen(3)

    self.client_socket,address=server_socket.accept()

    self.client_socket.send("Enter the file name")

    file_name= self.client_socket.recv(2048)

     

    if file_name is not None:

    transfer_data=open(file_name,'r').readlines()


    for data in transfer_data:

    self.client_socket.send(data)

     

    self.client_socket.send("Transfer complete")

    Next, let us start the server. To do that we first need to check whether the main function is being executed. If it is being executed, then we create the instance of the server.

    from bluetooth import *


    class rfcomm_server:

    def __init__(self,port):

    self.port=port

    self.create_server()


    def create_server(self):

    self.server_socket=

    Bluetooth.BluetoothSocket(Bluetooth.RFCOMM)

     

    self.server_socket.bind(("",self.port))

    self.start_server()


    def start_server(self):

    while true:

    self.server_socket.listen(3)

    self.client_socket,address=server_socket.accept()

    self.client_socket.send("Enter the file name")

    file_name= self.client_socket.recv(2048)

     

    if file_name is not None:

    transfer_data=open(file_name,'r').readlines()


    for data in transfer_data:

    self.client_socket.send(data)

     

    self.client_socket.send("Transfer complete")


    if '__name__'=='__main__':

    server=rfcomm_server(20)


    That completes the application. It also brings us to the end of this discussion. The discussion until now has not touched upon the topic of service discovery. The next article will be about service discovery. 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 article, I have discussed about using RFCOMM for network programming. Please...
     

       

    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