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



     
     
    >>> 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 5 Hosted by Hostway
    Stay green...Green IT