root/branch/BluetoothScanner/umitBluetooth/btcore.py @ 3378

Revision 3378, 7.1 kB (checked in by devtar, 2 years ago)

fixed import order. btcore updated.

Line 
1#!/usr/bin/env python
2# Copyright (C) 2008 Adriano Monteiro Marques.
3#
4# Author: Devtar Singh <devtar@gmail.com>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20import os
21import sys
22
23import pygtk
24pygtk.require('2.0')
25import gtk
26
27if(os.name=="mac"):
28   try:
29      import lightblue
30   except ImportError, msg:
31      print >> sys.stderr, "Error loading LightBlue dependency.Exiting UmitBT..."
32      raise
33else:
34   try:
35      import bluetooth
36   except ImportError, msg:
37      print >> sys.stderr, "Error loading PyBluez dependency. Exiting UmitBT..."
38      raise
39
40#from umitCore.I18N import _
41from higwidgets.higdialogs import HIGAlertDialog
42
43import interface
44import io
45
46# globals used due to functions such as set_info and ease of access in io.py. needs redesigning
47btname = [""]
48btmac = [""]
49btmanu = [""]
50btsdp = [""]
51sdpstatus = ""
52
53class btcore:
54
55   def __init__(self):
56        global btname
57        global btmac
58        global btmanu
59        global btsdp
60        global sdpstatus
61
62   def scan(self, interface):
63        print "Refresh Interface and Clear Cache"
64        self.clear(interface)
65        global btname
66        global btmac
67        global btsdp
68        global sdpstatus
69        count = 1
70        if(os.name=="mac"):
71                btdevices = lightblue.findservices()
72                # Lightblue Output: [('00:0E:6D:71:A2:0B', u'My6600', 5243396)]
73        else:
74                btdevices = bluetooth.discover_devices(lookup_names = True)
75                for addr, name in btdevices:                 
76                   if (name == ""):
77                      name = "N/A"
78                   btname.append(name)
79                   btmac.append(addr)
80                   print name
81        # first entry of btdevice is at index 1 hence +1 on initial
82        if (sdpstatus == "Enabled" and (len(btname) > 1)):       
83                self.sdp_scan(count)
84        else:
85                btsdp.append("SDP Disabled")
86                count+=1
87         # BTMAC input test
88        interface.progb.set_text("40%")
89        interface.progb.set_fraction(.4)
90        self.manufac()
91        interface.progb.set_text("75%")
92        interface.progb.set_fraction(.75)
93        self.map(interface)
94        interface.progb.set_text("100%")
95        interface.progb.set_fraction(1)
96        message_id = interface.status_bar.push(interface.context_id, "Scanning Completed")
97
98   def sdp_scan(self, btid):
99        global sdpstatus
100        global btmac
101        global btsdp
102        sdpbrowseinfo = ""
103        print "BTID: " + str(btid)
104        if(os.name=="mac"):
105                 sdpservices = lightblue.findservices(address=btmac[btid])
106                  # Lightblue Output: [('00:0D:93:19:C8:68', 10, 'OBEX Object Push')]
107        else:   
108                 sdpservices = bluetooth.find_service(address=btmac[btid])
109        if len(sdpservices) > 0:
110                 for svc in sdpservices:
111                         sdpbrowseinfo += "\n\nService Name: " + str(svc["name"]) + "\n Description: " + str(svc["description"]) + "\n Provided By: " + str(svc["provider"]) + "\n Protocol: " + str(svc["protocol"]) + "\n channel/PSM: " + str(svc["port"]) + "\n svc classes: " + str(svc["service-classes"]) + "\n profiles:  " + str(svc["profiles"]) + "\n service id: " + str(svc["service-id"])
112                         print sdpbrowseinfo
113                 btsdp.append("\nFound " + str(len(sdpservices)) + " services\n" + sdpbrowseinfo)
114        else:
115                 sdpbrowseinfo = "no services found"
116                 btsdp.append(sdpbrowseinfo)
117                 print sdpbrowseinfo   
118
119   def manufac(self):
120        global btname
121        global btmac
122        global btmanu
123        print "manufacturer detection"
124        count = 0
125        endsearch = False
126        btmacsearch = [""]
127        btnamesearch = [""]
128        # btdb.db search               
129        if(os.path.exists(os.path.join("db", "btdb.db"))):
130                print "start manufac detection"
131                btdb_dir = os.path.abspath(os.path.join("db", "btdb.db"))
132                btdb = open(btdb_dir, "rb")
133                while ((len(btname)-1) > count):
134                        count+= 1
135                        count2 = 0
136                        endsearch = False
137                        endread = False
138                        for line in btdb:
139                                count2+=1
140                                if(endread == False):
141                                        btbuf1, btbuf2 = line.split(";")
142                                        btmacsearch.append(btbuf1)
143                                        btnamesearch.append(btbuf2)
144                                if(btmac[count][:8] == btmacsearch[count2]):
145                                        btnamesearch[count2] = btnamesearch[count2].replace("\n", "")
146                                        btmanu.append(btnamesearch[count2])
147                                        print "Count: " + str(count) + "\nNAME: " + btname[count] + "\nMAC : " + btmac[count] + "\nManufacturer detected : " + btmanu[count]
148                                        endsearch = True
149                        endread = True
150                        print "Status : " + str(endsearch)
151                        if(endsearch == False):
152                                print "Null: " + btmac[count][:8]
153                                btmanu.append("null")
154                btdb.close()
155        else:                   
156                print "ERROR: Can't open btdb!"
157                dlg = HIGAlertDialog(None, message_format=('I/O Error'), secondary_text=("Cant open btdb.db"))
158                dlg.run()
159                dlg.destroy()
160
161   def map(self, interface):   
162        global btname
163        global btmac
164        global btmanu
165        count = 0
166        print str(len(btname)), btmanu[0]
167        while ((len(btname)-1) > count):
168         count+=1
169         if(btmanu[count].find("Nokia") > -1 ):
170          pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.abspath(os.path.join("pixmaps", "nokia.png")))
171          interface.mapmodel.append([btname[count], pixbuf])
172         elif(btmanu[count].find("Motorola") > -1):
173          pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.abspath(os.path.join("pixmaps", "moto.png")))
174          interface.mapmodel.append([btname[count], pixbuf])
175         elif(btmanu[count].find("Sony") > -1):
176          pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.abspath(os.path.join("pixmaps", "sony-eric.png")))
177          interface.mapmodel.append([btname[count], pixbuf])
178         else:
179          pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.abspath(os.path.join("pixmaps", "bt.png")))
180          interface.mapmodel.append([btname[count], pixbuf])
181
182   def set_info(self, interface, btid):
183         global btname
184         global btmac
185         global btmanu
186         global sdpstatus
187         btid+=1
188         print "Selected: " + str(interface.btmap.get_selected_items()) + "\n" + str(btid)       
189         interface.label.set_text("Device Details\t\t\t\t\n\nName: " + btname[btid] + "\nMAC: " + btmac[btid] + "\nManufacturer: " + btmanu[btid])
190         if (sdpstatus == "Enabled"):
191          interface.sdpview.get_buffer().set_text(btsdp[btid])
192
193   def set_sdp(self, status):
194         # Status of Enabled or Disabled
195         global sdpstatus
196         sdpstatus = status
197
198   def clear(self, interface):   
199        global btname
200        global btmac
201        global btmanu
202        global btsdp
203
204        btname = [""]
205        btmac = [""]
206        btmanu = [""]
207        btsdp = [""]
208        interface.progb.set_text("")               
209        interface.progb.set_fraction(0)
210        interface.mapmodel.clear()
211
212   def save_scan(self, interface):   
213         io.io().save()
214         interface.progb.set_text("100%")
215         interface.progb.set_fraction(1)
216         message_id = interface.status_bar.push(interface.context_id, "File Saved")
217
218   def load_scan(self, interface):     
219         io.io().load()
220         self.map(interface)
221         interface.progb.set_text("100%")
222         interface.progb.set_fraction(1)
223         message_id = interface.status_bar.push(interface.context_id, "File Loaded")
Note: See TracBrowser for help on using the browser.