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