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: 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 check_devices(self): 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 check_devices(self): def device_found(self): if self.target_device_address is not 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 if addr is not None: 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....
blog comments powered by Disqus |
|
|
|
|
|
|
|