bthildon.py

#!/usr/bin/python2.4
#auth: Pekka Jappinen
#desc:  This program searches SPP supporting bluetooth devices on it's
#       proximity.
#version: 0.1
# $Id: btsample.py,v 0.1
#
#

import bluetooth
import sys
import os

import hildon
import gtk

#these are not used, but may be needed later
import pango
import gobject

class BtHildon(hildon.Program):
	def __init__(self):
		hildon.Program.__init__(self)
		self.window =hildon.Window()
		self.add_window(self.window)
		self.window.set_title("BtHildonTest")
		#when x is pressed on main window call onExit function
		self.window.connect('destroy', self.onExit)
		
		#let's divide the window in 2 horisontal boxes 
		#first hbox is further divided in two vertical boxes
		self._createPanel()

		#adding button for inquiry
		self.button = gtk.Button("Inquiry")
		#when button is pressed call DoInquiry function
		self.button.connect('clicked',self.DoInquiry)
		#add button in the vertical box created  and show it
		self.vboxes[0].add(self.button)
		self.button.show()

		#let's add message area in the other vertical box
		self.mArea = gtk.TextView()
		self.mArea.set_wrap_mode(1)  
		self.vboxes[1].add(self.mArea)
		self.mArea.show()

		#let's reserve vboxes[1] for other messages
		self.mArea2 = gtk.TextView()
		self.mArea2.set_wrap_mode(1)  
		self.vboxes[0].add(self.mArea2)
		self.mArea2.show()

		#scrolled window (for treeview)
		self.scrollW =gtk.ScrolledWindow()
		self.vboxes[0].add(self.scrollW)
		self.scrollW.show()

		#btDevices contains list of found bluetooth devices
		self.btDevices = ["haba","duba","boba"]
		self._treeTest()
		#time to show all in window
		self.window.show_all()
		#notes coming up
	# quit application
	def onExit(self,widget):
		gtk.main_quit()
	def showNote(self,mess):
		dialog = hildon.Note("information",(self.window,mess))
		dialog.run()
		dialog.destroy()
	
#this function does Bluetooth inquiry (called when button is pushed)
	def DoInquiry(self,widget):
		self.btDevices = [] 
		buff = self.mArea2.get_buffer()
		buff.set_text("Inquiring...\n")

    		try:
        		self.btDevices = bluetooth.discover_devices()
    		except bluetooth.BluetoothError,e:
			mess = "Bluetooth Error %s" %e
			dialog = hildon.Note("information",(self.window,mess))
			dialog.run()
			dialog.destroy()
		else:
        		print "no BT devices around"
		if self.btDevices:
	   		mess = "found devices: %s" %self.btDevices
   			self.showNote(mess)
			print "%i devices found" %len(self.btDevices)
		mess = "%i devices found" %len(self.btDevices)
		buff = self.mArea.get_buffer()
		buff.set_text(mess)
		for tmpdev in self.btDevices:
			mess = "\n%s" %tmpdev
			buff.insert_at_cursor(mess)		
		self.UpdateBtTree()

	def BTConnect(self,widget):
	    	recv_data = ""
		sb=self.btSelected
		#let's try to figure out the content of selection
		model,iter = sb.get_selected()
		selection=self.treeS.get_value(iter,0)

    		self.rSock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
		#let's try to connect to rfcomm port 1 
		rPort = 1
		rDev= selection    
    		print "trying to connect to %s:%s" % (rDev,rPort)
    		try:
        		self.rSock.connect((rDev, rPort))            
    		except:
    			mess = "no Connection to %s" %rDev
   			self.showNote(mess)
  		else:
	    		recv_data=self.rSock.recv(1024)
    			print "Data Received: %s \n" % recv_data
	    		recv_data=self.rSock.recv(1024)
    			print "Data Received: %s \n" % recv_data
	    		self.rSock.close()  

	def _treeTest(self):
		#we create list which has one string per row)
		self.treeS = gtk.TreeStore(gobject.TYPE_STRING)
		for tmp in self.btDevices:
			iter = self.treeS.append(None)
			self.treeS.set(iter,0,tmp)
		self.treeView = gtk.TreeView(self.treeS)
		#now we append columns for our tree		
		self.tvcol = gtk.TreeViewColumn('hops')
		self.treeView.append_column(self.tvcol)

		#finally cell renderer tells how to display the stuff in column
		self.cell = gtk.CellRendererText()
		self.tvcol.pack_start(self.cell,True)
		self.tvcol.add_attribute(self.cell, 'text',0)
		
		#and add the treeview to scrollwindow
		self.scrollW.add(self.treeView)
#		self.vboxes[0].add(self.treeView)

		#let's try connection to selected device	
		self.btSelected = self.treeView.get_selection()
#		self.btSelected.connect("changed", self.selectnotify)

		#add button for creating connection to selected address
		self.cbutton = gtk.Button("Connect")
		self.cbutton.connect('clicked',self.BTConnect)
		self.vboxes[0].add(self.cbutton)
		self.cbutton.show()


	def UpdateBtTree(self):
		self.treeS.clear()
		for tmp in self.btDevices:
			iter = self.treeS.append(None)
			self.treeS.set(iter,0,tmp)

	def selectnotify (self,treesel):
	   		mess = "selected: %s" %self.treeView.get_selection()
   			self.showNote(mess)
			
#dividing the window in parts
	def _createPanel(self):
		#first create a horisontal box as a container to main window 
		self.hbox = gtk.HBox()
		self.window.add(self.hbox)
		#then create vertical boxes inside horsontal boxes 
		#let's make a list from these boxes i.e have vboxes [0] and vbox[1]
		self.vboxes = []
		self.vboxes.append (gtk.VBox())
		self.vboxes.append (gtk.VBox())
		#and pack all vboxes in hbox1 without filling and padding
		for vbox in self.vboxes:
			self.hbox.pack_start(vbox,expand=True)

		
	def run(self):
		gtk.main()
    
""" different ways to find services in bluetooth
    slist = find_service(address="00:11:22:33:44:55)
     slist = find_service(name="Serial Port")

"""

if __name__ == "__main__":
   BtHildon().run()
Last modified: 2013/07/01 14:41