Home arrow Python arrow Page 5 - Bluetooth Programming in Python: Network Programming using RFCOMM

RFCOMM in the Real World - Python

In the last article I discussed various Bluetooth profiles. If one wants to create a client-server based application using Bluetooth, then one should program for the RFCOMM profile. RFCOMM offers a socket-based client-server paradigm for providing services.

TABLE OF CONTENTS:
  1. Bluetooth Programming in Python: Network Programming using RFCOMM
  2. Developing Applications for RFCOMM, Step by Step
  3. Listening for requests
  4. Developing the Client
  5. RFCOMM in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 10
December 12, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: