r4858 - in developers/erin_yueh: . bt
erin_yueh at docs.openmoko.org
erin_yueh at docs.openmoko.org
Mon Dec 8 14:16:46 CET 2008
Author: erin_yueh
Date: 2008-12-08 14:16:45 +0100 (Mon, 08 Dec 2008)
New Revision: 4858
Added:
developers/erin_yueh/bt/
developers/erin_yueh/bt/browse_device.py
developers/erin_yueh/bt/bt_adapter.py
developers/erin_yueh/bt/bt_main.py
developers/erin_yueh/bt/pin.py
developers/erin_yueh/bt/preferences.py
developers/erin_yueh/bt/send_file.py
Log:
init bt manager
Added: developers/erin_yueh/bt/browse_device.py
===================================================================
--- developers/erin_yueh/bt/browse_device.py (rev 0)
+++ developers/erin_yueh/bt/browse_device.py 2008-12-08 13:16:45 UTC (rev 4858)
@@ -0,0 +1,136 @@
+#!/usr/bin/env python
+
+import pygtk
+pygtk.require('2.0')
+
+import gtk, gobject
+# Check for new pygtk: this is new class in PyGtk 2.4
+if gtk.pygtk_version < (2,3,90):
+ print "PyGtk 2.3.90 or later required for this example"
+ raise SystemExit
+
+import lightblue
+import bluetooth
+
+import bt_adapter
+DEVICE_TYPE = ('Miscellaneous','Computer','Phone','LAN/Network Access point','Audio/Video',
+ 'Peripheral','Imaging','Wearable','Toy','Uncategorized')
+
+
+class BrowseDevice:
+
+ # close the window and quit
+ def delete_event(self, widget, event, data=None):
+ print 'delete_event:', widget,event
+ self.w.destroy()
+ #gtk.main_quit()
+ return False
+
+ def add_row(self, b):
+
+ dev_list = lightblue.finddevices()
+ print dev_list
+ i0 = None
+ for dev in dev_list:
+ (addr, name, class_of_service) = dev
+ if addr not in self.lastseen:
+ name = self.adapter.getRemoteName(addr)
+ dev_type = self.adapter.getRemoteMajorClass(addr)
+ pincode = self.adapter.getPinCodeLength(addr)
+ row = (addr,name,dev_type,pincode)
+ i0 = self.w.sm.get_model().append(row)
+ # select the new row in each view
+ if i0:
+ sel = self.w.tv.get_selection()
+ i1 = self.w.sm.convert_child_iter_to_iter(None, i0)
+ sel.select_iter(i1)
+
+
+ def get_BT_adapter(self):
+
+ self.bt_addr = self.adapter.getAddress()
+ self.bt_name = self.adapter.getName()
+ self.bt_currmode = self.adapter.getMode()
+ print 'bt_addr', self.bt_addr
+ # list currently connected remote devices.
+ self.lastseen = self.adapter.retrieve_names_fromFile()
+
+ def col1_toggled_cb( self, treeview, path, column ):
+ print 'toggle_cb'
+ model = treeview.get_model()
+ iter = model.get_iter(path)
+ selected_addr = model.get_value(iter,0)
+ print selected_addr
+ self.connect_to_dv(selected_addr)
+
+ def connect_to_dv(self,addr):
+ print 'connecting....', addr
+ self.adapter.createBonding(addr)
+
+ def __init__(self):
+
+ # retrieve BT adapter info
+ self.adapter = bt_adapter.BluetoothAdapter()
+
+ self.get_BT_adapter()
+ #print self.lastseen_name,self.lastseen_class
+
+ # create a liststore with 2 columns
+ #self.liststore = gtk.ListStore(gtk.gdk.Pixbuf,str,str,str,gtk.gdk.Pixbuf)
+ # addr,name,type,security
+ self.liststore = gtk.ListStore(str,str,str,int)
+ # Create new windows
+ self.w = gtk.Window(gtk.WINDOW_TOPLEVEL)
+ win = self.w
+ win.set_title("Browse Devices")
+ win.set_size_request(220, 200)
+ win.connect("delete_event", self.delete_event)
+
+ win.vbox = gtk.VBox()
+ win.add(win.vbox)
+ win.sw = gtk.ScrolledWindow()
+ win.sm = gtk.TreeModelSort(self.liststore)
+ # Set sort column
+ win.sm.set_sort_column_id(1, gtk.SORT_ASCENDING)
+
+ win.tv = gtk.TreeView(win.sm)
+ win.tv.connect("row-activated", self.col1_toggled_cb)
+ win.vbox.pack_start(win.sw)
+
+ win.b = gtk.Button('Start Searching...')
+ win.b.connect('clicked', self.add_row)
+ win.vbox.pack_start(win.b, False)
+
+ win.sw.add(win.tv)
+ win.tv.column = [None]*2
+ win.tv.column[0] = gtk.TreeViewColumn('Name')
+ win.tv.column[1] = gtk.TreeViewColumn('Device Type')
+ win.tv.cell= [None]*2
+ win.tv.cell[0] = gtk.CellRendererText()
+ win.tv.cell[1] = gtk.CellRendererPixbuf()
+ # 'icon name', 'icon type'
+
+ win.tv.column[0].set_sort_column_id(1)
+ win.tv.column[0].pack_start(win.tv.cell[1], True)
+ win.tv.column[0].pack_start(win.tv.cell[0], True)
+ win.tv.column[0].set_attributes(win.tv.cell[1], stock_id=1)
+ win.tv.column[0].set_attributes(win.tv.cell[0], text=1)
+ win.tv.append_column(win.tv.column[0])
+
+ win.tv.column[1].set_sort_column_id(2)
+ win.tv.column[1].pack_start(win.tv.cell[0], True)
+ win.tv.column[1].pack_start(win.tv.cell[1], True)
+ win.tv.column[1].set_attributes(win.tv.cell[0], text=2)
+ win.tv.column[1].set_attributes(win.tv.cell[1], stock_id=1)
+ win.tv.append_column(win.tv.column[1])
+
+
+ # display last seen devices
+ for addr in self.lastseen:
+ name = self.adapter.getRemoteName(addr)
+ dev_type = self.adapter.getRemoteMajorClass(addr)
+ pincode = self.adapter.getPinCodeLength(addr)
+ row = (addr,name,dev_type,pincode)
+ self.w.sm.get_model().append(row)
+
+ win.show_all()
Added: developers/erin_yueh/bt/bt_adapter.py
===================================================================
--- developers/erin_yueh/bt/bt_adapter.py (rev 0)
+++ developers/erin_yueh/bt/bt_adapter.py 2008-12-08 13:16:45 UTC (rev 4858)
@@ -0,0 +1,329 @@
+#!/usr/bin/env python
+
+import gtk, gobject
+import dbus
+from dbus.mainloop.glib import DBusGMainLoop
+DBusGMainLoop(set_as_default=True)
+bus = dbus.SystemBus()
+
+class BluetoothAdapter(object):
+
+ def __init__(self):
+
+ #self.btObj = bus.get_object( "org.freesmartphone.odeviced", "/org/freesmartphone/Device/PowerControl/Bluetooth")
+ #self.btInf = dbus.Interface( self.btObj, "org.freesmartphone.Device.PowerControl" )
+
+ self.managerObj = bus.get_object('org.bluez', '/org/bluez')
+ self.managerInf = dbus.Interface(self.managerObj, 'org.bluez.Manager')
+ # /org/bluez/hci0
+ self.defaultadapter = self.managerInf.DefaultAdapter()
+
+ #self.adapterObj = bus.get_object('org.bluez', self.managerInf.DefaultAdapter())
+ self.adapterObj = bus.get_object('org.bluez', '/org/bluez/hci0')
+ self.adapterInf = dbus.Interface(self.adapterObj, 'org.bluez.Adapter')
+
+ bus.add_signal_receiver(self.bonding_created_signal, 'BondingCreated', 'org.bluez.Adapter', 'org.bluez', self.defaultadapter)
+ bus.add_signal_receiver(self.bonding_removed_signal, 'BondingRemoved', 'org.bluez.Adapter', 'org.bluez', self.defaultadapter)
+
+ bus.add_signal_receiver(self.disc_started_signal, 'DiscoveryStarted', 'org.bluez.Adapter', 'org.bluez', self.defaultadapter)
+ bus.add_signal_receiver(self.disc_completed_signal, 'DiscoveryCompleted', 'org.bluez.Adapter', 'org.bluez', self.defaultadapter)
+
+ bus.add_signal_receiver(self.rem_dev_connected_signal, 'RemoteDeviceConnected', 'org.bluez.Adapter', 'org.bluez', self.defaultadapter)
+ bus.add_signal_receiver(self.rem_dev_disconnected_signal, 'RemoteDeviceDisconnected', 'org.bluez.Adapter', 'org.bluez', self.defaultadapter)
+
+
+ bus.add_signal_receiver(self.rem_dev_found_signal, 'RemoteDeviceFound', 'org.bluez.Adapter', 'org.bluez', self.defaultadapter)
+ bus.add_signal_receiver(self.rem_dev_disappeared_signal, 'RemoteDeviceDisappeared', 'org.bluez.Adapter', 'org.bluez', self.defaultadapter)
+ bus.add_signal_receiver(self.rem_dev_name_signal, 'RemoteNameUpdated', 'org.bluez.Adapter', 'org.bluez', self.defaultadapter)
+
+ bus.add_signal_receiver(self.mode_changed_signal, 'ModeChanged', 'org.bluez.Adapter', 'org.bluez', self.defaultadapter)
+
+ self.device_list = {}
+
+ def retrieve_names_fromFile(self):
+ # read last seen devices
+ adapter_address = self.getAddress()
+ self.fname = "/var/lib/bluetooth/"+adapter_address+"/names"
+ self.lastseen = []
+ try:
+ self.f = open(self.fname, 'r')
+ for line in self.f:
+ self.lastseen.append(line[0:17])
+ self.f.close()
+ except Exception:
+ print 'cannot find this file', self.fname
+ return self.lastseen
+
+ def bonding_created_signal(self,address):
+ print 'Signal: BondingCreated(%s)' % address
+
+ def bonding_removed_signal(self,address):
+ print 'Signal: BondingRemoved(%s)' % address
+
+ def disc_started_signal(self,):
+ print 'Signal: DiscoveryStarted()'
+
+ def disc_completed_signal(self):
+ print 'Signal: DiscoveryCompleted()'
+
+ def rem_dev_disappeared_signal(self, address):
+ print 'Signal: RemoteDeviceDisappeared()', address
+ if address in self.device_list:
+ self.device_list.pop(address)
+
+ def rem_dev_found_signal(self,address, cls, rssi):
+ #print 'Signal: RemoteDeviceFound(%s, %s, %s)' % (address, cls, rssi)
+ name = self.adapterInf.GetRemoteName(address)
+ _type = self.adapterInf.GetRemoteMajorClass(address)
+ security = self.getPinCodeLength(address)
+ self.device_list[address] = (name,_type,security)
+
+ def rem_dev_name_signal(self,address, name):
+ print 'Signal: RemoteNameUpdated(%s, %s)' % (address, name)
+
+ def rem_dev_connected_signal(self,address):
+ print 'Signal: RemoteDeviceConnected(%s)' % address
+
+ def rem_dev_disconnected_signal(self,address):
+ print 'Signal: RemoteDeviceDisconnected(%s)' % address
+
+ def mode_changed_signal(self,mode):
+ print 'Signal: ModeChanged(%s)' % mode
+
+ def getPower(self):
+
+ return self.btInf.GetPower()
+ try:
+ self.btPower = self.btInf.GetPower()
+ return self.btPower
+ except Exception:
+ print 'getPower error'
+ return None
+
+ def setPower(self,status):
+ try:
+ self.btInf.SetPower(status)
+ except Exception:
+ print 'setPower error', status
+
+ def getInfo(self):
+ try:
+ self.Info = self.adapterInf.GetInfo()
+ return self.Info
+ except Exception:
+ print 'getInfo error'
+ return None
+
+ def getAddress(self):
+ try:
+ self.Address = self.adapterInf.GetAddress()
+ return self.Address
+ except Exception:
+ print 'getAddress error'
+ return None
+
+ def getName(self):
+ try:
+ self.Name = self.adapterInf.GetName()
+ return self.Name
+ except Exception:
+ print 'getName error'
+ return None
+
+ def getMode(self):
+ try:
+ self.Mode = self.adapterInf.GetMode()
+ return self.Mode
+ except Exception:
+ print 'getMode error'
+ return None
+
+ def getMajorClass(self):
+ try:
+ self.MajorClass = self.adapterInf.GetMajorClass()
+ return self.MajorClass
+ except Exception:
+ print 'getMajorClass error'
+ return None
+
+ def setName(self,name):
+ try:
+ self.adapterInf.SetName(str(name))
+ except Exception:
+ print 'setName error', name
+
+ def setMode(self,mode):
+ try:
+ self.adapterInf.SetMode(str(mode))
+ except Exception:
+ print 'setMode error', Mode
+
+ def getDiscoverableTimeout(self):
+ try:
+ self.DiscoverableTimeout = self.adapterInf.GetDiscoverableTimeout()
+ return self.DiscoverableTimeout
+ except Exception:
+ print 'getDiscoverableTimeout error'
+ return None
+
+ def setDiscoverableTimeout(self,second):
+ try:
+ self.adapterInf.SetDiscoverableTimeout(second)
+ except Exception:
+ print 'setDiscoverableTimeout error', second
+
+ def getServiceClasses(self):
+ try:
+ self.ServiceClasses = self.adapterInf.GetServiceClasses()
+ return self.ServiceClasses
+ except Exception:
+ print 'getServiceClasses error'
+ return None
+
+ def listBondings(self):
+
+ return self.adapterInf.ListBondings()
+
+ def listConnections(self):
+
+ return self.adapterInf.ListConnections()
+
+ def hasBonding(self,addr):
+ try:
+ self.hasBonding = self.adapterInf.HasBonding(addr)
+ return self.hasBonding
+ except dbus.exceptions.DBusException,e:
+ print 'hasBonding error: ',addr,e
+ return None
+
+ def createBonding(self,addr):
+ print 'createBonding', addr
+ try:
+ self.adapterInf.CreateBonding(addr)
+ except dbus.exceptions.DBusException,e:
+ print 'createBonding error: ',addr,e
+
+ def removeBonding(self,addr):
+ try:
+ self.adapterInf.RemoveBonding(addr)
+ except dbus.exceptions.DBusException,e:
+ print 'removeBonding error: ',addr,e
+
+ def getPinCodeLength(self,addr):
+ try:
+ self.PinCodeLength = self.adapterInf.GetPinCodeLength(addr)
+ return self.PinCodeLength
+ except Exception:
+ return 0
+
+ def setTrusted(self,addr):
+ try:
+ self.adapterInf.SetTrusted(addr)
+ except Exception:
+ print 'setTrusted error', addr
+
+ def isTrusted(self,addr):
+ try:
+ self.isTrusted = self.adapterInf.IsTrusted(addr)
+ return self.isTrusted
+ except dbus.exceptions.DBusException,e:
+ print 'isTrusted error: ',addr,e
+ return None
+
+ def removeTrust(self,addr):
+ try:
+ self.adapterInf.RemoveTrust(addr)
+ except Exception:
+ print 'removeTrust error', addr
+
+ def getEncryptionKeySize(self,addr):
+ try:
+ self.EncryptionKeySize = self.adapterInf.GetEncryptionKeySize(addr)
+ return self.EncryptionKeySize
+ except Exception:
+ return None
+
+ def listTrusts(self):
+
+ return self.adapterInf.ListTrusts()
+
+ def getRemoteInfo(self,addr):
+ try:
+ self.RemoteInfo = self.adapterInf.GetRemoteInfo(addr)
+ return self.RemoteInfo
+ except dbus.exceptions.DBusException,e:
+ print 'getRemoteInfo: ',addr,e
+ return None
+
+ def getRemoteClass(self,addr):
+ try:
+ self.RemoteClass = self.adapterInf.GetRemoteClass(addr)
+ return self.RemoteClass
+ except dbus.exceptions.DBusException,e:
+ print 'getRemoteClass: ',addr,e
+ return None
+
+ def getRemoteName(self,addr):
+ try:
+ self.RemoteName = self.adapterInf.GetRemoteName(addr)
+ return self.RemoteName
+ except dbus.exceptions.DBusException,e:
+ print 'getRemoteName: ',addr,e
+ return None
+
+
+ def getRemoteMajorClass(self,addr):
+ try:
+ self.RemoteMajorClass = self.adapterInf.GetRemoteMajorClass(addr)
+ return self.RemoteMajorClass
+ except dbus.exceptions.DBusException,e:
+ print 'getRemoteMajorClass: ',addr,e
+ return None
+
+ def getRemoteServiceClasses(self,addr):
+ try:
+ self.RemoteServiceClasses = self.adapterInf.GetRemoteServiceClasses(addr)
+ return self.RemoteServiceClasses
+ except dbus.exceptions.DBusException,e:
+ print 'GetRemoteServiceClasses: ',addr,e
+ return None
+
+ def getRemoteServiceHandles(self,addr,match):
+ try:
+ print addr,match
+ self.RemoteServiceHandles = self.adapterInf.GetRemoteServiceHandles(addr,match)
+ return self.RemoteServiceHandles
+ except dbus.exceptions.DBusException,e:
+ print 'getRemoteServiceHandles: ',addr,match,e
+ return None
+
+ def getRemoteServiceRecord(self,addr,handle):
+ try:
+ self.RemoteServiceRecord = self.adapterInf.GetRemoteServiceRecord(addr,handle)
+ return self.RemoteServiceRecord
+ except dbus.exceptions.DBusException,e:
+ print 'getRemoteServiceRecordAsXML: ',addr,handle,e
+ return None
+
+ def getRemoteServiceRecordAsXML(self,addr,handle):
+ try:
+ self.RemoteServiceRecordAsXML = self.adapterInf.GetRemoteServiceRecordAsXML(addr,handle)
+ return self.RemoteServiceRecordAsXML
+ except dbus.exceptions.DBusException,e:
+ print 'getRemoteServiceRecordAsXML: ',addr,handle,e
+ return None
+
+
+ def listRemoteDevices(self):
+ try:
+ self.ListRemoteDevices = self.adapterInf.ListRemoteDevices()
+ return self.ListRemoteDevices
+ except dbus.exceptions.DBusException,e:
+ print 'ListRemoteDevices: ',e
+ return None
+
+ def discoverDevices(self):
+ try:
+ self.ListRemoteDevices = self.adapterInf.DiscoverDevices()
+ except dbus.exceptions.DBusException,e:
+ print 'discoverDevices: ',e
Added: developers/erin_yueh/bt/bt_main.py
===================================================================
--- developers/erin_yueh/bt/bt_main.py (rev 0)
+++ developers/erin_yueh/bt/bt_main.py 2008-12-08 13:16:45 UTC (rev 4858)
@@ -0,0 +1,95 @@
+#!/usr/bin/env python
+
+import gtk, gobject
+import dbus
+import browse_device, pin, send_file, bt_adapter
+
+POWER_STATE = ('OFF', 'ON')
+
+
+
+class bt_main(object):
+
+ def power_status_cb(self,widget):
+ print 'power_status_cb', widget
+ '''
+ curr_status = bluetooth_adapter.getPower()
+ if curr_status == 1:
+ bluetooth_adapter.setPower(False)
+ button.set_label("OFF")
+ print 'set Power OFF'
+ elif curr_status == 0:
+ bluetooth_adapter.setPower(True)
+ button.set_label("ON")
+ print 'set Power ON'
+ '''
+
+ def browse_device_cb(self,widget):
+ print 'browse_device_cb', widget
+ browse = browse_device.BrowseDevice()
+
+ def send_file_cb(self,widget):
+ print 'send_file_cb', widget
+ send_file.FileSelectionExample()
+
+ def preferences_cb(self,widget):
+ print 'send_preferences_cb', widget
+
+ def __init__(self):
+ # build bluetooth adapter
+ self.bluetooth_adapter = bt_adapter.BluetoothAdapter()
+
+ # Create the user interface
+ win = gtk.Window()
+ win.connect('delete-event', gtk.main_quit)
+
+ vbox = gtk.VBox()
+ win.add (vbox)
+ hbox = gtk.HBox()
+ vbox.pack_start (hbox)
+
+ # Bluetooth Power
+ #status = bt_iface.GetPower()
+ status = 1
+ power_button = gtk.Button()
+ power_button.set_label('Power Status: '+ POWER_STATE[status])
+ power_button.connect ("clicked", self.power_status_cb)
+ vbox.pack_start (power_button)
+
+ # Browse Devices
+ device_button = gtk.Button()
+ device_button.set_label('Browse Devices')
+ device_button.connect ("clicked", self.browse_device_cb)
+ vbox.pack_start (device_button)
+
+ # Send File
+ file_button = gtk.Button()
+ file_button.set_label('Send File')
+ file_button.connect ("clicked", self.send_file_cb)
+ vbox.pack_start (file_button)
+
+ # preferences
+ pref_button = gtk.Button()
+ pref_button.set_label('Perferences')
+ pref_button.connect ("clicked", self.preferences_cb)
+ vbox.pack_start (pref_button)
+
+ '''
+ # PassKeyEvent
+
+ PATH = '/my/PasskeyAgent'
+ handler = pin.PasskeyAgent(PATH)
+ adapter = bus.get_object('org.bluez', '/org/bluez/hci0')
+ secuirty = dbus.Interface(adapter, 'org.bluez.Security')
+ secuirty.RegisterDefaultPasskeyAgent(PATH)
+ '''
+ # Show all widgets and start the main loop
+ win.show_all()
+
+def main():
+ gtk.main()
+
+if __name__ == "__main__":
+ bt_main()
+ main()
+
Added: developers/erin_yueh/bt/pin.py
===================================================================
--- developers/erin_yueh/bt/pin.py (rev 0)
+++ developers/erin_yueh/bt/pin.py 2008-12-08 13:16:45 UTC (rev 4858)
@@ -0,0 +1,77 @@
+import dbus
+import dbus.glib
+import dbus.service
+import gobject
+import sys
+import gtk
+
+
+def pin_entry(address):
+ # build dialog
+ dialog = gtk.Dialog("New bluetooth connection", None, gtk.DIALOG_MODAL,
+ (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
+ dialog.set_default_response(gtk.RESPONSE_ACCEPT)
+ dialog.set_position(gtk.WIN_POS_CENTER)
+
+ # tabulka
+ table = gtk.Table(2, 2, False)
+ dialog.vbox.pack_start(table)
+ table.show()
+
+ # entry
+ entry = gtk.Entry()
+ entry.set_activates_default(True)
+ table.attach(entry, 1, 2, 1, 2, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 10, 2)
+ entry.show()
+
+ # icon
+ icon = gtk.Image()
+ icon.set_from_stock(gtk.STOCK_DIALOG_AUTHENTICATION, gtk.ICON_SIZE_DIALOG)
+ table.attach(icon, 0, 1, 0, 2, gtk.FILL, gtk.FILL, 12, 12)
+ icon.show()
+
+ # label
+ label = gtk.Label("New bluetooth connection to " + address + "\nPlease enter PIN:")
+ label.set_alignment(0, 1)
+ table.attach(label, 1, 2, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 10, 2)
+ label.show()
+
+ # run dialog
+ if dialog.run() == gtk.RESPONSE_ACCEPT:
+ pin = entry.get_text()
+ else:
+ pin = ""
+ dialog.destroy()
+ return pin
+
+
+class PasskeyAgent(dbus.service.Object):
+ def __init__(self, path):
+ dbus.service.Object.__init__(self, dbus.SystemBus(), path)
+
+ @dbus.service.method(dbus_interface='org.bluez.PasskeyAgent',
+ in_signature='ssb', out_signature='s')
+
+ def Request(self, path, address, numeric):
+
+ print 'Request pin code', path, address
+ try:
+ pin = pin_entry(address)
+ return pin
+ except:
+ return ""
+
+ def Cancel(self,path,address,numeric):
+ print 'cancel'
+
+
+if __name__ == "__main__":
+ PATH = '/my/PasskeyAgent'
+ bus = dbus.SystemBus();
+ handler = PasskeyAgent(PATH)
+ adapter = bus.get_object('org.bluez', '/org/bluez/hci0')
+ sec = dbus.Interface(adapter, 'org.bluez.Security')
+ sec.RegisterDefaultPasskeyAgent(PATH)
+ main_loop = gobject.MainLoop()
+ main_loop.run()
+
Added: developers/erin_yueh/bt/preferences.py
===================================================================
Added: developers/erin_yueh/bt/send_file.py
===================================================================
--- developers/erin_yueh/bt/send_file.py (rev 0)
+++ developers/erin_yueh/bt/send_file.py 2008-12-08 13:16:45 UTC (rev 4858)
@@ -0,0 +1,153 @@
+#!/usr/bin/env python
+
+import pygtk
+pygtk.require('2.0')
+
+import gtk, gobject
+# Check for new pygtk: this is new class in PyGtk 2.4
+if gtk.pygtk_version < (2,3,90):
+ print "PyGtk 2.3.90 or later required for this example"
+ raise SystemExit
+
+from xml.dom.minidom import parseString
+
+import lightblue
+import bt_adapter
+
+class FileSelectionExample:
+ # Get the selected filename and print it to the console
+ def file_ok_sel(self, w):
+ print "%s" % self.filew.get_filename()
+ dev = SelectDevice(self.filew.get_filename())
+ self.filew.destroy()
+
+ def destroy(self, widget):
+ self.filew.destroy()
+
+ def __init__(self):
+
+ # Create a new file selection widget
+ self.filew = gtk.FileSelection("File selection")
+
+ self.filew.connect("destroy", self.destroy)
+ # Connect the ok_button to file_ok_sel method
+ self.filew.ok_button.connect("clicked", self.file_ok_sel)
+
+ # Connect the cancel_button to destroy the widget
+ self.filew.cancel_button.connect("clicked",
+ lambda w: self.filew.destroy())
+
+ # Lets set the filename, as if this were a save dialog,
+ # and we are giving a default filename
+ self.filew.set_filename("penguin.png")
+
+ self.filew.show()
+
+class SelectDevice:
+
+ # close the window and quit
+ def delete_event(self, widget, event, data=None):
+ print 'delete_event:', widget,event
+ self.w.destroy()
+ return False
+
+ def col1_toggled_cb( self, treeview, path, column ):
+ print 'toggle_cb'
+ model = treeview.get_model()
+ iter = model.get_iter(path)
+ selected_dev_name = model.get_value(iter,0)
+ addr = self.dev_list[selected_dev_name]
+ print selected_dev_name,addr
+ self.prepare_to_send(addr)
+
+ def __browsexml(self, doc):
+ record=doc.documentElement
+
+ for node in record.childNodes:
+ if node.nodeType == node.ELEMENT_NODE and node.getAttribute('id') == '0x0004':
+ val = node.getElementsByTagName('uint8')
+ return int(val[0].getAttribute('value'), 16)
+
+ def resolveService(self, addr, service='FTP' ):
+
+ service_handle = self.adapter.getRemoteServiceHandles(addr,'FTP')
+ if service_handle:
+ xml= self.adapter.getRemoteServiceRecordAsXML(addr,service_handle[0])
+ doc=parseString(xml)
+ return self.__browsexml(doc)
+ else:
+ return None
+
+ def prepare_to_send(self,addr):
+ print 'prepare to send',addr,self.sendfname
+ channel = self.resolveService(addr)
+ if channel:
+ print 'sendfile', addr, channel,self.sendfname
+ try:
+ lightblue.obex.sendfile(addr,channel,self.sendfname)
+ except Exception,e:
+ print 'sendfile ERROR',e
+ else:
+ print 'No FTP service'
+
+
+ def start_sending(self, widget):
+ print 'starting_sending'
+
+ def __init__(self,name):
+ print 'SelectDevice: dev_list'
+ self.sendfname = name
+
+ # create a liststore with 2 columns
+ self.liststore = gtk.ListStore(str, str)
+
+ # Create new windows
+ self.w = gtk.Window(gtk.WINDOW_TOPLEVEL)
+ win = self.w
+ win.set_title("Select Device")
+ win.set_size_request(220, 200)
+ win.connect("delete_event", self.delete_event)
+
+ win.vbox = gtk.VBox()
+ win.add(win.vbox)
+ win.sw = gtk.ScrolledWindow()
+ win.sm = gtk.TreeModelSort(self.liststore)
+ # Set sort column
+ win.sm.set_sort_column_id(1, gtk.SORT_ASCENDING)
+
+ win.tv = gtk.TreeView(win.sm)
+ win.tv.connect("row-activated", self.col1_toggled_cb)
+ win.vbox.pack_start(win.sw)
+
+ win.b = gtk.Button('Send File...')
+ win.b.connect('clicked', self.start_sending)
+ win.vbox.pack_start(win.b, False)
+
+ win.sw.add(win.tv)
+ win.tv.column = [None]*2
+ win.tv.column[0] = gtk.TreeViewColumn('Name')
+ win.tv.column[1] = gtk.TreeViewColumn('Device Type')
+ win.tv.cell = [None]*2
+
+ for i in range(2):
+ win.tv.cell[i] = gtk.CellRendererText()
+ win.tv.append_column(win.tv.column[i])
+ win.tv.column[i].set_sort_column_id(i)
+ win.tv.column[i].pack_start(win.tv.cell[i], True)
+ win.tv.column[i].set_attributes(win.tv.cell[i], text=i)
+
+ # display ListBondings
+ #self.obj = bus.get_object('org.bluez', '/org/bluez/hci0')
+ #self.adapter = dbus.Interface(self.obj, 'org.bluez.Adapter')
+ self.adapter = bt_adapter.BluetoothAdapter()
+ list_bonding = self.adapter.listBondings()
+ print list_bonding
+ self.dev_list = {}
+
+ for addr in list_bonding:
+ name = self.adapter.getRemoteName(addr)
+ dtype = self.adapter.getRemoteMajorClass(addr)
+ self.w.sm.get_model().append([name,dtype])
+ self.dev_list[name] = addr
+
+ win.show_all()
\ No newline at end of file
More information about the commitlog
mailing list